Share via


SqlCeCommand.Prepare 方法

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

命名空間:  System.Data.SqlServerCe
組件:  System.Data.SqlServerCe (在 System.Data.SqlServerCe.dll 中)

語法

'宣告
Public Overrides Sub Prepare
'用途
Dim instance As SqlCeCommand

instance.Prepare()
public override void Prepare()
public:
virtual void Prepare() override
abstract Prepare : unit -> unit 
override Prepare : unit -> unit 
public override function Prepare()

實作

IDbCommand.Prepare()

例外狀況

例外狀況 條件
InvalidOperationException

Connection 尚未設定。

-或-

Connection 並非 Open

備註

如果 CommandType 屬性設定為 TableDirect,Prepare 不會執行任何作業。

在您呼叫 Prepare 之前,請在陳述式中指定每個參數的資料類型以做準備。針對每一個有可變長度資料類型的參數,您必須將 Size 屬性設定為所需要的最大值。如果條件不符合,則 Prepare 會傳回錯誤。

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

範例

Dim conn As New SqlCeConnection("Data Source = MyDatabase.sdf;")
conn.Open()

Dim command As SqlCeCommand = conn.CreateCommand()

' Create and prepare a SQL statement
'
command.CommandText = "INSERT INTO Region (RegionID, RegionDescription) VALUES (@id, @desc)"

Dim param As SqlCeParameter = Nothing

' NOTE:
' For optimal performance, make sure you always set the parameter
' type and the maximum size - this is especially important for non-fixed
' types such as NVARCHAR or NTEXT; In case of named parameters, 
' SqlCeParameter instances do not need to be added to the collection
' in the order specified in the query; If however you use ? as parameter
' specifiers, then you do need to add the parameters in the correct order
'
param = New SqlCeParameter("@id", SqlDbType.Int)
command.Parameters.Add(param)

param = New SqlCeParameter("@desc", SqlDbType.NVarChar, 100)
command.Parameters.Add(param)

command.Parameters("@desc").Size = 100

' Calling Prepare after having set the CommandText and parameters
'
command.Prepare()

' Execute the SQL statement
'
command.ExecuteNonQuery()

' Change parameter values and call ExecuteNonQuery again
'
command.Parameters(0).Value = 21
command.Parameters(1).Value = "mySecondRegion"
command.ExecuteNonQuery()
SqlCeConnection conn = new SqlCeConnection("Data Source = MyDatabase.sdf;");
conn.Open();

SqlCeCommand command = conn.CreateCommand();

// Create and prepare a SQL statement
//
command.CommandText = "INSERT INTO Region (RegionID, RegionDescription) VALUES (@id, @desc)";

SqlCeParameter param = null;

// NOTE:
// For optimal performance, make sure you always set the parameter
// type and the maximum size - this is especially important for non-fixed
// types such as NVARCHAR or NTEXT; In case of named parameters, 
// SqlCeParameter instances do not need to be added to the collection
// in the order specified in the query; If however you use ? as parameter
// specifiers, then you do need to add the parameters in the correct order
//
param = new SqlCeParameter("@id", SqlDbType.Int);
command.Parameters.Add(param);

param = new SqlCeParameter("@desc", SqlDbType.NVarChar, 100);
command.Parameters.Add(param);

command.Parameters["@desc"].Size = 100;

// Calling Prepare after having set the CommandText and parameters
//
command.Prepare();

// Execute the SQL statement
//
command.ExecuteNonQuery();

// Change parameter values and call ExecuteNonQuery again
//
command.Parameters[0].Value = 21;
command.Parameters[1].Value = "mySecondRegion";
command.ExecuteNonQuery();

請參閱

參考

SqlCeCommand 類別

System.Data.SqlServerCe 命名空間