ADO.NET Provider for Microsoft Excel

Build 23.0.8839

Result Sets

You can access the same column information about the results of a query that you can for table schemas. See Columns for the columns returned.

Retrieving Result Set Metadata

You can use the GetSchemaTable method of the ExcelDataReader to retrieve result set metadata. Call GetSchemaTable after calling ExecuteReader.

Each row of the DataTable describes a column in the query's result.

C#

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

using (ExcelConnection conn = new ExcelConnection(connectionString)) {
  ExcelCommand cmd = new ExcelCommand("SELECT * FROM Sheet WHERE FirstName <> 'Bob'", conn);
  ExcelDataReader rdr = cmd.ExecuteReader();
  DataTable schemaTable = rdr.GetSchemaTable();
  foreach (DataRow row in schemaTable.Rows) {
    foreach (DataColumn col in schemaTable.Columns) {
      Console.WriteLine("{0}: {1}", col.ColumnName, row[col]);
    }
  }
}

VB.NET

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

Using conn As New ExcelConnection(connectionString)
  Dim cmd As New ExcelCommand("SELECT * FROM Sheet WHERE FirstName <> 'Bob'", conn)
  Dim rdr As ExcelDataReader = cmd.ExecuteReader()
  Dim schemaTable As DataTable = rdr.GetSchemaTable()
  For Each row As DataRow In schemaTable.Rows
    For Each col As DataColumn In schemaTable.Columns
      Console.WriteLine("{0}: {1}", col.ColumnName, row(col))
    Next
  Next
End Using

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