OleDbCommand Class
Represents an SQL statement or stored procedure to execute against a data source.
For a list of all members of this type, see OleDbCommand Members.
System.Object
System.MarshalByRefObject
System.ComponentModel.Component
System.Data.OleDb.OleDbCommand
[Visual Basic] NotInheritable Public Class OleDbCommand Inherits Component Implements ICloneable, IDbCommand [C#] public sealed class OleDbCommand : Component, ICloneable, IDbCommand [C++] public __gc __sealed class OleDbCommand : public Component, ICloneable, IDbCommand [JScript] public class OleDbCommand extends Component implements ICloneable, IDbCommand
Thread Safety
Any public static (Shared in Visual Basic) members of this type are thread safe. Any instance members are not guaranteed to be thread safe.
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:
| Item | Description |
|---|---|
| ExecuteReader | Executes commands that return rows. ExecuteReader may not have the desired effect if used to execute commands such as SQL SET statements. |
| ExecuteNonQuery | Executes commands such as SQL INSERT, DELETE, UPDATE, and SET statements. |
| ExecuteScalar | 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.
When querying an Oracle database using the Microsoft OLE DB Provider for Oracle (MSDAORA) and the OLE DB .NET Data Provider, using the LIKE clause to query values in fixed-length fields may not return all expected matches. For example, if a table in an Oracle database contain a field named "Field1" that is defined as char(3), and you enter the value "a" into a row of that table, the following code will fail to return the row.
[Visual Basic] Dim sql As String = "SELECT * FROM Table1 WHERE Field1 LIKE ?" Dim cmd As OleDbCommand = New OleDbCommand(sql, oracleConn) cmd.Parameters.Add("@p1", OleDbType.Char, 3).Value = "a" Dim dr As OleDbDataReader = cmd.ExecuteReader() [C#] string sql = "SELECT * FROM Table1 WHERE Field1 LIKE ?"; OleDbCommand cmd = new OleDbCommand(sql, oracleConn); cmd.Parameters.Add("@p1", OleDbType.Char, 3).Value = "a"; OleDbDataReader dr = cmd.ExecuteReader();
Example
[Visual Basic, C#, C++] 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.
[Visual Basic] Public Sub ReadMyData(myConnString As String) Dim mySelectQuery As String = "SELECT OrderID, CustomerID FROM Orders" Dim myConnection As New OleDbConnection(myConnString) Dim myCommand As New OleDbCommand(mySelectQuery, myConnection) myConnection.Open() Dim myReader As OleDbDataReader = myCommand.ExecuteReader() Try While myReader.Read() Console.WriteLine(myReader.GetInt32(0).ToString() + ", " _ + myReader.GetString(1)) End While Finally ' always call Close when done reading. myReader.Close() ' always call Close when done reading. myConnection.Close() End Try End Sub [C#] public void ReadMyData(string myConnString) { string mySelectQuery = "SELECT OrderID, CustomerID FROM Orders"; OleDbConnection myConnection = new OleDbConnection(myConnString); OleDbCommand myCommand = new OleDbCommand(mySelectQuery,myConnection); myConnection.Open(); OleDbDataReader myReader = myCommand.ExecuteReader(); try { while (myReader.Read()) { Console.WriteLine(myReader.GetInt32(0) + ", " + myReader.GetString(1)); } } finally { // always call Close when done reading. myReader.Close(); // always call Close when done reading. myConnection.Close(); } } [C++] public: void ReadMyData(String* myConnString) { String* mySelectQuery = S"SELECT OrderID, CustomerID FROM Orders"; OleDbConnection* myConnection = new OleDbConnection(myConnString); OleDbCommand* myCommand = new OleDbCommand(mySelectQuery,myConnection); myConnection->Open(); OleDbDataReader* myReader = myCommand->ExecuteReader(); try { while (myReader->Read()) { Console::WriteLine(S"{0}, {1}", __box(myReader->GetInt32(0)), myReader->GetString(1)); } } __finally { // always call Close when done reading. myReader->Close(); // always call Close when done reading. myConnection->Close(); } }
[JScript] No example is available for JScript. To view a Visual Basic, C#, or C++ example, click the Language Filter button
in the upper-left corner of the page.
Requirements
Namespace: System.Data.OleDb
Platforms: Windows 98, Windows NT 4.0, Windows Millennium Edition, Windows 2000, Windows XP Home Edition, Windows XP Professional, Windows Server 2003 family
Assembly: System.Data (in System.Data.dll)
See Also
OleDbCommand Members | System.Data.OleDb Namespace | OleDbDataAdapter | OleDbConnection