1. 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?
答
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.
2. Does SqlDataAdapter.Dispose actually Close an associated SqlConnection?
问
Does anyone know if the SqlDataAdapter.Dispose method actually closes or disposes any SqlConnections? I loaded up Reflector and I see that SqlDataAdapter inherits from DbDataAdapter. If I disassemble and look at the dispose method in that class, there appears to be no disposal of any SqlConnections. I suppose I could write a test for this, but I figured I would ask to see if anyone had any insight on this.
答
The first thing to be aware of is that the DataAdapter does manage and close your connection in some circumstances. For example, if you're using a DataAdapter you're probably operating on DataTables/DataSets using the .Fill() and .Update() functions.
From the .Fill() docs:
The .Update() docs don't mention anything about the connection at all, so I would expect to need to manage it manually.
Now you asked specifically about the Dispose() method. Like Update, the Dispose() docs don't specifically mention the connection, so I would expect to need to close it manually.
Finally, we can improve on Bob King's code slightly like this:
Using conn as New SqlConnection(""), _ adapter as New SqlDataAdapter() With {.Connection = conn} 'Do stuff End Using
Or in C#:
using (SqlConnection conn = new SqlConnection("")) using (SqlDataAdapter adapter = new SqlDataAdapter() {Connection = conn}) { // Do stuff }
Not 100% I got the initialize syntax for the adapter right, but I typed it directly into the reply window. I'll fix it later if needed.