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
[C#]
public virtual void Prepare();
[C++]
public: virtual void Prepare();
[JScript]
public function Prepare();

Implements

IDbCommand.Prepare

Exceptions

Exception Type Condition
InvalidOperationException The Connection is not set.

-or-

The Connection is not Open.

Remarks

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.

Example

[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)
    myCommand.Connection.Open()
    myCommand.Prepare()
End Sub

[C#] 
public void CreateMyOdbcCommand(string mySelectQuery, string myConnectionString)
{
   OdbcConnection myConnection = new OdbcConnection(myConnectionString);
   OdbcCommand myCommand = new OdbcCommand(mySelectQuery, myConnection);
   myCommand.Connection.Open();
   myCommand.Prepare();
}

[C++] 
public:
    void CreateMyOdbcCommand(String* mySelectQuery, String* myConnectionString)
    {
        OdbcConnection* myConnection = new OdbcConnection(myConnectionString);
        OdbcCommand* myCommand = new OdbcCommand(mySelectQuery, myConnection);
        myCommand->Connection->Open();
        myCommand->Prepare();
    };

[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

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

Show: