SqlCommand.Prepare メソッド
アセンブリ: System.Data (system.data.dll 内)


例外の種類 | 条件 |
---|---|
InvalidOperationException | Connection が設定されていません。 または Connection が Open ではありません。 |

CommandType を StoredProcedure に設定したときは、Prepare の呼び出しには成功しますが、結果が no-op になることがあります。
Prepare を呼び出す前に、準備するステートメントの各パラメータのデータ型を指定します。可変長データ型のパラメータの場合は、Size プロパティに、必要な最大サイズを設定する必要があります。これらの条件が満たされていない場合は、Prepare からエラーが返されます。
Prepare を呼び出した後で Execute メソッドを呼び出すと、Size プロパティに指定した値よりも大きいパラメータ値は、パラメータで指定したサイズに自動的に切り詰められます。このとき、切り捨てエラーは返されません。
出力パラメータは、準備済みかどうかにかかわらず、ユーザー指定のデータ型にする必要があります。可変長データ型を指定する場合は、Size の最大値も指定する必要があります。

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(); } }

Windows 98, Windows 2000 SP4, Windows CE, Windows Millennium Edition, Windows Mobile for Pocket PC, Windows Mobile for Smartphone, Windows Server 2003, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP SP2, Windows XP Starter Edition
開発プラットフォームの中には、.NET Framework によってサポートされていないバージョンがあります。サポートされているバージョンについては、「システム要件」を参照してください。


Weblioに収録されているすべての辞書からSqlCommand.Prepare メソッドを検索する場合は、下記のリンクをクリックしてください。

- SqlCommand.Prepare メソッドのページへのリンク