ADO.NET Provider for Microsoft Excel

Build 24.0.9060

Querying with the DataAdapter

The CData ADO.NET Provider for Microsoft Excel implements two ADO.NET interfaces you can use to retrieve data from Microsoft Excel: ExcelDataAdapter and ExcelDataReader objects. Whereas ExcelDataAdapter objects retrieve a single result set of all the data that matches a query, ExcelDataReader objects fetch data in subset increments as needed.

Using the ExcelDataAdapter

Use the adapter's Fill method to retrieve data from the data source. An empty DataTable instance is passed as an argument to the Fill method. When the method returns, the DataTable instance is populated with the queried data. Note that the ExcelDataAdapter is slower than the ExcelDataReader because the Fill method needs to retrieve all data from the data source before returning.

The following example selects the RowId and LastName columns of the Sheet table:

C#

string connectionString = "URI=C:\MyExcelWorkbooks\SampleWorkbook.xlsx;";

using (ExcelConnection connection = new ExcelConnection(connectionString)) {
  ExcelDataAdapter dataAdapter = new ExcelDataAdapter(
  "SELECT RowId, LastName FROM Sheet", connection);

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

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

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

VB.NET

Dim connectionString As String = "URI=C:\MyExcelWorkbooks\SampleWorkbook.xlsx;"

Using connection As New ExcelConnection(connectionString)
  Dim dataAdapter As New ExcelDataAdapter("SELECT RowId, LastName FROM Sheet", connection)

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

  Console.WriteLine("Contents of Sheet.")

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

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