IDbCommand.Prepare メソッド

定義

コマンドの準備済み (またはコンパイル済み) のバージョンをデータ ソースに作成します。

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

例外

Connection が設定されていません。

または

ConnectionOpen() ではありません。

次の例では、派生クラス のインスタンスを作成し、 OleDbCommand接続を開きます。 次に、SQL Select ステートメントである文字列と、データ ソースへの接続に使用する文字列を渡して、データ ソースにストアド プロシージャを準備します。

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

注釈

プロパティが CommandTypeTableDirectPrepare設定されている場合、何も行われません。 が にStoredProcedure設定されている場合CommandType、 のPrepare呼び出しは成功するはずですが、操作が失敗する可能性があります。 サーバーは、必要に応じて再利用のためにプランを自動的にキャッシュします。そのため、クライアント アプリケーションでこのメソッドを直接呼び出す必要はありません。

適用対象