<p>Can I use this approach efficiently?</p>

<pre><code>using(SqlCommand cmd = new SqlCommand("GetSomething", new SqlConnection(Config.ConnectionString))
{
    cmd.Connection.Open();
    // set up parameters and CommandType to StoredProcedure etc. etc.
    cmd.ExecuteNonQuery();
}
</code></pre>

<p>My concern is : Will the Dispose method of the SqlCommand (which is called when exiting the using block) close the underlying SqlConnection object or not?</p>

[{'answer_id': 60934, 'author': 'Ryan Farley', 'author_id': 1627, 'author_profile': 'https://Stackoverflow.com/users/1627', 'pm_score': 8, 'selected': True, 'text': '<p>No, Disposing of the <code>SqlCommand</code> will not effect the Connection. A better approach would be to also wrap the <code>SqlConnection</code> in a using block as well:  </p>\n\n<pre><code>using (SqlConnection conn = new SqlConnection(connstring))\n{\n    conn.Open();\n    using (SqlCommand cmd = new SqlCommand(cmdstring, conn))\n    {\n        cmd.ExecuteNonQuery();\n    }\n}\n</code></pre>\n\n<p>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 \nbe disposed of as well, and likely more important to dispose of than a command.  </p>\n\n<p><strong>EDIT:</strong>  </p>\n\n<p>I just tested this:  </p>\n\n<pre><code>SqlConnection conn = new SqlConnection(connstring);\nconn.Open();\n\nusing (SqlCommand cmd = new SqlCommand("select field from table where fieldid = 1", conn))\n{\n    Console.WriteLine(cmd.ExecuteScalar().ToString());\n}\n\nusing (SqlCommand cmd = new SqlCommand("select field from table where fieldid = 2", conn))\n{\n    Console.WriteLine(cmd.ExecuteScalar().ToString());\n}\n\nconn.Dispose();  \n</code></pre>\n\n<p>The first command was disposed when the using block was exited. The connection was still open and good for the second command.   </p>\n\n<p><strong><em>So, disposing of the command definitely does not dispose of the connection it was using.</em></strong></p>\n'}, {'answer_id': 185227, 'author': 'user26320', 'author_id': 26320, 'author_profile': 'https://Stackoverflow.com/users/26320', 'pm_score': 4, 'selected': False, 'text': '<p>SqlCommand.Dispose will not be sufficient because many SqlCommand(s) can (re)use the same SqlConnection.  Center your focus on the SqlConnection.</p>\n'}, {'answer_id': 5654064, 'author': 'Chuck Bevitt', 'author_id': 704658, 'author_profile': 'https://Stackoverflow.com/users/704658', 'pm_score': -1, 'selected': False, 'text': "<p>I use this pattern. I have this private method somewhere in my app:</p>\n\n<pre><code>private void DisposeCommand(SqlCommand cmd)\n{\n    try\n    {\n        if (cmd != null)\n        {\n            if (cmd.Connection != null)\n            {\n                cmd.Connection.Close();\n                cmd.Connection.Dispose();\n            }\n            cmd.Dispose();\n        }\n    }\n    catch { } //don't blow up\n}\n</code></pre>\n\n<p>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:</p>\n\n<pre><code>    finally\n    {\n        DisposeCommand(cmd);\n    }\n</code></pre>\n\n<p>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.</p>\n"}, {'answer_id': 59635946, 'author': 'Keith Patrick', 'author_id': 12670837, 'author_profile': 'https://Stackoverflow.com/users/12670837', 'pm_score': 1, 'selected': False, 'text': '<p>Soooo many places get this wrong, even MS\' own documentation.  Just remember - in DB world, almost <em>everything</em> is backed by an unmanaged resource, so almost everything implements IDisposable.  Assume a class does unless the compiler tells you otherwise.\nWrap your command in a using.  Wrap your <em>connection</em> 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.\nSo your code should end up looking nice and symmetrical:</p>\n\n<pre><code>var provider = DbProviderFactories.GetFactory("System.Data.SqlClient");// Or MS.Data.SqlClient\nusing (var connection = provider.CreateConnection())\n{\n    connection.ConnectionString = "...";\n    using (var command = connection.CreateCommand())\n    {\n        command.CommandText = "...";\n        connection.Open();\n\n        using (var reader = command.ExecuteReader())\n        {\n...\n        }\n    }\n}\n</code></pre>\n'}]
['https://Stackoverflow.com/questions/60919', 'https://Stackoverflow.com', 'https://Stackoverflow.com/users/1796/']