.NET Framework Class Library
OleDbCommand Class

Represents an SQL statement or stored procedure to execute against a data source.

Namespace: System.Data.OleDb
Assembly: System.Data (in system.data.dll)

Syntax

Visual Basic (Declaration)
Public NotInheritable Class OleDbCommand
    Inherits DbCommand
    Implements ICloneable, IDbCommand, IDisposable
Visual Basic (Usage)
Dim instance As OleDbCommand
C#
public sealed class OleDbCommand : DbCommand, ICloneable, IDbCommand, IDisposable
C++
public ref class OleDbCommand sealed : public DbCommand, ICloneable, IDbCommand, IDisposable
J#
public final class OleDbCommand extends DbCommand implements ICloneable, IDbCommand, 
    IDisposable
JScript
public final class OleDbCommand extends DbCommand implements ICloneable, IDbCommand, 
    IDisposable
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 effect that you want 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.

Example

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(ByVal connectionString As String)
    Dim queryString As String = "SELECT OrderID, CustomerID FROM Orders"
    Using connection As New OleDbConnection(connectionString)
        Dim command As New OleDbCommand(queryString, connection)

        connection.Open()

        Dim reader As OleDbDataReader = command.ExecuteReader()
        While reader.Read()
            Console.WriteLine(reader.GetInt32(0).ToString() + ", " _
               + reader.GetString(1))
        End While

        ' always call Close when done reading.
        reader.Close()
    End Using
End Sub
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();
    
C#
using System;
using System.Data;
using System.Data.OleDb;

class Class1
{
    static void Main()
    {
    

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

System.Object
   System.MarshalByRefObject
     System.ComponentModel.Component
       System.Data.Common.DbCommand
        System.Data.OleDb.OleDbCommand
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.
Platforms

Windows 98, Windows 2000 SP4, Windows Millennium Edition, Windows Server 2003, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP SP2, Windows XP Starter Edition

The .NET Framework does not support all versions of every platform. For a list of the supported versions, see System Requirements.

Version Information

.NET Framework

Supported in: 2.0, 1.1, 1.0
See Also

Tags :


Page view tracker