ADO.NET Provider for DB2

Build 23.0.8839

DataAdapter を使用したクエリ

CData ADO.NET Provider for DB2 では、次の2つのネイティブ.NET インターフェースを使用して、DB2 からデータを取得できます。DB2DataAdapter オブジェクトおよびDB2DataReader オブジェクト。各オブジェクトは同じタスク(データの取得)を実行しますが、実行方法が異なります。DB2DataAdapter オブジェクトはクエリに一致するすべてのデータを取得しますが、DB2DataReader オブジェクトは必要に応じてインクリメントしながら一部のデータだけをフェッチします。

DB2DataAdapter の使用

アダプターのFill メソッドを使用して、データソースからデータを取得します。空のDataTable インスタンスがFill メソッドへの引数として渡されます。このメソッドが戻ってきたとき、DataTable インスタンスにはクエリされたデータが設定されています。Fill メソッドは、戻る前にデータソースからすべてのデータを取得する必要があるため、DB2DataAdapter はDB2DataReader よりも時間がかかります。

次の例は、Books テーブルのId カラムとAuthor カラムを選択します。

C#

string connectionString = "Server=10.0.1.2;Port=50000;User=admin;Password=admin;Database=test";

using (DB2Connection connection = new DB2Connection(connectionString)) {
  DB2DataAdapter dataAdapter = new DB2DataAdapter(
  "SELECT Id, Author FROM \"Sample\".\"DB2INST1\".Books", connection);

  DataTable table = new DataTable();
  dataAdapter.Fill(table);

  Console.WriteLine("Contents of Books.");

  foreach (DataRow row in table.Rows) {
    Console.WriteLine("{0}: {1}", row["Id"], row["Author"]);
  }
}

VB.NET

Dim connectionString As String = "Server=10.0.1.2;Port=50000;User=admin;Password=admin;Database=test"

Using connection As New DB2Connection(connectionString)
  Dim dataAdapter As New DB2DataAdapter("SELECT Id, Author FROM \"Sample\".\"DB2INST1\".Books", connection)

  Dim table As New DataTable()
  dataAdapter.Fill(table)

  Console.WriteLine("Contents of Books.")

  For Each row As DataRow In table.Rows
    Console.WriteLine("{0}: {1}", row("Id"), row("Author"))
  Next
End Using

Copyright (c) 2024 CData Software, Inc. - All rights reserved.
Build 23.0.8839