OleDbDataAdapter クラス
アセンブリ: System.Data (system.data.dll 内)
構文
Public NotInheritable Class OleDbDataAdapter Inherits DbDataAdapter Implements IDbDataAdapter, IDataAdapter, ICloneable
public ref class OleDbDataAdapter sealed : public DbDataAdapter, IDbDataAdapter, IDataAdapter, ICloneable
OleDbDataAdapter は、DataSet とデータ ソースの間でデータを取得および保存するための、ブリッジの役割を果たします。OleDbDataAdapter は、このブリッジを提供するために、データ ソース内のデータを DataSet に読み込む Fill、および DataSet での変更をデータ ソースに送り返す Update を使用します。
OleDbDataAdapter は、DataSet にデータを読み込むときに、返されたデータを格納するテーブルおよび列が存在しない場合は、それらを作成します。ただし、MissingSchemaAction プロパティを AddWithKey に設定しない限り、暗黙的に作成されたスキーマには主キー情報は設定されません。DataSet にデータを格納する前に、FillSchema を使用して、主キー情報を含むスキーマを OleDbDataAdapter に作成させることもできます。詳細については、「DataSet への既存の制約の追加」を参照してください。
MSDataShape プロバイダなど、OLE DB プロバイダによっては、ベース テーブルまたは主キー情報が返されないことに注意してください。したがって、OleDbDataAdapter は、作成された DataTable に対し、必ずしも PrimaryKey プロパティを正しく設定できるわけではありません。この場合は、DataSet 内のテーブルの主キーを明示的に指定する必要があります。
OleDbDataAdapter には、データの読み込みと更新を効率的に行うために、SelectCommand、InsertCommand、DeleteCommand、UpdateCommand、TableMappings の各プロパティも用意されています。
OleDbDataAdapter のインスタンスを作成すると、プロパティが初期値に設定されます。これらの初期値の一覧については、OleDbDataAdapter コンストラクタのトピックを参照してください。
OleDbCommand、OleDbDataAdapter、および OleDbConnection を使用して、Access データ ソースからレコードを選択し、選択した行を DataSet に格納する例を次に示します。次に、格納された DataSet が返されます。このメソッドには、初期化済みの DataSet、接続文字列、および SQL SELECT ステートメントのクエリ文字列が渡されます。
Public Function CreateDataAdapter(ByVal selectCommand As String, _ ByVal connection As OleDbConnection) As OleDbDataAdapter Dim adapter As OleDbDataAdapter = _ New OleDbDataAdapter(selectCommand, connection) adapter.MissingSchemaAction = MissingSchemaAction.AddWithKey ' Create the commands. adapter.InsertCommand = New OleDbCommand( _ "INSERT INTO Customers (CustomerID, CompanyName) " & _ "VALUES (?, ?)") adapter.UpdateCommand = New OleDbCommand( _ "UPDATE Customers SET CustomerID = ?, CompanyName = ? " & _ "WHERE CustomerID = ?") adapter.DeleteCommand = New OleDbCommand( _ "DELETE FROM Customers WHERE CustomerID = ?") ' Create the parameters. adapter.InsertCommand.Parameters.Add( _ "@CustomerID", OleDbType.Char, 5, "CustomerID") adapter.InsertCommand.Parameters.Add( _ "@CompanyName", OleDbType.VarChar, 40, "CompanyName") adapter.UpdateCommand.Parameters.Add( _ "@CustomerID", OleDbType.Char, 5, "CustomerID") adapter.UpdateCommand.Parameters.Add( _ "@CompanyName", OleDbType.VarChar, 40, "CompanyName") adapter.UpdateCommand.Parameters.Add( _ "@oldCustomerID", OleDbType.Char, 5, "CustomerID").SourceVersion = _ DataRowVersion.Original adapter.DeleteCommand.Parameters.Add( _ "@CustomerID", OleDbType.Char, 5, "CustomerID").SourceVersion = _ DataRowVersion.Original Return adapter End Function
public static OleDbDataAdapter CreateDataAdapter(string selectCommand, OleDbConnection connection) { OleDbDataAdapter adapter = new OleDbDataAdapter(selectCommand, connection); adapter.MissingSchemaAction = MissingSchemaAction.AddWithKey; // Create the Insert, Update and Delete commands. adapter.InsertCommand = new OleDbCommand( "INSERT INTO Customers (CustomerID, CompanyName) " + "VALUES (?, ?)"); adapter.UpdateCommand = new OleDbCommand( "UPDATE Customers SET CustomerID = ?, CompanyName = ? " + "WHERE CustomerID = ?"); adapter.DeleteCommand = new OleDbCommand( "DELETE FROM Customers WHERE CustomerID = ?"); // Create the parameters. adapter.InsertCommand.Parameters.Add("@CustomerID", OleDbType.Char, 5, "CustomerID"); adapter.InsertCommand.Parameters.Add("@CompanyName", OleDbType.VarChar, 40, "CompanyName"); adapter.UpdateCommand.Parameters.Add("@CustomerID", OleDbType.Char, 5, "CustomerID"); adapter.UpdateCommand.Parameters.Add("@CompanyName", OleDbType.VarChar, 40, "CompanyName"); adapter.UpdateCommand.Parameters.Add("@oldCustomerID", OleDbType.Char, 5, "CustomerID").SourceVersion = DataRowVersion.Original; adapter.DeleteCommand.Parameters.Add("@CustomerID", OleDbType.Char, 5, "CustomerID").SourceVersion = DataRowVersion.Original; return adapter; }
using System; using System.Data; using System.Data.OleDb; class Class1 { static void Main() { } public static OleDbDataAdapter CreateDataAdapter(string selectCommand, OleDbConnection connection) { OleDbDataAdapter adapter = new OleDbDataAdapter(selectCommand, connection); adapter.MissingSchemaAction = MissingSchemaAction.AddWithKey; // Create the Insert, Update and Delete commands. adapter.InsertCommand = new OleDbCommand( "INSERT INTO Customers (CustomerID, CompanyName) " + "VALUES (?, ?)"); adapter.UpdateCommand = new OleDbCommand( "UPDATE Customers SET CustomerID = ?, CompanyName = ? " + "WHERE CustomerID = ?"); adapter.DeleteCommand = new OleDbCommand( "DELETE FROM Customers WHERE CustomerID = ?"); // Create the parameters. adapter.InsertCommand.Parameters.Add("@CustomerID", OleDbType.Char, 5, "CustomerID"); adapter.InsertCommand.Parameters.Add("@CompanyName", OleDbType.VarChar, 40, "CompanyName"); adapter.UpdateCommand.Parameters.Add("@CustomerID", OleDbType.Char, 5, "CustomerID"); adapter.UpdateCommand.Parameters.Add("@CompanyName", OleDbType.VarChar, 40, "CompanyName"); adapter.UpdateCommand.Parameters.Add("@oldCustomerID", OleDbType.Char, 5, "CustomerID").SourceVersion = DataRowVersion.Original; adapter.DeleteCommand.Parameters.Add("@CustomerID", OleDbType.Char, 5, "CustomerID").SourceVersion = DataRowVersion.Original; return adapter; }
System.MarshalByRefObject
System.ComponentModel.Component
System.Data.Common.DataAdapter
System.Data.Common.DbDataAdapter
System.Data.OleDb.OleDbDataAdapter
プラットフォーム
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 によってサポートされていないバージョンがあります。サポートされているバージョンについては、「システム要件」を参照してください。
参照
- OleDbDataAdapter クラスのページへのリンク