asp.net.ph

OleDbCommand.Prepare Method

System.Data.OleDb Namespace   OleDbCommand Class


Initializes a prepared version of the command on the data source.

[ VB ]
NotOverridable Public Sub Prepare ( )

[ C# ]
public void Prepare ( );

[ C++ ]
public: __sealed void Prepare ( );

[ JScript ]
public function Prepare ( );

Implements

IDbCommand.Prepare

Exceptions


Exception Type Condition
InvalidOperationException Occurs when the Connection is not set or 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.

Before you call Prepare, specify the data type of each parameter in the statement to be prepared. For each parameter that has a variable length data type, you must set the Size property to the maximum size needed. Prepare returns an error if these conditions are not met.

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

The following example demonstrates the use of the Prepare method.

void PrepareDemo ( ) {
   int  id = 20;
   string  desc = "myFirstRegion" ;
   OleDbConnection rConn = new OleDbConnection (
      "Provider=SQLOLEDB; server=localhost; uid=sa; pwd=; database=northwind" );
   rConn.Open ( );
   OleDbCommand command = new OleDbCommand ( null, rConn );

   // set up insert command ...
   command.CommandText = "insert into Region ( RegionID, RegionDescription )
      values ( @id, @desc ) " ;
   command.Parameters.Add ( "@id", id );
   command.Parameters.Add ( "@desc", desc );

   // calling Prepare after having setup commandtext and params.
   command.Prepare ( );  
   command.ExecuteNonQuery ( );

   // change param values and call execute. This time the prepared command will be executed.
   command.Parameters [ 0 ].Value = 21;
   command.Parameters [ 1 ].Value = "mySecondRegion";
   command.ExecuteNonQuery ( );
}
  C# VB

See Also

OleDbCommand Members Skip Navigation Links




Home
Suggested Reading


Previous page Back to top Next page

© 2000-2010 Rey Nuñez All rights reserved.

If you have any question, comment or suggestion
about this site, please send us a note

You can help support asp.net.ph