OleDbCommand.Prepare 方法

定义

在数据源上创建准备就绪(或已编译)的命令版本。

C#
public override void Prepare();
C#
public void Prepare();

实现

例外

未设置 Connection

- 或 -

Connection 未打开。

示例

以下示例创建 OleDbCommand 并打开连接。 然后,该示例通过传递 SQL SELECT 语句的字符串和用于连接到数据源的字符串,在数据源上准备存储过程。

C#
private static void OleDbCommandPrepare(string connectionString)
{
    using (OleDbConnection connection = new
               OleDbConnection(connectionString))
    {
        connection.Open();

        // Create the Command.
        OleDbCommand command = new OleDbCommand();

        // Set the Connection, CommandText and Parameters.
        command.Connection = connection;
        command.CommandText =
            "INSERT INTO dbo.Region (RegionID, RegionDescription) VALUES (?, ?)";
        command.Parameters.Add("RegionID", OleDbType.Integer, 4);
        command.Parameters.Add("RegionDescription", OleDbType.VarWChar, 50);
        command.Parameters[0].Value = 20;
        command.Parameters[1].Value = "First Region";

        // Call  Prepare and ExecuteNonQuery.
        command.Prepare();
        command.ExecuteNonQuery();

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

注解

如果 属性 CommandType 设置为 TableDirect,则 Prepare 不执行任何作用。 如果 CommandType 设置为 StoredProcedure,则对 的 Prepare 调用应成功,尽管这可能会导致 no-op。

在调用 Prepare之前,指定要准备的语句中每个参数的数据类型。 对于具有可变长度数据类型的每个参数,必须将 Size 属性设置为所需的最大大小。 Prepare 如果不满足这些条件,则返回错误。

如果在调用 后Prepare调用Execute方法,则任何大于 Size 属性指定的值的参数值都将自动截断为参数的原始指定大小,并且不会返回截断错误。

输出参数 (准备好) 是否必须具有用户指定的数据类型。 如果指定可变长度数据类型,则还必须指定最大 大小

适用于

产品 版本
.NET 8 (package-provided), 9 (package-provided), 10 (package-provided)
.NET Framework 1.1, 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
.NET Standard 2.0 (package-provided)

另请参阅