Sdílet prostřednictvím


SqlCommand.Prepare Metoda

Definice

Vytvoří připravenou verzi příkazu v instanci SQL Serveru.

public:
 override void Prepare();
public:
 virtual void Prepare();
public override void Prepare();
public void Prepare();
override this.Prepare : unit -> unit
abstract member Prepare : unit -> unit
override this.Prepare : unit -> unit
Public Overrides Sub Prepare ()
Public Sub Prepare ()

Implementuje

Příklady

Následující příklad ukazuje použití Prepare metody.

private static void SqlCommandPrepareEx(string connectionString)
{
    using (SqlConnection connection = new SqlConnection(connectionString))
    {
        connection.Open();
        SqlCommand command = new SqlCommand(null, connection);

        // Create and prepare an SQL statement.
        command.CommandText =
            "INSERT INTO Region (RegionID, RegionDescription) " +
            "VALUES (@id, @desc)";
        SqlParameter idParam = new SqlParameter("@id", SqlDbType.Int, 0);
        SqlParameter descParam =
            new SqlParameter("@desc", SqlDbType.Text, 100);
        idParam.Value = 20;
        descParam.Value = "First Region";
        command.Parameters.Add(idParam);
        command.Parameters.Add(descParam);

        // Call Prepare after setting the Commandtext and Parameters.
        command.Prepare();
        command.ExecuteNonQuery();

        // Change parameter values and call ExecuteNonQuery.
        command.Parameters[0].Value = 21;
        command.Parameters[1].Value = "Second Region";
        command.ExecuteNonQuery();
    }
}
Private Sub SqlCommandPrepareEx(ByVal connectionString As String)
    Using connection As New SqlConnection(connectionString)
        connection.Open()
        Dim command As New SqlCommand("", connection)

        ' Create and prepare an SQL statement.
        command.CommandText = _
           "INSERT INTO Region (RegionID, RegionDescription) " & _
           "VALUES (@id, @desc)"
        Dim idParam As SqlParameter = _
            New SqlParameter("@id", SqlDbType.Int, 0)
        Dim descParam As SqlParameter = _
            New SqlParameter("@desc", SqlDbType.Text, 100)
        idParam.Value = 20
        descParam.Value = "First Region"
        command.Parameters.Add(idParam)
        command.Parameters.Add(descParam)

        ' Call Prepare after setting the Commandtext and Parameters.
        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

Poznámky

Pokud CommandType je nastavena na StoredProcedure, volání Prepare by mělo být úspěšné, i když může způsobit no-op.

Před voláním Preparezadejte datový typ každého parametru v příkazu, který se má připravit. U každého parametru, který má datový typ s proměnlivou délkou, je nutné nastavit Size vlastnost na maximální požadovanou velikost. Prepare vrátí chybu, pokud tyto podmínky nejsou splněny.

Poznámka:

Pokud je kontext databáze změněn spuštěním příkazu Transact-SQL USE <database> nebo voláním ChangeDatabase metody, Prepare musí být volána podruhé.

Pokud voláte metodu Execute po volání Prepare, jakákoli hodnota parametru, která je větší než hodnota zadaná Size vlastností, se automaticky zkrátí na původní zadanou velikost parametru a nebudou vráceny žádné chyby zkrácení.

Výstupní parametry (ať už připravené nebo ne) musí mít datový typ zadaný uživatelem. Pokud zadáte datový typ s proměnnou délkou, musíte také zadat maximální Sizehodnotu .

Před sadou Visual Studio 2010 Prepare došlo k výjimce. Počínaje sadou Visual Studio 2010 tato metoda nevyvolá výjimku.

Platí pro

Viz také