IDbConnection インターフェイス

定義

データ ソースへのオープン接続を表し、リレーショナル データベースにアクセスする .NET データ プロバイダーにより実装されます。

public interface class IDbConnection : IDisposable
public interface IDbConnection : IDisposable
type IDbConnection = interface
    interface IDisposable
Public Interface IDbConnection
Implements IDisposable
派生
実装

次の例では、 SqlCommandSqlConnection派生クラスと のインスタンスを作成します。 SqlConnectionが開き、 の としてConnection設定されます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 は 名前空間の SqlConnectionSystem.Data.SqlClient クラスのプレフィックスです。

インターフェイスから IDbConnection 継承する場合は、次のコンストラクターを実装する必要があります。

アイテム 説明
PrvConnection() PrvConnection クラスの新しいインスタンスを初期化します。
PrvConnection(string connectionString) 接続文字列を含む文字列が指定されたときに、PrvConnection クラスの新しいインスタンスを初期化します。

プロパティ

ConnectionString

データベースを開くために使用する文字列を取得または設定します。

ConnectionTimeout

試行を終了してエラーを生成するまでの、接続の確立の試行時に待機する時間 (秒単位) を取得します。

Database

現在のデータベース、または接続が開いてから使用するデータベースの名前を取得します。

State

現在の接続の状態を取得します。

メソッド

BeginTransaction()

データベース トランザクションを開始します。

BeginTransaction(IsolationLevel)

指定した IsolationLevel 値を使用して、データベース トランザクションを開始します。

ChangeDatabase(String)

開いている Connection オブジェクトの現在のデータベースを変更します。

Close()

データベースへの接続を閉じます。

CreateCommand()

接続に関連付けられた Command オブジェクトを作成し、返します。

Dispose()

アンマネージ リソースの解放またはリセットに関連付けられているアプリケーション定義のタスクを実行します。

(継承元 IDisposable)
Open()

プロバイダー固有の接続オブジェクトの ConnectionString プロパティで指定した設定でデータベース接続を開きます。

適用対象