Does SqlCommand.Dispose close the connection?

后端 未结 4 1087
一生所求
一生所求 2020-11-27 15:19

Can I use this approach efficiently?

using(SqlCommand cmd = new SqlCommand(\"GetSomething\", new SqlConnection(Config.ConnectionString))
{
    cmd.Connection         


        
相关标签:
4条回答
  • 2020-11-27 15:35

    SqlCommand.Dispose will not be sufficient because many SqlCommand(s) can (re)use the same SqlConnection. Center your focus on the SqlConnection.

    0 讨论(0)
  • 2020-11-27 15:46

    Soooo many places get this wrong, even MS' own documentation. Just remember - in DB world, almost everything is backed by an unmanaged resource, so almost everything implements IDisposable. Assume a class does unless the compiler tells you otherwise. Wrap your command in a using. Wrap your connection in a using. Create your connection off a DbProvider (get that from DbProviderFactories.GetFactory), and your command off your connection so that if you change your underlying DB, you only need to change the call to DBPF.GetFactory. So your code should end up looking nice and symmetrical:

    var provider = DbProviderFactories.GetFactory("System.Data.SqlClient");// Or MS.Data.SqlClient
    using (var connection = provider.CreateConnection())
    {
        connection.ConnectionString = "...";
        using (var command = connection.CreateCommand())
        {
            command.CommandText = "...";
            connection.Open();
    
            using (var reader = command.ExecuteReader())
            {
    ...
            }
        }
    }
    
    0 讨论(0)
  • 2020-11-27 15:48

    No, Disposing of the SqlCommand will not effect the Connection. A better approach would be to also wrap the SqlConnection in a using block as well:

    using (SqlConnection conn = new SqlConnection(connstring))
    {
        conn.Open();
        using (SqlCommand cmd = new SqlCommand(cmdstring, conn))
        {
            cmd.ExecuteNonQuery();
        }
    }
    

    Otherwise, the Connection is unchanged by the fact that a Command that was using it was disposed (maybe that is what you want?). But keep in mind, that a Connection should be disposed of as well, and likely more important to dispose of than a command.

    EDIT:

    I just tested this:

    SqlConnection conn = new SqlConnection(connstring);
    conn.Open();
    
    using (SqlCommand cmd = new SqlCommand("select field from table where fieldid = 1", conn))
    {
        Console.WriteLine(cmd.ExecuteScalar().ToString());
    }
    
    using (SqlCommand cmd = new SqlCommand("select field from table where fieldid = 2", conn))
    {
        Console.WriteLine(cmd.ExecuteScalar().ToString());
    }
    
    conn.Dispose();  
    

    The first command was disposed when the using block was exited. The connection was still open and good for the second command.

    So, disposing of the command definitely does not dispose of the connection it was using.

    0 讨论(0)
  • 2020-11-27 16:00

    I use this pattern. I have this private method somewhere in my app:

    private void DisposeCommand(SqlCommand cmd)
    {
        try
        {
            if (cmd != null)
            {
                if (cmd.Connection != null)
                {
                    cmd.Connection.Close();
                    cmd.Connection.Dispose();
                }
                cmd.Dispose();
            }
        }
        catch { } //don't blow up
    }
    

    Then I always create SQL commands and connections in a try block (but without being wrapped in a using block) and always have a finally block as:

        finally
        {
            DisposeCommand(cmd);
        }
    

    The connection object being a property of the command object makes a using block awkward in this situation - but this pattern gets the job done without cluttering up your code.

    0 讨论(0)
提交回复
热议问题