This documentation is archived and is not being maintained.

OdbcCommand.Prepare Method

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

Creates a prepared (or compiled) version of the command at the data source.

[Visual Basic]
Public Overridable Sub Prepare() Implements IDbCommand.Prepare
public virtual void Prepare();
public: virtual void Prepare();
public function Prepare();




Exception Type Condition
InvalidOperationException The Connection is not set.


The Connection is not Open.


The Prepare method calls the ODBC SQLPrepare function. Depending on the capabilities of the underlying ODBC driver and data source, parameter information (such as data types) may be checked when the statement is prepared (if all parameters have been bound), or when it is executed (if not all parameters have been bound). For maximum interoperability, an application should unbind all parameters that applied to a previous SQL statement before preparing a new SQL statement on the same OdbcCommand. This prevents errors that are due to previous parameter information being applied to the new SQL statement.

If you call an Execute method after calling Prepare, any parameter value that is larger than the value specified by the Size property is automatically truncated to the original specified size of the parameter, and no truncation errors are returned.

Output parameters (whether prepared or not) must have a user-specified data type. If you specify a variable length data type, you must also specify the maximum Size.


[Visual Basic, C#, C++] The following example creates an OdbcCommand and opens the connection. The example then prepares a stored procedure on the data source by passing a string that is an SQL SELECT statement and a string to use to connect to the data source.

[Visual Basic] 
Public Sub CreateMyOdbcCommand(mySelectQuery As String, _
myConnectionString As String)
    Dim myConnection As New OdbcConnection(myConnectionString)
    Dim myCommand As New OdbcCommand(mySelectQuery, myConnection)
End Sub

public void CreateMyOdbcCommand(string mySelectQuery, string myConnectionString)
   OdbcConnection myConnection = new OdbcConnection(myConnectionString);
   OdbcCommand myCommand = new OdbcCommand(mySelectQuery, myConnection);

    void CreateMyOdbcCommand(String* mySelectQuery, String* myConnectionString)
        OdbcConnection* myConnection = new OdbcConnection(myConnectionString);
        OdbcCommand* myCommand = new OdbcCommand(mySelectQuery, myConnection);

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


Platforms: Windows 98, Windows NT 4.0, Windows Millennium Edition, Windows 2000, Windows XP Home Edition, Windows XP Professional, Windows Server 2003 family

See Also

OdbcCommand Class | OdbcCommand Members | System.Data.Odbc Namespace