This documentation is archived and is not being maintained.

OdbcCommand Class

Note: This namespace, class, or member is supported only in version 1.1 of the .NET Framework.

Represents an SQL statement or stored procedure to execute against a data source. This class cannot be inherited.

For a list of all members of this type, see OdbcCommand Members.


[Visual Basic]
NotInheritable Public Class OdbcCommand
   Inherits Component
   Implements ICloneable, IDbCommand
public sealed class OdbcCommand : Component, ICloneable, IDbCommand
public __gc __sealed class OdbcCommand : public Component,
   ICloneable, IDbCommand
public class OdbcCommand 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.


The OdbcCommand class provides the following methods for executing commands against a data source:

Item Description
ExecuteReader Executes commands that return rows.
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 OdbcCommand object. However, you must close the OdbcDataReader before you can execute a new or previous command.

If execution of the command results in a fatal OdbcException (for example, a SQL Server severity level of 20 or greater), the OdbcConnection may close. However, the user can reopen the connection and continue.


[Visual Basic, C#, C++] The following example uses the ExecuteReader method of OdbcCommand, along with OdbcDataReader and OdbcConnection, to select rows from a table.

[Visual Basic] 
Public Sub ReadMyData(myConnString As String)
    Dim mySelectQuery As String = "SELECT OrderID, CustomerID FROM Orders"
    Dim myConnection As New OdbcConnection(myConnString)
    Dim myCommand As New OdbcCommand(mySelectQuery, myConnection)
    Dim myReader As OdbcDataReader = myCommand.ExecuteReader()
        While myReader.Read()
            Console.WriteLine(myReader.GetInt32(0).ToString() + ", " _
               + myReader.GetString(1))
        End While
        ' always call Close when done with connection.
        ' always call Close when done with connection.
    End Try
End Sub

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

    void ReadMyData(String* myConnString)
        String* mySelectQuery = S"SELECT OrderID, CustomerID FROM Orders";
        OdbcConnection* myConnection = new OdbcConnection(myConnString);
        OdbcCommand* myCommand = new OdbcCommand(mySelectQuery, myConnection);
        OdbcDataReader* myReader = myCommand->ExecuteReader();

            while (myReader->Read())
                Console::WriteLine("{0}, {1}", __box(myReader->GetInt32(0)), myReader->GetString(1));
        catch(Exception* e)
            Console::WriteLine("An error occurred: '{0}'", e);

        // always call Close when done with connection.
        // always call Close when done with connection.


[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.Odbc

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

OdbcCommand Members | System.Data.Odbc Namespace | OdbcDataAdapter | OdbcConnection