Was this page helpful?
Your feedback about this content is important. Let us know what you think.
Additional feedback?
1500 characters remaining
Export (0) Print
Expand All

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.

System.Object
   System.MarshalByRefObject
      System.ComponentModel.Component
         System.Data.Odbc.OdbcCommand

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

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.

Example

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

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

[C++] 
public:
    void ReadMyData(String* myConnString)
    {
        String* mySelectQuery = S"SELECT OrderID, CustomerID FROM Orders";
        OdbcConnection* myConnection = new OdbcConnection(myConnString);
        OdbcCommand* myCommand = new OdbcCommand(mySelectQuery, myConnection);
        myConnection->Open();
        OdbcDataReader* myReader = myCommand->ExecuteReader();

        try
        {
            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.
        myReader->Close();
        // always call Close when done with connection.
        myConnection->Close();

    };

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

Requirements

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

Show:
© 2015 Microsoft