.NET Framework Class Library 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)

Syntax
Public Interface IDbConnection _
Inherits IDisposable
public interface IDbConnection : IDisposable
public interface class IDbConnection : IDisposable
type IDbConnection =
interface
interface IDisposable
end
The IDbConnection type exposes the following members.

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 (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 ImplementersTo 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. |

Examples
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 Sub OpenSqlConnection()
Dim connectionString As String = GetConnectionString()
Using connection As New SqlConnection(connectionString)
connection.Open()
Console.WriteLine("ServerVersion: {0}", connection.ServerVersion)
Console.WriteLine("State: {0}", connection.State)
End Using
End Sub
Private Function GetConnectionString() As String
' 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);Database=AdventureWorks;" _
& "Integrated Security=SSPI;"
End Function
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;";
}

Version Information
.NET FrameworkSupported in: 4, 3.5, 3.0, 2.0, 1.1, 1.0 .NET Framework Client ProfileSupported in: 4, 3.5 SP1

Platforms
Windows 7, Windows Vista SP1 or later, Windows XP SP3, Windows XP SP2 x64 Edition, Windows Server 2008 (Server Core Role not supported), Windows Server 2008 R2 (Server Core Role not supported), 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.

See Also
|
Biblioteca de clases de .NET Framework IDbConnection (Interfaz) Representa una conexión abierta a un origen de datos y la implementan los proveedores de datos de .NET Framework que tienen acceso a bases de datos relacionales.
Espacio de nombres:
System.Data
Ensamblado:
System.Data (en System.Data.dll)

Sintaxis
Public Interface IDbConnection _
Inherits IDisposable
public interface IDbConnection : IDisposable
public interface class IDbConnection : IDisposable
type IDbConnection =
interface
interface IDisposable
end
El tipo IDbConnection expone los siguientes miembros.

Comentarios
La interfaz IDbConnection permite que una clase heredada implemente una clase Connection, que representa una sesión única con un origen de datos (por ejemplo, una conexión de red con un servidor). Para obtener más información sobre las clases Connection, vea Conectar con un origen de datos (ADO.NET). Una aplicación no crea una instancia de la interfaz IDbConnection directamente, sino que la crea de una clase que hereda de IDbConnection. Las clases que heredan IDbConnection deben implementar todos los miembros heredados y suelen definir miembros adicionales para agregar la funcionalidad específica de proveedor. Por ejemplo, la interfaz IDbConnection define la propiedad ConnectionTimeout. A su vez, la clase SqlConnection hereda esta propiedad y también define la propiedad PacketSize. Notas para los implementadoresPara potenciar la coherencia entre los proveedores de datos de .NET Framework, asigne a la clase heredada un nombre con el formato PrvClassname, donde Prv es el prefijo uniforme que se asigna a todas las clases de un espacio de nombres de proveedor de datos de .NET Framework específico. Por ejemplo, Sql es el prefijo de la clase SqlConnection en el espacio de nombres System.Data.SqlClient. Al heredar de la interfaz IDbConnection, se deben implementar los siguientes constructores: Elemento | Descripción |
|---|
PrvConnection() | Inicializa una nueva instancia de la clase PrvConnection. | PrvConnection(string connectionString) | Inicializa una nueva instancia de la clase PrvConnection cuando se proporciona una cadena que contiene la cadena de conexión. |

Ejemplos
En el ejemplo siguiente se crean instancias de las clases derivadas SqlCommand y SqlConnection. Se abre SqlConnection y se establece como Connection para SqlCommand. A continuación, en el ejemplo se llama a ExecuteNonQuery y se cierra la conexión. Para ello, se pasa a ExecuteNonQuery una cadena de conexión y una cadena de consulta que es una instrucción Transact-SQL INSERT.
Private Sub OpenSqlConnection()
Dim connectionString As String = GetConnectionString()
Using connection As New SqlConnection(connectionString)
connection.Open()
Console.WriteLine("ServerVersion: {0}", connection.ServerVersion)
Console.WriteLine("State: {0}", connection.State)
End Using
End Sub
Private Function GetConnectionString() As String
' 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);Database=AdventureWorks;" _
& "Integrated Security=SSPI;"
End Function
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;";
}

Información de versión
.NET FrameworkCompatible con: 4, 3.5, 3.0, 2.0, 1.1, 1.0 .NET Framework Client ProfileCompatible con: 4, 3.5 SP1

Plataformas
Windows 7, Windows Vista SP1 o posterior, Windows XP SP3, Windows XP SP2 x64 Edition, Windows Server 2008 (no se admite Server Core), Windows Server 2008 R2 (se admite Server Core con SP1 o posterior), Windows Server 2003 SP2
.NET Framework no admite todas las versiones de todas las plataformas. Para obtener una lista de las versiones compatibles, vea Requisitos de sistema de .NET Framework.

Vea también
|