IDbCommand.Prepare 方法

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

**命名空间:**System.Data
**程序集:**System.Data(在 system.data.dll 中)

语法

声明
Sub Prepare
用法
Dim instance As IDbCommand

instance.Prepare
void Prepare ()
void Prepare ()
void Prepare ()
function Prepare ()

异常

异常类型 条件

InvalidOperationException

未设置 Connection

- 或 -

Connection 不为 Open。

备注

如果 CommandType 属性设置为 TableDirect,则 Prepare 不会执行任何操作。如果 CommandType 设置为 StoredProcedure,则对 Prepare 的调用应该会成功(虽然它可能导致无操作 (no-op))。

示例

下面的示例创建派生类 OleDbCommand 的实例,并打开该连接。然后,该示例通过传递如下两个字符串在数据源上准备存储过程:一个是 SQL Select 语句,另一个是用于连接到数据源的字符串。

Public Sub OleDbCommandPrepare(ByVal connectionString As String)

    Using connection As OleDbConnection = New _
        OleDbConnection(connectionString)
        connection.Open()

        ' Create the Command.
        Dim command As OleDbCommand = 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 = "Second Region"
        command.ExecuteNonQuery()
    End Using
End Sub
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();
    }
}

平台

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 并不是对每个平台的所有版本都提供支持。有关受支持版本的列表,请参见系统要求

版本信息

.NET Framework

受以下版本支持:2.0、1.1、1.0

.NET Compact Framework

受以下版本支持:2.0、1.0

请参见

参考

IDbCommand 接口
IDbCommand 成员
System.Data 命名空间