使用 using 语句的 SqlConnection 和 SqlCommand

SqlConnection and SqlCommand with using statement

我连接到这样设置的数据库以调用存储过程。我只是想知道这是否是最好的方法。 我有两个 using 语句,一个用于 sqlConnection,一个用于 sqlCommand(我不确定是否需要)

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();
    }                
}

我最大的问题是我什么时候做的:

using (SqlCommand cmd1 = new SqlCommand())

现在的做法好吗..或者应该更像,

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

我认为你的方式很好,因为the using statement will ensure that the object is disposed of either way