By and large, all you need to know about a IDBConnection object is that you need to Open it before you make a request and you should Close it when you are done with it. That bit of knowledge can get you pretty far, but if you want to really learn about the connection object, there's a few other things you might be interested in.
I had recently written an article describing the ConnectionState property of the SqlClient and OleDbClient objects. As I mentioned, only two of the enumerated values are supported, namely ConnectionState.Open and ConnectionState.Closed. However, let's say you wanted to know if something went wrong with the connection in the middle of some action, for instance, someone tripped on the network cable and pulled it out of your machine. The StateChange event is fired whenever the State of a connection changes, so you could wire an event handler to let you know WHEN a connection has successfully been made and when/if it's been closed. (This example came from MSDN, but there were some modifications that needed to be made to get it to run):
VB.NET
| Protected Shared Sub OnStateChange(ByVal sender As Object, ByVal e As StateChangeEventArgs) Console.WriteLine("The current Connection state has changed from {0} to {1}.", _ e.OriginalState, e.CurrentState) End Sub |
| private void cnMain_StateChange(object sender, System.Data.StateChangeEventArgs e) { Debug.WriteLine("The current Connection state has changed from {0} to {1}.", e.OriginalState, e.CurrentState); } |
| AddHandler cnMain.StateChange, New StateChangeEventHandler(AddressOf OnStateChange) |
| this.cnMain.StateChange += new System.Data.StateChangeEventHandler(this.cnMain_StateChange); |
| daFacilities.Fill(dsEmployees1, "Facilities") |
| daFacilities.Fill(dsEmployees1, "Facilities"); |
| The current Connection state has changed from Closed to Open. The current Connection state has changed from Open to Closed. |
| AddHandler cnMain.InfoMessage, New OleDbInfoMessageEventHandler(AddressOf OnInfoMessage) |
| this.cnMain.InfoMessage += new System.Data.SqlClient.SqlInfoMessageEventHandler(this.cnMain_InfoMessage); Now, if we get an error message back with a severity level less than 10 in SqlServer (I wasn't able to find it out for the other providers) it will be contained in the Errors collection. As such, you can interrogate it an find out about subtle errors that may have occured but weren't deal breakers as such: VB.NET Private Shared Sub OnInfoMessage(ByVal sender As Object, ByVal args As System.Data.SqlClient.SqlInfoMessageEventArgs) Dim err As System.Data.SqlClient.SqlError For Each err In args.Errors Console.WriteLine("The {0} has received a severity {1}, state {2} error number {3}\n" & _ "on line {4} of procedure {5} on server {6}:\n{7}", _ err.Source, err.Class, err.State, err.Number, err.LineNumber, _ err.Procedure, err.Server, err.Message) Next End Sub |
| private void cnMain_InfoMessage(object sender, System.Data.SqlClient.SqlInfoMessageEventArgs e) { foreach (System.Data.SqlClient.SqlError err in e.Errors) { Console.WriteLine("The {0} has received a severity {1}, state {2} error number {3}\n" + "on line {4} of procedure {5} on server {6}:\n{7}", err.Source, err.Class, err.State, err.Number, err.LineNumber, err.Procedure, err.Server, err.Message); } } |