SqlCommand.Prepare Method
Definition
Important
Some information relates to prerelease product that may be substantially modified before it’s released. Microsoft makes no warranties, express or implied, with respect to the information provided here.
Creates a prepared version of the command on an instance of SQL Server.
public:
override void Prepare();
public:
virtual void Prepare();
public override void Prepare ();
public void Prepare ();
override this.Prepare : unit -> unit
abstract member Prepare : unit -> unit
override this.Prepare : unit -> unit
Public Overrides Sub Prepare ()
Public Sub Prepare ()
Implements
Examples
The following example demonstrates the use of the Prepare method.
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();
}
}
Private Sub SqlCommandPrepareEx(ByVal connectionString As String)
Using connection As New SqlConnection(connectionString)
connection.Open()
Dim command As 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
Remarks
If CommandType is set to StoredProcedure
, the call to Prepare should succeed, although it may cause a no-op.
Before you call Prepare, specify the data type of each parameter in the statement to be prepared. For each parameter that has a variable length data type, you must set the Size property to the maximum size needed. Prepare returns an error if these conditions are not met.
Note
If the database context is changed by executing the Transact-SQL USE <database>
statement, or by calling the ChangeDatabase method, then Prepare must be called a second time.
If you call an Execute
method after calling Prepare, any parameter value that is larger than the value specified by the Size property is automatically truncated to the original specified size of the parameter, and no truncation errors are returned.
Output parameters (whether prepared or not) must have a user-specified data type. If you specify a variable length data type, you must also specify the maximum Size.
Prior to Visual Studio 2010, Prepare threw an exception. Beginning in Visual Studio 2010, this method does not throw an exception.