Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Does SqlCommand.Dispose close the connection?

Can I use this approach efficiently?

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

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?

like image 550
Andrei Rînea Avatar asked Sep 13 '08 22:09

Andrei Rînea


People also ask

Does dispose close connection?

Connections are automatically pooled, and calling Dispose / Close on the connection does not physically close the connection (under normal circumstances).

Do you need to dispose SqlCommand?

"Not calling dispose on the command won't do anything too bad." True, but don't get used to it; it's only true for SqlCommand s. On the other hand, not disposing a SqlCeCommand , for example, will cause your mobile device to run out of memory quite fast.

Is it necessary to dispose SqlConnection as well as SqlCommand?

yes , it is necessary to dispose the sqlconnection and sqlcommand object after your piece of code gets executed.

What is the use of SqlCommand?

A SqlCommand object allows you to specify what type of interaction you want to perform with a database. For example, you can do select, insert, modify, and delete commands on rows of data in a database table.


2 Answers

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.

like image 147
Ryan Farley Avatar answered Sep 22 '22 21:09

Ryan Farley


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

like image 42
user26320 Avatar answered Sep 24 '22 21:09

user26320