Executing DbCommands
The following section shows how to execute commands using ADO.NET base classes like DbCommand and DbDataAdapter.
Executing Commands to Microsoft Excel
The following code executes a "SELECT *" query to Microsoft Excel, given an existing DbConnection object.
using (connection) {
// Create the DbCommand.
DbCommand command = factory.CreateCommand();
command.CommandText =
"SELECT * FROM Sheet";
command.Connection = connection;
// Create the DbDataAdapter.
DbDataAdapter adapter = factory.CreateDataAdapter();
adapter.SelectCommand = command;
// Fill the DataTable.
DataTable table = new DataTable();
adapter.Fill(table);
// Display each row and column value.
foreach (DataRow row in table.Rows) {
foreach (DataColumn column in table.Columns) {
Console.WriteLine(row[column]);
}
}
}