DB2指令類別

代表要針對資料來源執行的 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 Fix Pack 5 以及更新版本中, DB2Command 類別提供 ArrayBindCountOff 常數來重設 ArrayBindCount 內容。

DB2Command 類別提供下列方法,以針對資料庫執行指令:

項目 說明
ExecuteReader 執行傳回列的指令。
執行結果集 CommandText 傳送至 連線 ,並建置 DB2ResultSet
執行非查詢 執行 SQL INSERT、DELETE、UPDATE 及 SET 陳述式之類的指令。
ExecuteXmlReader CommandText 傳送至 連線 ,並建置 XMLReader
ExecuteScalar 從資料庫中擷取單一值 (例如,聚集值)。

如果執行指令導致嚴重 DB2®異常狀況 ,則 DB2連線 可能會關閉。 不過,使用者可以重新開啟連線並繼續。

範例

[Visual Basic , C#] 下列範例使用 DB2CommandExecuteReader 方法,以及 DB2DataReaderDB2連線 ,從表格中選取列。

[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 ) 成員對於多執行緒作業都是安全的。 不保證任何實例成員都是安全執行緒。