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()
实现
异常
异常 | 条件 |
---|---|
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();