IDbConnection Interface


The .NET API Reference documentation has a new home. Visit the .NET API Browser on to see the new experience.

Represents an open connection to a data source, and is implemented by .NET Framework data providers that access relational databases.

Namespace:   System.Data
Assembly:  System.Data (in System.Data.dll)

Public Interface IDbConnection
	Inherits IDisposable


Gets or sets the string used to open a database.


Gets the time to wait while trying to establish a connection before terminating the attempt and generating an error.


Gets the name of the current database or the database to be used after a connection is opened.


Gets the current state of the connection.


Begins a database transaction.


Begins a database transaction with the specified IsolationLevel value.


Changes the current database for an open Connection object.


Closes the connection to the database.


Creates and returns a Command object associated with the connection.


Performs application-defined tasks associated with freeing, releasing, or resetting unmanaged resources.(Inherited from IDisposable.)


Opens a database connection with the settings specified by the ConnectionString property of the provider-specific Connection object.

The IDbConnection interface enables an inheriting class to implement a Connection class, which represents a unique session with a data source (for example, a network connection to a server). For more information about Connection classes, see Connecting to a Data Source in ADO.NET.

An application does not create an instance of the IDbConnection interface directly, but creates an instance of a class that inherits IDbConnection.

Classes that inherit IDbConnection must implement all inherited members, and typically define additional members to add provider-specific functionality. For example, the IDbConnection interface defines the ConnectionTimeout property. In turn, the SqlConnection class inherits this property, and also defines the PacketSize property.

Notes to Implementers:

To promote consistency among .NET Framework data providers, name the inheriting class in the form PrvClassname where Prv is the uniform prefix given to all classes in a specific .NET Framework data provider namespace. For example, Sql is the prefix of the SqlConnection class in the System.Data.SqlClient namespace.

When you inherit from the IDbConnection interface, you should implement the following constructors:




Initializes a new instance of the PrvConnection class.

PrvConnection(string connectionString)

Initializes a new instance of the PrvConnection class when given a string containing the connection string.

The following example creates instances of the derived classes, SqlCommand and SqlConnection. The SqlConnection is opened and set as the Connection for the SqlCommand. The example then calls ExecuteNonQuery, and closes the connection. To accomplish this, the ExecuteNonQuery is passed a connection string and a query string that is a Transact-SQL INSERT statement.

Imports System.Data

Class Program

    Public Shared Sub Main(args As String())

        Dim connection As IDbConnection

        ' First use a SqlClient connection

        connection = New System.Data.SqlClient.SqlConnection("Server=(localdb)\V11.0")

        Console.WriteLine("SqlClient" & vbCr & vbLf & "{0}", GetServerVersion(connection))

        connection = New System.Data.SqlClient.SqlConnection("Server=(local);Integrated Security=true")

        Console.WriteLine("SqlClient" & vbCr & vbLf & "{0}", GetServerVersion(connection))

        ' Call the same method using ODBC

        ' NOTE: LocalDB requires the SQL Server 2012 Native Client ODBC driver

        connection = New System.Data.Odbc.OdbcConnection("Driver={SQL Server Native Client 11.0};Server=(localdb)\v11.0")

        Console.WriteLine("ODBC" & vbCr & vbLf & "{0}", GetServerVersion(connection))

        connection = New System.Data.Odbc.OdbcConnection("Driver={SQL Server Native Client 11.0};Server=(local);Trusted_Connection=yes")

        Console.WriteLine("ODBC" & vbCr & vbLf & "{0}", GetServerVersion(connection))

        ' Call the same method using OLE DB

        connection = New System.Data.OleDb.OleDbConnection("Provider=SQLNCLI11;Server=(localdb)\v11.0;Trusted_Connection=yes;")

        Console.WriteLine("OLE DB" & vbCr & vbLf & "{0}", GetServerVersion(connection))

        connection = New System.Data.OleDb.OleDbConnection("Provider=SQLNCLI11;Server=(local);Trusted_Connection=yes;")

        Console.WriteLine("OLE DB" & vbCr & vbLf & "{0}", GetServerVersion(connection))

    End Sub

    Public Shared Function GetServerVersion(connection As IDbConnection) As String

        ' Ensure that the connection is opened (otherwise executing the command will fail)

        Dim originalState As ConnectionState = connection.State

        If originalState <> ConnectionState.Open Then


        End If


            ' Create a command to get the server version

            ' NOTE: The query's syntax is SQL Server specific

            Dim command As IDbCommand = connection.CreateCommand()

            command.CommandText = "SELECT @@version"

            Return DirectCast(command.ExecuteScalar(), String)


            ' Close the connection if that's how we got it

            If originalState = ConnectionState.Closed Then


            End If

        End Try

    End Function

End Class

.NET Framework
Available since 1.1
Return to top