IDbConnection 介面

定義

代表針對資料來源的開放連線,而且是由存取關聯式資料庫的 .NET Framework 資料提供者所實作。

public interface class IDbConnection : IDisposable
public interface IDbConnection : IDisposable
type IDbConnection = interface
    interface IDisposable
Public Interface IDbConnection
Implements IDisposable
衍生
實作

範例

下列範例會建立衍生類別 SqlCommandSqlConnection 的實例。 SqlConnection會開啟 ,並將 設定為 ConnectionSqlCommand 。 然後範例會呼叫 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)

為開啟的 Connection 物件變更目前的資料庫。

Close()

關閉與資料庫的連接。

CreateCommand()

建立並傳回與連線相關聯的 Command 物件。

Dispose()

執行與釋放 (Free)、釋放 (Release) 或重設 Unmanaged 資源相關聯之應用程式定義的工作。

(繼承來源 IDisposable)
Open()

使用提供者特定 Connection 物件的 ConnectionString 屬性所指定的設定來開啟資料庫連接。

適用於