SqlCeCommand.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 on 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

If the CommandType property is set to TableDirect, Prepare does nothing.

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.

Example

  [Visual Basic] 
Public Sub SqlCeCommandPrepare()
    Dim id   As Integer = 20
    Dim desc As String  = "myFirstRegion"

    Dim conn As SqlCeConnection = New SqlCeConnection("Data Source = Northwind.sdf;")
    conn.Open()

    Dim command As SqlCeCommand = conn.CreateCommand()

    ' Create and prepare a SQL statement.
    command.CommandText = "INSERT INTO Region (RegionID, RegionDescription) VALUES (?, ?)"

    ' Note: Even though named parameterized queries are not supported, we still need
    '       to provide unique parameter names so that we can manipulate parameter collection;
    command.Parameters.Add("@id", id)
    command.Parameters.Add("@desc", desc)

    ' Calling Prepare after having set the Commandtext and parameters.
    command.Prepare()
    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 SqlCeCommandPrepareEx() {
    int    id   = 20;
    string desc = "myFirstRegion" ;
 
    SqlCeConnection conn = new SqlCeConnection("Data Source = Northwind.sdf;");
    conn.Open();
    SqlCeCommand command = conn.CreateCommand();

    // Create and prepare a SQL statement.
    command.CommandText = "INSERT INTO Region (RegionID, RegionDescription) VALUES (?, ?)";
    
    // Note: Even though named parameterized queries are not supported, we still need
    //       to provide unique parameter names so that we can manipulate parameter collection;
    command.Parameters.Add( "@id",   id) ;
    command.Parameters.Add( "@desc", desc) ;

    // Calling Prepare after having set the Commandtext and parameters.
    command.Prepare() ;  
    command.ExecuteNonQuery();

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

[C++, JScript] No example is available for C++ or JScript. To view a Visual Basic or C# example, click the Language Filter button Language Filter in the upper-left corner of the page.

Requirements

Platforms: .NET Compact Framework

.NET Framework Security:

See Also

SqlCeCommand Class | SqlCeCommand Members | System.Data.SqlServerCe Namespace

Syntax based on .NET Framework version 1.1.
Documentation version 1.1.1.

Send comments on this topic.

© Microsoft Corporation. All rights reserved.