DataAdapter を使用したクエリ
CData ADO.NET Provider for Google Search では、次の2つのネイティブ.NET インターフェースを使用して、Google Search からデータを取得できます。GoogleSearchDataAdapter オブジェクトおよびGoogleSearchDataReader オブジェクト。各オブジェクトは同じタスク(データの取得)を実行しますが、実行方法が異なります。GoogleSearchDataAdapter オブジェクトはクエリに一致するすべてのデータを取得しますが、GoogleSearchDataReader オブジェクトは必要に応じてインクリメントしながら一部のデータだけをフェッチします。
GoogleSearchDataAdapter の使用
アダプターのFill メソッドを使用して、データソースからデータを取得します。空のDataTable インスタンスがFill メソッドへの引数として渡されます。このメソッドが戻ってきたとき、DataTable インスタンスにはクエリされたデータが設定されています。Fill メソッドは、戻る前にデータソースからすべてのデータを取得する必要があるため、GoogleSearchDataAdapter はGoogleSearchDataReader よりも時間がかかります。
次の例は、WebSearch テーブルのDisplayLink カラムとTitle カラムを選択します。
C#
string connectionString = "ApiKey=abc123; CustomSearchID=def456;"; using (GoogleSearchConnection connection = new GoogleSearchConnection(connectionString)) { GoogleSearchDataAdapter dataAdapter = new GoogleSearchDataAdapter( "SELECT DisplayLink, Title FROM WebSearch WHERE SearchTerms = 'Fantastic Four'", connection); DataTable table = new DataTable(); dataAdapter.Fill(table); Console.WriteLine("Contents of WebSearch."); foreach (DataRow row in table.Rows) { Console.WriteLine("{0}: {1}", row["DisplayLink"], row["Title"]); } }
VB.NET
Dim connectionString As String = "ApiKey=abc123; CustomSearchID=def456;" Using connection As New GoogleSearchConnection(connectionString) Dim dataAdapter As New GoogleSearchDataAdapter("SELECT DisplayLink, Title FROM WebSearch WHERE SearchTerms = 'Fantastic Four'", connection) Dim table As New DataTable() dataAdapter.Fill(table) Console.WriteLine("Contents of WebSearch.") For Each row As DataRow In table.Rows Console.WriteLine("{0}: {1}", row("DisplayLink"), row("Title")) Next End Using