Close() or Dispose()...

Interesting post from Jim Blizzard.Still the debate continue but yes agree MSDN docs are the reference.

 

 

Rory asks us (http://neopoleon.com/blog/posts/2889.aspx)

about calling Close() or Dispose() on ADO.NET objects.  He went into ILDASM to see if Dispose() also calls Close(), and sure enough, it does.

 

And all he had to do was look in the MSDN docs.  Cause we know that the MSDN docs are always right, don't we.  ;)

 

public void SqlConnectionHereAndGone()

{

SqlConnection myConnection = new

SqlConnection("Initial Catalog=Northwind;Data Source=localhost;Integrated Security=SSPI;");

myConnection.Open();

//Calling Dispose also calls SqlConnection.Close.

myConnection.Dispose();

}

 

Look at that comment in the sample code (straight from MSDN):  "Calling Dispose also calls SqlConnection.Close."

 

Amazing.  :)

 

 

1 Comment

  • That's true, but it doesn't call cancel on any commands associated with the connection. So, while you may be tempted to just dispose(), in the case of a failed command, I'd also suggest cancel(), too.

Comments have been disabled for this content.