Represents an open connection to a server.
The following sample demonstrates how using base classes helps to create database-independent code.
public void PrintAccount(DbConnectionBase myConnection) {
DbCommandBase myCommand = (DbCommandBase)myConnection.CreateCommand();
myCommand.CommandText = "SELECT * FROM Account";
myConnection.Open();
DbDataReader myReader = myCommand.ExecuteReader();
try {
while (myReader.Read()) {
Console.WriteLine(myReader.GetInt32(0) + " " +
myReader.GetString(1) + " " + myReader.GetString(2));
}
}
finally {
myReader.Close();
myConnection.Close();
}
}
Public Sub PrintAccount(ByVal myConnection As DbConnectionBase)
Dim myCommand As DbCommandBase = myConnection.CreateCommand()
myCommand.CommandText = "SELECT * FROM Account"
myConnection.Open()
Dim myReader As DbDataReader = myCommand.ExecuteReader()
Try
While myReader.Read()
Console.WriteLine(String.Concat(myReader.GetInt32(0), " ", _
myReader.GetString(1), " ", myReader.GetString(2)))
End While
Finally
myReader.Close()
myConnection.Close()
End Try
End Sub
Target Platforms: Windows 7, Windows Vista SP1 or later, Windows XP SP3, Windows Server 2008 (Server Core not supported), Windows Server 2008 R2 (Server Core supported with SP1 or later), Windows Server 2003 SP2