IDbCommand.Prepare 方法

定義

在資料來源上建立命令已備妥 (或已編譯) 的版本。

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

例外狀況

Connection 未設定。

-或-

Connection 不是 Open()

範例

下列範例會建立衍生類別 的實例, 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

備註

CommandType如果 屬性設定為 TableDirectPrepare 則不會執行任何動作。 如果 CommandType 設定為 StoredProcedure ,則 呼叫 Prepare 應該會成功,但可能會導致無作業。 伺服器會視需要自動快取計畫以供重複使用;因此,您不需要直接在用戶端應用程式中呼叫此方法。

適用於