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

備註

如果 CommandType 設定為 StoredProcedure,則呼叫 Prepare 應該會成功,不過它可能會導致無作業。

呼叫 Prepare之前,請在語句中指定要備妥之每個參數的數據類型。 對於具有可變長度數據類型的每個參數,您必須將 Size 屬性設定為所需的大小上限。 Prepare 如果不符合這些條件,則傳回錯誤。

注意

如果執行 Transact-SQL USE <database> 語句或呼叫 ChangeDatabase 方法來變更資料庫內容, Prepare 則必須再次呼叫 。

如果您在呼叫 Prepare之後呼叫 Execute 方法,任何大於 屬性所指定Size值的參數值都會自動截斷為參數的原始指定大小,而且不會傳回任何截斷錯誤。

輸出參數 (是否已備妥) 必須具有使用者指定的數據類型。 如果您指定可變長度資料類型,您也必須指定最大值 Size

在 Visual Studio 2010 之前, Prepare 擲回例外狀況。 從 Visual Studio 2010 開始,此方法不會擲回例外狀況。

適用於

另請參閱