Export (0) Print
Expand All

IDbCommand.Prepare Method

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

[Visual Basic]
Sub Prepare()
[C#]
void Prepare();
[C++]
void Prepare();
[JScript]
function Prepare();

Exceptions

Exception Type Condition
InvalidOperationException The Connection is not set.

-or-

The Connection is not Open.

Remarks

If the CommandType property is set to TableDirect, Prepare does nothing. If CommandType is set to StoredProcedure, the call to Prepare should succeed, although it may result in a no-op.

Example

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

[Visual Basic] 
Public Sub OleDbCommandPrepare()
    Dim id As Integer = 20
    Dim desc As String = "myFirstRegion"
    Dim rConn As OleDbConnection = New OleDbConnection("Provider=SQLOLEDB;Data Source=localhost;Integrated Security=SSPI;Initial Catalog=Northwind;")
    rConn.Open()
    Dim command As OleDbCommand = New OleDbCommand("", rConn)

    ' Create and prepare an SQL statement.
    command.CommandText = "insert into Region (RegionID, RegionDescription) values (@id, @desc)"
    command.Parameters.Add("@id", id)
    command.Parameters.Add("@desc", desc)
    command.Prepare() ' Calling Prepare after having set the Commandtext and parameters.
    command.ExecuteNonQuery()

    ' Change parameter values and call ExecuteNonQuery.
    command.Parameters(0).Value = 21
    command.Parameters(1).Value = "mySecondRegion"
    command.ExecuteNonQuery()
End Sub

[C#] 
public void OleDbCommandPrepareEx() {
   int  id = 20;
   string  desc = "myFirstRegion" ;
   OleDbConnection rConn = new OleDbConnection("Provider=SQLOLEDB;Data Source=localhost;Integrated Security=SSPI;Initial Catalog=Northwind;");
   rConn.Open();
   OleDbCommand command    = new OleDbCommand(null, rConn);

   // Create and prepare an SQL statement.
   command.CommandText = "insert into Region (RegionID, RegionDescription) values (@id, @desc)" ;
   command.Parameters.Add ( "@id", id) ;
   command.Parameters.Add ( "@desc", desc) ;
   command.Prepare() ;  // Calling Prepare after having set the Commandtext and parameters.
   command.ExecuteNonQuery();

   // Change parameter values and call ExecuteNonQuery.
   command.Parameters[0].Value = 21;
   command.Parameters[1].Value = "mySecondRegion";
   command.ExecuteNonQuery();
}

[C++] 
public:
  void OleDbCommandPrepareEx() {
     int  id = 20;
     String*  desc = S"myFirstRegion" ;
     OleDbConnection* rConn = 
       new OleDbConnection(S"Provider=SQLOLEDB;Data Source=localhost;Integrated Security=SSPI;Initial Catalog=Northwind;");
     rConn->Open();
     OleDbCommand* command = new OleDbCommand(0, rConn);

     // Create and prepare an SQL statement.
     command->CommandText = S"insert into Region (RegionID, RegionDescription) values (@id, @desc)" ;
     command->Parameters->Add ( S"@id", __box(id)) ;
     command->Parameters->Add ( S"@desc", desc) ;
     command->Prepare() ;  // Calling Prepare after having set the Commandtext and parameters.
     command->ExecuteNonQuery();

     // Change parameter values and call ExecuteNonQuery.
     command->Parameters->Item[0]->Value = __box(21);
     command->Parameters->Item[1]->Value = S"mySecondRegion";
     command->ExecuteNonQuery();
  }

[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, .NET Compact Framework

See Also

IDbCommand Interface | IDbCommand Members | System.Data Namespace

Show:
© 2014 Microsoft