DB2 V9.7 for Linux, UNIX, and Windows

DB2Command 类

表示要对数据源执行的 SQL 语句或存储过程。

名称空间:
IBM®.Data.DB2®
组合件:
IBM.Data.DB2(在 IBM.Data.DB2.dll 中)

.NET Framework 2.0、3.0、3.5 和 4.0 继承层次结构

System.Object
   System.MarshalByRefObject
      System.ComponentModel.Component
         System.Data.Common.DbCommand
         IBM.Data.DB2.DB2Command

.NET Framework 2.0、3.0、3.5 和 4.0 语法

[Visual Basic]
NotInheritable Public Class DB2Command
   Inherits DbCommand
   Implements ICloneable
[C#]
public sealed class DB2Command : DbCommand, ICloneable
[C++]
public __gc __sealed class DB2Command : public DbCommand,
   ICloneable
[JScript]
public class DB2Command extends DbCommand implements ICloneable

注释

在 DB2 V9.7 FP5 和更高版本中,DB2Command 类提供 ArrayBindCountOff 常量以重置 ArrayBindCount 属性。

DB2Command 类提供了下列方法来对数据库执行命令:

Item 描述
ExecuteReader 执行返回行的命令。
ExecuteResultSet CommandText 发送至 Connection 并构建 DB2ResultSet
ExecuteNonQuery 执行诸如 SQL INSERT、DELETE、UPDATE 和 SET 语句之类的命令。
ExecuteXmlReader CommandText 发送至 Connection 并构建 XMLReader
ExecuteScalar 从数据库检索单个值(例如,聚集值)。

如果执行命令导致致命的 DB2Exception,那么 DB2Connection 可能会关闭。但是,用户可以重新打开该连接并继续。

示例

[Visual Basic, C#] 以下示例将使用 DB2CommandExecuteReader 方法以及 DB2DataReaderDB2Connection 从表中选择行。

[Visual Basic]
Public Sub ReadMyData(myConnString As String)
     Dim mySelectQuery As String = "SELECT SALES, SALES_PERSON FROM SALES"
     Dim myConnection As New DB2Connection(myConnString)
     Dim myCommand As New DB2Command(mySelectQuery, myConnection)
     myConnection.Open()
     Dim myReader As DB2DataReader = myCommand.ExecuteReader()
     Try
         While myReader.Read()
             Console.WriteLine(myReader.GetInt32(0).ToString() + ", " _
                + myReader.GetString(1))
         End While
     Finally
         ' always call Close when done reading.
         myReader.Close()
         ' always call Close when done with connection.
         myConnection.Close()
     End Try
 End Sub
[C#]
public void ReadMyData(string myConnString)
{
    string mySelectQuery = "SELECT SALES, SALES_PERSON FROM SALES";
    DB2Connection myConnection = new DB2Connection(myConnString);
    DB2Command myCommand = new DB2Command(mySelectQuery,myConnection);
    myConnection.Open();
    DB2DataReader myReader = myCommand.ExecuteReader();
    try
    {
       while (myReader.Read())
       {
          Console.WriteLine(myReader.GetInt32(0) + ", " + myReader.GetString(1));
       }
    }
    finally 
    {
       // always call Close when done reading.
       myReader.Close();
       // always call Close when done with connection.
       myConnection.Close();
    }
}

线程安全

此类型的任何公用静态(Visual Basic 中的 Shared)成员对于多线程操作都是安全的。 不保证任何实例成员均为线程安全。

版本信息

最近一次更新
本主题在 IBM DB2 V9.5 中进行了最近一次更新
.NET Framework 版本
支持版本:2.0、3.0、3.5 和 4.0
IBM 数据服务器客户机
支持版本:8.1.2 及更高版本