SqlCommand.Prepare 方法  
定义
重要
一些信息与预发行产品相关,相应产品在发行之前可能会进行重大修改。 对于此处提供的信息,Microsoft 不作任何明示或暗示的担保。
在 SQL Server 的实例上创建命令的一个准备版本。
public:
 override void Prepare();public:
 virtual void Prepare();public override void Prepare();public void Prepare();override this.Prepare : unit -> unitabstract member Prepare : unit -> unit
override this.Prepare : unit -> unitPublic Overrides Sub Prepare ()Public Sub Prepare ()实现
示例
下面的示例演示 Prepare 方法的用法。
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
注解
如果 CommandType 设置为 StoredProcedure,则对 的 Prepare 调用应成功,尽管这可能会导致 no-op。
在调用 Prepare之前,指定要准备的语句中每个参数的数据类型。 对于具有可变长度数据类型的每个参数,必须将 属性设置为 Size 所需的最大大小。 Prepare 如果不满足这些条件,则返回错误。
注意
如果通过执行 Transact-SQL USE <database> 语句或调用 ChangeDatabase 方法更改了数据库上下文,则必须 Prepare 再次调用 。
如果在调用 Prepare后调用Execute方法,则任何大于 属性指定Size值的参数值都会自动截断为参数的原始指定大小,并且不会返回截断错误。
输出参数 (准备好) 是否必须具有用户指定的数据类型。 如果指定可变长度数据类型,则还必须指定最大 Size。
在 Visual Studio 2010 之前, Prepare 引发了异常。 从 Visual Studio 2010 开始,此方法不会引发异常。