0 out of 2 rated this helpful - Rate this topic

IDbConnection Interface

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 : IDisposable

The IDbConnection type exposes the following members.

  Name Description
Public property Supported by the XNA Framework ConnectionString Gets or sets the string used to open a database.
Public property Supported by the XNA Framework ConnectionTimeout Gets the time to wait while trying to establish a connection before terminating the attempt and generating an error.
Public property Supported by the XNA Framework Database Gets the name of the current database or the database to be used after a connection is opened.
Public property Supported by the XNA Framework State Gets the current state of the connection.
Top
  Name Description
Public method Supported by the XNA Framework BeginTransaction() Begins a database transaction.
Public method Supported by the XNA Framework BeginTransaction(IsolationLevel) Begins a database transaction with the specified IsolationLevel value.
Public method Supported by the XNA Framework ChangeDatabase Changes the current database for an open Connection object.
Public method Supported by the XNA Framework Close Closes the connection to the database.
Public method Supported by the XNA Framework CreateCommand Creates and returns a Command object associated with the connection.
Public method Supported by the XNA Framework Dispose Performs application-defined tasks associated with freeing, releasing, or resetting unmanaged resources. (Inherited from IDisposable.)
Public method Supported by the XNA Framework Open Opens a database connection with the settings specified by the ConnectionString property of the provider-specific Connection object.
Top

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 (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:

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.

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.


private static void OpenSqlConnection()
{
    string connectionString = GetConnectionString();
    using (SqlConnection connection = new SqlConnection(connectionString))
    {
        connection.Open();
        Console.WriteLine("ServerVersion: {0}", connection.ServerVersion);
        Console.WriteLine("State: {0}", connection.State);
    }
}

static private string GetConnectionString()
{
    // To avoid storing the connection string in your code, 
    // you can retrieve it from a configuration file, using the 
    // System.Configuration.ConfigurationSettings.AppSettings property 
    return "Data Source=(local);Initial Catalog=AdventureWorks;"
        + "Integrated Security=SSPI;";
}


.NET Framework

Supported in: 4, 3.5, 3.0, 2.0, 1.1, 1.0

.NET Framework Client Profile

Supported in: 4, 3.5 SP1

Windows 7, Windows Vista SP1 or later, Windows XP SP3, Windows XP SP2 x64 Edition, Windows Server 2008 (Server Core not supported), Windows Server 2008 R2 (Server Core supported with SP1 or later), Windows Server 2003 SP2

The .NET Framework does not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.
Did you find this helpful?
(1500 characters remaining)
Community Content Add
Annotations FAQ
Interface inheritance
IDBConnection is an interface. Interfaces are implemented, not inherited. This article uses the the verb 'inherits', and it could cause confusion.
Example doesn't match Example description
The description of the example is incorrect. ExecuteNonQuery is not called. Nor is there any use of a SqlCommand.

"The following example creates instances of the derived classes, SqlCommand and SqlConnection. The SqlConnection is opened and set as the Connection for theSqlCommand. 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."