SqlCommand.Prepare メソッドとは? わかりやすく解説

Weblio 辞書 > コンピュータ > .NET Framework クラス ライブラリ リファレンス > SqlCommand.Prepare メソッドの意味・解説 

SqlCommand.Prepare メソッド

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

名前空間: System.Data.SqlClient
アセンブリ: System.Data (system.data.dll 内)
構文構文

例外例外
例外の種類条件

InvalidOperationException

Connection が設定されていません。

または

Connection が Open ではありません。

解説解説
使用例使用例

Prepare メソッドを使用する例を次に示します。

Private Sub SqlCommandPrepareEx(ByVal
 connectionString As String)
    Using connection As New SqlConnection(connectionString)
        connection.Open()
        Dim command As SqlCommand = 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
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();
    }
}
プラットフォームプラットフォーム
バージョン情報バージョン情報
参照参照



英和和英テキスト翻訳

英語⇒日本語日本語⇒英語

辞書ショートカット

すべての辞書の索引

SqlCommand.Prepare メソッドのお隣キーワード
検索ランキング

   

英語⇒日本語
日本語⇒英語
   



SqlCommand.Prepare メソッドのページの著作権

   
日本マイクロソフト株式会社日本マイクロソフト株式会社
© 2026 Microsoft.All rights reserved.

©2026 GRAS Group, Inc.RSS