SqlConnection and SqlCommand with using statement

I have a connection to a database set up like this to call a stored procedure. I am just wondering if this is the best way to do this. I have two using statements one for the sqlConnection and one for the sqlCommand (which I am not really sure if its needed)

using (SqlConnection con1 = new SqlConnection(conString1))
{
    using (SqlCommand cmd1 = new SqlCommand())
    {
        cmd1.Connection = con1;

        cmd1.CommandType = CommandType.StoredProcedure;
        cmd1.CommandText = "updateVendorEstNo";
        cmd1.Parameters.AddWithValue("@plantNameNew", vPlantName.Value.ToString().Trim());

        var result = cmd1.Parameters.Add("@result", SqlDbType.Int);
        result.Direction = ParameterDirection.Output;
        var resultDesc = cmd1.Parameters.Add("@resultDesc", SqlDbType.VarChar, 100);
        resultDesc.Direction = ParameterDirection.Output;

        con1.Open(); // open connection
        cmd1.ExecuteNonQuery();

        res = result.Value.ToString().Trim();
        resDesc = resultDesc.Value.ToString().Trim();
    }                
}

My biggest question is when I am doing :

using (SqlCommand cmd1 = new SqlCommand())

Is it fine the way it is done right now.. or should it be more like,

using (SqlCommand cmd1 = new SqlCommand("updateVendorEstNo",con1))

我认为你拥有它的方式很好,因为using声明将确保对象以任何方式处理。

链接地址: http://www.djcxy.com/p/20376.html

上一篇: 单独项目中的MVC解决方案中的Web API

下一篇: 使用语句的SqlConnection和SqlCommand