IDbConnection 接口
定义
重要
一些信息与预发行产品相关,相应产品在发行之前可能会进行重大修改。 对于此处提供的信息,Microsoft 不作任何明示或暗示的担保。
表示一个与数据源的开放连接,并由访问关系数据库的 .NET 数据提供程序实现。
public interface class IDbConnection : IDisposable
public interface IDbConnection : IDisposable
type IDbConnection = interface
interface IDisposable
Public Interface IDbConnection
Implements IDisposable
- 派生
- 实现
示例
以下示例创建派生类的实例, SqlCommand 以及 SqlConnection。 The SqlConnection is opened and set as the Connection for the SqlCommand. 然后,该示例调用 ExecuteNonQuery并关闭连接。 为此,传递ExecuteNonQuery连接字符串和查询字符串,该字符串是 Transact-SQL INSERT 语句。
using System;
using System.Data;
namespace IDbConnectionSample {
class Program {
static void Main(string[] args) {
IDbConnection connection;
// First use a SqlClient connection
connection = new System.Data.SqlClient.SqlConnection(@"Server=(localdb)\V11.0");
Console.WriteLine("SqlClient\r\n{0}", GetServerVersion(connection));
connection = new System.Data.SqlClient.SqlConnection(@"Server=(local);Integrated Security=true");
Console.WriteLine("SqlClient\r\n{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\r\n{0}", GetServerVersion(connection));
connection = new System.Data.Odbc.OdbcConnection(@"Driver={SQL Server Native Client 11.0};Server=(local);Trusted_Connection=yes");
Console.WriteLine("ODBC\r\n{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\r\n{0}", GetServerVersion(connection));
connection = new System.Data.OleDb.OleDbConnection(@"Provider=SQLNCLI11;Server=(local);Trusted_Connection=yes;");
Console.WriteLine("OLE DB\r\n{0}", GetServerVersion(connection));
}
public static string GetServerVersion(IDbConnection connection) {
// Ensure that the connection is opened (otherwise executing the command will fail)
ConnectionState originalState = connection.State;
if (originalState != ConnectionState.Open)
connection.Open();
try {
// Create a command to get the server version
// NOTE: The query's syntax is SQL Server specific
IDbCommand command = connection.CreateCommand();
command.CommandText = "SELECT @@version";
return (string)command.ExecuteScalar();
}
finally {
// Close the connection if that's how we got it
if (originalState == ConnectionState.Closed)
connection.Close();
}
}
}
}
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
connection.Open()
End If
Try
' 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)
Finally
' Close the connection if that's how we got it
If originalState = ConnectionState.Closed Then
connection.Close()
End If
End Try
End Function
End Class
注解
该 IDbConnection 接口允许继承类实现 Connection 类,该类表示与数据源的唯一会话 (,例如,与服务器) 的网络连接。 有关连接类的详细信息,请参阅 “连接到数据源”。
应用程序不直接创建接口的 IDbConnection 实例,而是创建继承 IDbConnection的类的实例。
继承 IDbConnection 的类必须实现所有继承的成员,并且通常定义其他成员以添加特定于提供程序的功能。 例如,接口 IDbConnection 定义 ConnectionTimeout 属性。 反过来,类 SqlConnection 继承此属性,还定义 PacketSize 该属性。
实施者说明
若要在.NET Framework数据访问接口之间提升一致性,请在窗体PrvClassname
中命名继承类,其中Prv
为特定.NET Framework数据提供程序命名空间中的所有类提供统一前缀。 例如,Sql
是命名空间中System.Data.SqlClient
类的SqlConnection前缀。
从 IDbConnection 接口继承时,应实现以下构造函数:
项 | 描述 |
---|---|
PrvConnection () | 初始化 PrvConnection 类的新实例。 |
PrvConnection (字符串 connectionString) | 当给定包含连接字符串的字符串时,初始化 PrvConnection 类的新实例。 |
属性
ConnectionString |
获取或设置用于打数据库的字符串。 |
ConnectionTimeout |
获取在尝试建立连接时终止尝试并生成错误之前所等待的时间(以秒为单位)。 |
Database |
获取当前数据库的名称或打开连接后要使用的数据库的名称。 |
State |
获取连接的当前状态。 |
方法
BeginTransaction() |
开始一项数据库事务。 |
BeginTransaction(IsolationLevel) |
以指定的 IsolationLevel 值开始数据库事务。 |
ChangeDatabase(String) |
更改已打开的 |
Close() |
关闭与数据库之间的连接。 |
CreateCommand() |
创建并返回一个与连接关联的命令对象。 |
Dispose() |
执行与释放或重置非托管资源关联的应用程序定义的任务。 (继承自 IDisposable) |
Open() |
打开具有指定设置(由特定于提供程序的 Connection 对象的 |