OleDbConnection クラス
アセンブリ: System.Data (system.data.dll 内)

Public NotInheritable Class OleDbConnection Inherits DbConnection Implements ICloneable, IDbConnection, IDisposable
public sealed class OleDbConnection : DbConnection, ICloneable, IDbConnection, IDisposable
public ref class OleDbConnection sealed : public DbConnection, ICloneable, IDbConnection, IDisposable
public final class OleDbConnection extends DbConnection implements ICloneable, IDbConnection, IDisposable
public final class OleDbConnection extends DbConnection implements ICloneable, IDbConnection, IDisposable

OleDbConnection オブジェクトは、データ ソースへの一意な接続を表します。クライアント/サーバー データベース システムでは、サーバーへのネットワーク接続に相当します。ネイティブの OLE DB プロバイダがサポートする機能によっては、OleDbConnection オブジェクトの一部のメソッド、またはプロパティが利用できないこともあります。
OleDbConnection のインスタンスを作成すると、すべてのプロパティが初期値に設定されます。これらの初期値の一覧については、OleDbConnection コンストラクタのトピックを参照してください。
OleDbConnection は、適用範囲外では閉じられません。そのため、Close または Dispose を呼び出すか、OleDbConnection オブジェクトを Using ステートメント内に記述することによって、明示的に接続を閉じる必要があります。
![]() |
---|
パフォーマンスの高いアプリケーションを配置するには、接続プールを使用する必要があります。.NET Framework OLE DB 用データ プロバイダを使用する場合は、プロバイダが接続プールを自動的に管理するため、この機能を有効にする必要はありません。.NET Framework OLE DB 用データ プロバイダによる接続プールの使用方法の詳細については、「接続プールについて」を参照してください。 |
OleDbCommand を実行したメソッドで、致命的な OleDbException (SQL Server の重大度レベルが 20 以上など) が発生した場合は、OleDbConnection が閉じられることがあります。ただし、ユーザーが接続を再び開いて、処理を継続できます。
OleDbConnection オブジェクトのインスタンスを作成するアプリケーションは、宣言セキュリティまたは強制セキュリティの要求を設定することによって、直接的または間接的な呼び出し元すべてに対して、コードへの適切なアクセス許可を要求できます。OleDbConnection は、OleDbPermission オブジェクトを使用して、セキュリティ要求を作成します。ユーザーは、OleDbPermissionAttribute オブジェクトを使用して、コードに適切なアクセス許可が設定されているかどうかを確認できます。また、ユーザーおよび管理者は、コード アクセス セキュリティ ポリシー ツール (Caspol.exe) を使用して、コンピュータ、ユーザー、エンタープライズの各レベルでセキュリティ ポリシーを変更できます。詳細については、「コード アクセス セキュリティと ADO.NET」を参照してください。
データ サーバーから受け取る警告メッセージと情報メッセージの処理の詳細については、「接続イベントの使用」を参照してください。

OleDbCommand と OleDbConnection を作成する例を次に示します。OleDbConnection が開かれ、OleDbCommand 用の Connection として設定されます。この例では、次に、ExecuteNonQuery を呼び出し、接続を閉じます。この例では、ExecuteNonQuery に、接続文字列と SQL INSERT ステートメントのクエリ文字列が渡されます。
Public Sub InsertRow(ByVal connectionString As String, _ ByVal insertSQL As String) Using connection As New OleDbConnection(connectionString) ' The insertSQL string contains a SQL statement that ' inserts a new row in the source table. Dim command As New OleDbCommand(insertSQL) ' Set the Connection to the new OleDbConnection. command.Connection = connection ' Open the connection and execute the insert command. Try connection.Open() command.ExecuteNonQuery() Catch ex As Exception Console.WriteLine(ex.Message) End Try ' The connection is automatically closed when the ' code exits the Using block. End Using End Sub
public void InsertRow(string connectionString, string insertSQL) { using (OleDbConnection connection = new OleDbConnection(connectionString)) { // The insertSQL string contains a SQL statement that // inserts a new row in the source table. OleDbCommand command = new OleDbCommand(insertSQL); // Set the Connection to the new OleDbConnection. command.Connection = connection; // Open the connection and execute the insert command. try { connection.Open(); command.ExecuteNonQuery(); } catch (Exception ex) { Console.WriteLine(ex.Message); } // The connection is automatically closed when the // code exits the using block. }
using System; using System.Data; using System.Data.OleDb; class Class1 { static void Main() { } public void InsertRow(string connectionString, string insertSQL) { using (OleDbConnection connection = new OleDbConnection(connectionString)) { // The insertSQL string contains a SQL statement that // inserts a new row in the source table. OleDbCommand command = new OleDbCommand(insertSQL); // Set the Connection to the new OleDbConnection. command.Connection = connection; // Open the connection and execute the insert command. try { connection.Open(); command.ExecuteNonQuery(); } catch (Exception ex) { Console.WriteLine(ex.Message); } // The connection is automatically closed when the // code exits the using block. }

System.MarshalByRefObject
System.ComponentModel.Component
System.Data.Common.DbConnection
System.Data.OleDb.OleDbConnection


Windows 98, Windows 2000 SP4, Windows Millennium Edition, Windows Server 2003, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP SP2, Windows XP Starter Edition
開発プラットフォームの中には、.NET Framework によってサポートされていないバージョンがあります。サポートされているバージョンについては、「システム要件」を参照してください。


- OleDbConnection クラスのページへのリンク