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 ( );
IDbCommand.Prepare
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.
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 ( );
}
Public Sub PrepareDemo ( )
Dim id As Integer = 20
Dim desc As String = "myFirstRegion"
Dim rConn As OleDbConnection = New OleDbConnection ( _
"Provider=SQLOLEDB; server=localhost; uid=sa; pwd=; database=northwind" )
rConn.Open ( )
Dim command As OleDbCommand = New OleDbCommand ( "", 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 ( )
End Sub |
|
C# |
VB |
OleDbCommand Members