次の方法で共有


SqlCommand.Prepare メソッド

SQL Server のインスタンスに対する準備済みのコマンドを作成します。

Public Overridable Sub Prepare() Implements IDbCommand.Prepare
[C#]
public virtual void Prepare();
[C++]
public: virtual void Prepare();
[JScript]
public function Prepare();

実装

IDbCommand.Prepare

例外

例外の種類 条件
InvalidOperationException Connection が設定されていません。

または

ConnectionOpen ではありません。

解説

CommandTypeStoredProcedure に設定したときは、 Prepare の呼び出しには成功しますが、結果が no-op になることがあります。

Prepare を呼び出す前に、準備するステートメントの各パラメータのデータ型を指定します。可変長データ型のパラメータの場合は、 Size プロパティに、必要な最大サイズを設定する必要があります。これらの条件が満たされていない場合は、 Prepare からエラーが返されます。

Prepare を呼び出した後で Execute メソッドを呼び出すと、 Size プロパティに指定した値よりも大きいパラメータ値は、パラメータで指定したサイズに自動的に切り詰められます。このとき、切り捨てエラーは返されません。

出力パラメータは、準備済みかどうかにかかわらず、ユーザー指定のデータ型にする必要があります。可変長データ型を指定する場合は、 Size の最大値も指定する必要があります。

使用例

[Visual Basic, C#, C++] Prepare メソッドを使用する例を次に示します。

 
Public Sub SqlCommandPrepare()
    Dim id As Integer = 20
    Dim desc As String = "myFirstRegion"
    Dim rConn As SqlConnection = New SqlConnection("Persist Security Info=False;" & _
                 "Integrated Security=SSPI;database=northwind;server=mySQLServer")
    rConn.Open()
    Dim command As SqlCommand = New SqlCommand("", rConn)

    ' Create and prepare an SQL statement.
    command.CommandText = "insert into Region (RegionID, RegionDescription) values (@id, @desc)"
    command.Parameters.Add("@id", id)
    command.Parameters.Add("@desc", desc)
    command.Prepare() ' Calling Prepare after having set the Commandtext and parameters.
    command.ExecuteNonQuery()

    ' Change parameter values and call ExecuteNonQuery.
    command.Parameters(0).Value = 21
    command.Parameters(1).Value = "mySecondRegion"
    command.ExecuteNonQuery()
End Sub

[C#] 
public void SqlCommandPrepareEx() {
     int  id = 20;
     string  desc = "myFirstRegion" ;
     SqlConnection rConn = new SqlConnection("Persist Security Info=False;Integrated Security=SSPI;database=northwind;server=mySQLServer");
     rConn.Open();
     SqlCommand command    = new SqlCommand(null, rConn);

     // Create and prepare an SQL statement.
     command.CommandText = "insert into Region (RegionID, RegionDescription) values (@id, @desc)" ;
     command.Parameters.Add ( "@id", id) ;
     command.Parameters.Add ( "@desc", desc) ;
     command.Prepare() ;  // Calling Prepare after having set the Commandtext and parameters.
     command.ExecuteNonQuery();

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

[C++] 
public:
    void SqlCommandPrepareEx()
    {
        int id = 20;
        String*  desc = S"myFirstRegion" ;
        SqlConnection* rConn = new SqlConnection(S"Persist Security Info=False;Integrated Security=SSPI;database=northwind;server=mySQLServer");
        rConn->Open();
        SqlCommand* command = new SqlCommand(0, rConn);

        // Create and prepare an SQL statement.
        command->CommandText = S"insert into Region (RegionID, RegionDescription) values (@id, @desc)" ;
        command->Parameters->Add (S"@id", __box(id));
        command->Parameters->Add (S"@desc", desc);
        command->Prepare();  // Calling Prepare after having set the CommandText and parameters.
        command->ExecuteNonQuery();

        // Change parameter values and call ExecuteNonQuery.
        command->Parameters->Item[0]->Value = __box(21);
        command->Parameters->Item[1]->Value = S"mySecondRegion";
        command->ExecuteNonQuery();
    };

[JScript] JScript のサンプルはありません。Visual Basic、C#、および C++ のサンプルを表示するには、このページの左上隅にある言語のフィルタ ボタン 言語のフィルタ をクリックします。

必要条件

プラットフォーム: Windows 98, Windows NT 4.0, Windows Millennium Edition, Windows 2000, Windows XP Home Edition, Windows XP Professional, Windows Server 2003 ファミリ, .NET Compact Framework - Windows CE .NET

参照

SqlCommand クラス | SqlCommand メンバ | System.Data.SqlClient 名前空間