IDbConnection インターフェイス
重要
一部の情報は、リリース前に大きく変更される可能性があるプレリリースされた製品に関するものです。 Microsoft は、ここに記載されている情報について、明示または黙示を問わず、一切保証しません。
データ ソースへのオープン接続を表し、リレーショナル データベースにアクセスする .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 クラスを実装できます。これは、データ ソースとの一意のセッション (サーバーへのネットワーク接続など) を表します。 Connection クラスの詳細については、「 データ ソースへの接続」を参照してください。
アプリケーションは インターフェイスの IDbConnection インスタンスを直接作成するのではなく、 を継承するクラスのインスタンスを作成します IDbConnection。
継承するクラスは、 IDbConnection 継承されたすべてのメンバーを実装し、通常はプロバイダー固有の機能を追加するために追加のメンバーを定義する必要があります。 たとえば、 インターフェイスは IDbConnection プロパティを ConnectionTimeout 定義します。 さらに、 クラスはこのプロパティを SqlConnection 継承し、 プロパティも定義します PacketSize 。
.NET Framework データ プロバイダー間の一貫性を高めるために、継承クラスに という形式PrvClassname
で名前を付けます。ここでPrv
、 は、特定の.NET Framework データ プロバイダー名前空間内のすべてのクラスに渡される均一なプレフィックスです。 たとえば、 Sql
は 名前空間の System.Data.SqlClient
クラスのSqlConnectionプレフィックスです。
インターフェイスから IDbConnection 継承する場合は、次のコンストラクターを実装する必要があります。
アイテム | 説明 |
---|---|
PrvConnection() | PrvConnection クラスの新しいインスタンスを初期化します。 |
PrvConnection(string connectionString) | 接続文字列を含む文字列が指定されたときに、PrvConnection クラスの新しいインスタンスを初期化します。 |
Connection |
データベースを開くために使用する文字列を取得または設定します。 |
Connection |
試行を終了してエラーを生成するまでの、接続の確立の試行時に待機する時間 (秒単位) を取得します。 |
Database |
現在のデータベース、または接続が開いてから使用するデータベースの名前を取得します。 |
State |
現在の接続の状態を取得します。 |
Begin |
データベース トランザクションを開始します。 |
Begin |
指定した IsolationLevel 値を使用して、データベース トランザクションを開始します。 |
Change |
開いている |
Close() |
データベースへの接続を閉じます。 |
Create |
接続に関連付けられた Command オブジェクトを作成し、返します。 |
Dispose() |
アンマネージ リソースの解放またはリセットに関連付けられているアプリケーション定義のタスクを実行します。 (継承元 IDisposable) |
Open() |
プロバイダー固有の接続オブジェクトの |
製品 | バージョン |
---|---|
.NET | Core 1.0, Core 1.1, Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9, 10 |
.NET Framework | 1.1, 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1 |
.NET Standard | 2.0, 2.1 |
.NET に関するフィードバック
.NET はオープンソース プロジェクトです。 フィードバックを提供するにはリンクを選択します。