SqlCommand.Prepare メソッド

定義

SQL Server のインスタンスに対する準備済みのコマンドを作成します。

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 ()

実装

Prepare メソッドの使用例を次に示します。

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

注釈

StoredProcedure設定されている場合CommandType、呼び出しは成功しますPrepareが、操作が失敗する可能性があります。

呼び出す Prepare前に、準備するステートメント内の各パラメーターのデータ型を指定します。 可変長データ型を持つパラメーターごとに、プロパティを Size 必要な最大サイズに設定する必要があります。 Prepare は、これらの条件が満たされない場合にエラーを返します。

注意

Transact-SQL USE <database> ステートメントを実行するか、メソッドを呼び出してデータベース コンテキストをChangeDatabase変更する場合は、Prepareもう一度呼び出す必要があります。

呼び出し後にメソッドを Execute 呼び Prepare出すと、プロパティで Size 指定された値より大きいパラメーター値は、パラメーターの指定された元のサイズに自動的に切り捨てられ、切り捨てエラーは返されません。

出力パラメーター (準備済みかどうかに関係なく) には、ユーザー指定のデータ型が必要です。 可変長データ型を指定する場合は、最大値 Sizeも指定する必要があります。

2010 年Visual Studioより前に、Prepare例外をスローしました。 Visual Studio 2010 以降、このメソッドは例外をスローしません。

適用対象

こちらもご覧ください