This documentation is archived and is not being maintained.

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.


[Visual Basic]
NotInheritable Public Class OleDbCommand
   Inherits Component
   Implements ICloneable, IDbCommand
public sealed class OleDbCommand : Component, ICloneable,
public __gc __sealed class OleDbCommand : public Component,
   ICloneable, IDbCommand
public class OleDbCommand extends Component implements ICloneable,

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.


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()
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();


[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)
    Dim myReader As OleDbDataReader = myCommand.ExecuteReader()
        While myReader.Read()
            Console.WriteLine(myReader.GetInt32(0).ToString() + ", " _
               + myReader.GetString(1))
        End While
        ' always call Close when done reading.
        ' always call Close when done reading.
    End Try
End Sub

public void ReadMyData(string myConnString) 
   string mySelectQuery = "SELECT OrderID, CustomerID FROM Orders";
   OleDbConnection myConnection = new OleDbConnection(myConnString);
   OleDbCommand myCommand = new OleDbCommand(mySelectQuery,myConnection);
   OleDbDataReader myReader = myCommand.ExecuteReader();
     while (myReader.Read()) 
       Console.WriteLine(myReader.GetInt32(0) + ", " + myReader.GetString(1));
    // always call Close when done reading.
    // always call Close when done reading.

void ReadMyData(String* myConnString) 
   String* mySelectQuery = S"SELECT OrderID, CustomerID FROM Orders";
   OleDbConnection* myConnection = new OleDbConnection(myConnString);
   OleDbCommand* myCommand = new OleDbCommand(mySelectQuery,myConnection);
   OleDbDataReader* myReader = myCommand->ExecuteReader();
     while (myReader->Read()) 
       Console::WriteLine(S"{0}, {1}", __box(myReader->GetInt32(0)), myReader->GetString(1));
    // always call Close when done reading.
    // always call Close when done reading.

[JScript] No example is available for JScript. To view a Visual Basic, C#, or C++ example, click the Language Filter button Language Filter in the upper-left corner of the page.


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