次の方法で共有


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 が設定されていません。

または

ConnectionOpen ではありません。

説明

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 名前空間