System.Data.OleDB OleDbCommand Class Definition In this article Examples Remarks Constructors Properties Methods Explicit Interface Implementations Events Applies to .NET Core .NET Framework .NET Platform Extensions Xamarin.Mac See also Examples Source/Reference
System.Data.OleDB
The System.Data.OleDb namespace is the.NET Framework Data Provider for OLE DB.
The following example uses the OleDbCommand, along OleDbDataAdapter and OleDbConnection, to select rows from an Access database. The filled DataSet is then returned. The example is passed an initialized DataSet, a connection string, a query
string that is an SQL SELECT statement, and a string that is the name of the source database table.
C#
public void ReadMyData(string connectionString)
{
string queryString = "SELECT OrderID, CustomerID FROM Orders";
using (OleDbConnection connection = new OleDbConnection(connectionString))
{
OleDbCommand command = new OleDbCommand(queryString, connection);
connection.Open();
OleDbDataReader reader = command.ExecuteReader();
while (reader.Read())
{
Console.WriteLine(reader.GetInt32(0) + ", " + reader.GetString(1));
}
// always call Close when done reading.
reader.Close();
}
}
Remarks
When an instance of OleDbCommand is created, the read/write properties are set to their initial values. For a list of these values, see the OleDbCommand constructor.
OleDbCommand features the following methods executing commands at a data source:
Retrieves a single value, for example, an aggregate value from a database.
You can reset the CommandText property and reuse the OleDbCommand object. However, you must close the OleDbDataReader before you can execute a new or previous command.
If a fatal OleDbException (for example, a SQL Server severity level of 20 or greater) is generated by the method executing an OleDbCommand, the OleDbConnection, the connection may be closed. However, the user can reopen the connection and continue.