Export (0) Print
Expand All

IDbConnection Interface

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

For a list of all members of this type, see IDbConnection Members.

System.IDisposable
   System.Data.IDbConnection

[Visual Basic]
Public Interface IDbConnection
   Inherits IDisposable
[C#]
public interface IDbConnection : IDisposable
[C++]
public __gc __interface IDbConnection : public IDisposable
[JScript]
public interface IDbConnection implements IDisposable

Classes that Implement IDbConnection

Class Description
OdbcConnection Represents an open connection to a data source.
OleDbConnection Represents an open connection to a data source.
OracleConnection Represents an open connection to a database. This class cannot be inherited.
SqlCeConnection Represents an open connection to a data source.
SqlConnection Represents an open connection to a SQL Server database. This class cannot be inherited.

Remarks

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 Using ADO.NET. For more information about implementing .NET Framework data providers, see Implementing a .NET Framework Data Provider.

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:

Item Description
PrvConnection() 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.

Example

[Visual Basic, C#, C++] 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.

[Visual Basic] 
Public Sub InsertRow(myConnectionString As String)
    ' If the connection string is null, use a default.
    If myConnectionString = "" Then
        myConnectionString = "Initial Catalog=Northwind;Data Source=localhost;Integrated Security=SSPI;"
    End If
    Dim myConnection As New SqlConnection(myConnectionString)
    Dim myInsertQuery As String = "INSERT INTO Customers (CustomerID, CompanyName) Values('NWIND', 'Northwind Traders')"
    Dim myCommand As New SqlCommand(myInsertQuery)
    myCommand.Connection = myConnection
    myConnection.Open()
    myCommand.ExecuteNonQuery()
    myCommand.Connection.Close()
End Sub 'SelectSqlClientSrvRows

[C#] 
public void InsertRow(string myConnectionString) 
 {
    // If the connection string is null, use a default.
    if(myConnectionString == "") 
    {
       myConnectionString = "Initial Catalog=Northwind;Data Source=localhost;Integrated Security=SSPI;";
    }
    SqlConnection myConnection = new SqlConnection(myConnectionString);
    string myInsertQuery = "INSERT INTO Customers (CustomerID, CompanyName) Values('NWIND', 'Northwind Traders')";
    SqlCommand myCommand = new SqlCommand(myInsertQuery);
    myCommand.Connection = myConnection;
    myConnection.Open();
    myCommand.ExecuteNonQuery();
    myCommand.Connection.Close();
 }

[C++] 
public:
void InsertRow(String* myConnectionString) 
 {
    // If the connection string is null, use a default.
    if(myConnectionString->Equals(S""))
    {
       myConnectionString = S"Initial Catalog=Northwind;Data Source=localhost;Integrated Security=SSPI;";
    }
    SqlConnection* myConnection = new SqlConnection(myConnectionString);
    String* myInsertQuery = S"INSERT INTO Customers (CustomerID, CompanyName) Values('NWIND', 'Northwind Traders')";
    SqlCommand* myCommand = new SqlCommand(myInsertQuery);
    myCommand->Connection = myConnection;
    myConnection->Open();
    myCommand->ExecuteNonQuery();
    myCommand->Connection->Close();
 }

[JScript] No example is available for JScript. To view a Visual Basic, C#, or C++ example, click the Language Filter button Language Filter in the upper-left corner of the page.

Requirements

Namespace: System.Data

Platforms: Windows 98, Windows NT 4.0, Windows Millennium Edition, Windows 2000, Windows XP Home Edition, Windows XP Professional, Windows Server 2003 family, .NET Compact Framework

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

See Also

IDbConnection Members | System.Data Namespace

Show:
© 2014 Microsoft