Partager via


IDbCommand.Prepare Méthode

Définition

Crée une version préparée (ou compilée) de la commande sur la source de données.

public:
 void Prepare();
public void Prepare();
abstract member Prepare : unit -> unit
Public Sub Prepare ()

Exceptions

Ce Connection n’est pas défini.

- ou -

Ce Connection n’est pas Open()le cas.

Exemples

L’exemple suivant crée une instance de la classe dérivée, OleDbCommandpuis ouvre la connexion. L’exemple prépare ensuite une procédure stockée sur la source de données en passant une chaîne qui est une instruction SQL Select et une chaîne à utiliser pour se connecter à la source de données.

private static void OleDbCommandPrepare(string connectionString)
{
    using (OleDbConnection connection = new
               OleDbConnection(connectionString))
    {
        connection.Open();

        // Create the Command.
        OleDbCommand command = new OleDbCommand();

        // Set the Connection, CommandText and Parameters.
        command.Connection = connection;
        command.CommandText =
            "INSERT INTO dbo.Region (RegionID, RegionDescription) VALUES (?, ?)";
        command.Parameters.Add("RegionID", OleDbType.Integer, 4);
        command.Parameters.Add("RegionDescription", OleDbType.VarWChar, 50);
        command.Parameters[0].Value = 20;
        command.Parameters[1].Value = "First Region";

        // Call  Prepare and ExecuteNonQuery.
        command.Prepare();
        command.ExecuteNonQuery();

        // Change parameter values and call ExecuteNonQuery.
        command.Parameters[0].Value = 21;
        command.Parameters[1].Value = "SecondRegion";
        command.ExecuteNonQuery();
    }
}
Public Sub OleDbCommandPrepare(ByVal connectionString As String)

    Using connection As OleDbConnection = New _
        OleDbConnection(connectionString)
        connection.Open()

        ' Create the Command.
        Dim command As New OleDbCommand()

        ' Set the Connection, CommandText and Parameters.
        command.Connection = connection
        command.CommandText = _
          "INSERT INTO dbo.Region (RegionID, RegionDescription) VALUES (?, ?);"
        command.Parameters.Add("RegionID", OleDbType.Integer, 4)
        command.Parameters.Add("RegionDescription", OleDbType.VarWChar, 50)
        command.Parameters(0).Value = 20
        command.Parameters(1).Value = "First Region"

        ' Call  Prepare and ExecuteNonQuery.
        command.Prepare()
        command.ExecuteNonQuery()

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

Remarques

Si la CommandType propriété est définie TableDirectsur , Prepare ne fait rien. Si CommandType la valeur est définie StoredProcedure, l’appel doit Prepare réussir, bien qu’il puisse entraîner une no-op. Le serveur met automatiquement en cache les plans de réutilisation si nécessaire ; par conséquent, il n’est pas nécessaire d’appeler cette méthode directement dans votre application cliente.

S’applique à