OracleDataReader 類別

定義

提供從數據源讀取順向數據列數據流的方式。 此類別無法獲得繼承。

public ref class OracleDataReader sealed : MarshalByRefObject, IDisposable, System::Collections::IEnumerable, System::Data::IDataReader
public ref class OracleDataReader sealed : System::Data::Common::DbDataReader
public sealed class OracleDataReader : MarshalByRefObject, IDisposable, System.Collections.IEnumerable, System.Data.IDataReader
public sealed class OracleDataReader : System.Data.Common.DbDataReader
type OracleDataReader = class
    inherit MarshalByRefObject
    interface IDataReader
    interface IDisposable
    interface IDataRecord
    interface IEnumerable
type OracleDataReader = class
    inherit DbDataReader
Public NotInheritable Class OracleDataReader
Inherits MarshalByRefObject
Implements IDataReader, IDisposable, IEnumerable
Public NotInheritable Class OracleDataReader
Inherits DbDataReader
繼承
OracleDataReader
繼承
實作

範例

以下範例可產生一個 OracleConnection、 、 OracleCommandOracleDataReader。 此範例會讀取數據,並將其寫出至主控台。 最後,範例會封 OracleDataReader閉 ,接著 OracleConnection

public void ReadData(string connectionString)
{
   string queryString = "SELECT EmpNo, EName FROM Emp";
   using (OracleConnection connection = new OracleConnection(connectionString))
   {
      OracleCommand command = new OracleCommand(queryString, connection);
      connection.Open();
      using(OracleDataReader reader = command.ExecuteReader())
      {
         // Always call Read before accessing data.
         while (reader.Read())
         {
            Console.WriteLine(reader.GetInt32(0) + ", " + reader.GetString(1));
         }
      }
   }
}
Public Sub ReadData(ByVal connectionString As String)
    Dim queryString As String = "SELECT EmpNo, EName FROM Emp"
    Using connection As New OracleConnection(connectionString)
        Dim command As New OracleCommand(queryString, connection)
        connection.Open()
        Using reader As OracleDataReader = command.ExecuteReader()
            ' Always call Read before accessing data.
            While reader.Read()
                Console.WriteLine(reader.GetInt32(0).ToString() + ", " _
                   + reader.GetString(1))
            End While
        End Using
    End Using
End Sub

備註

要建立 , OracleDataReader你必須呼叫 ExecuteReader 物件的方法 OracleCommand ,而不是直接使用建構子。

在資料讀取過程中,其他程序或執行緒對結果集所做的變更,使用者可能會看到 OracleDataReader

IsClosedRecordsAffected 是關閉後 OracleDataReader 你唯一能調用的屬性。 在某些情況下,您必須先打電話 Close 才能撥打 RecordsAffected

同時可能有多個 OracleDataReader 門開著。

以下兩個Visual Basic範例示範如何使用 OracleDataReader 取得 Oracle REF CURSOR。 這些範例使用Oracle Scott/Tiger架構中定義的資料表,並需要以下PL/SQL套件及套件主體。 你必須在伺服器上建立這些範例才能使用範例。

在 Oracle 伺服器上建立下列 Oracle 套件。

CREATE OR REPLACE PACKAGE CURSPKG AS
   TYPE T_CURSOR IS REF CURSOR;
   PROCEDURE OPEN_ONE_CURSOR (N_EMPNO IN NUMBER,
      IO_CURSOR IN OUT T_CURSOR);
   PROCEDURE OPEN_TWO_CURSORS (EMPCURSOR OUT T_CURSOR,
      DEPTCURSOR OUT T_CURSOR);
END CURSPKG;
/

在 Oracle 伺服器上建立下列 Oracle 套件主體。

CREATE OR REPLACE PACKAGE BODY CURSPKG AS
    PROCEDURE OPEN_ONE_CURSOR (N_EMPNO IN NUMBER,
                               IO_CURSOR OUT T_CURSOR)
    IS
        V_CURSOR T_CURSOR;
    BEGIN
        IF N_EMPNO <> 0 THEN
             OPEN V_CURSOR FOR
             SELECT EMP.EMPNO, EMP.ENAME, DEPT.DEPTNO, DEPT.DNAME
                  FROM EMP, DEPT
                  WHERE EMP.DEPTNO = DEPT.DEPTNO
                        AND EMP.EMPNO = N_EMPNO;
        ELSE
             OPEN V_CURSOR FOR
             SELECT EMP.EMPNO, EMP.ENAME, DEPT.DEPTNO, DEPT.DNAME
                  FROM EMP, DEPT
                  WHERE EMP.DEPTNO = DEPT.DEPTNO;
        END IF;
        IO_CURSOR := V_CURSOR;
    END OPEN_ONE_CURSOR;
    PROCEDURE OPEN_TWO_CURSORS (EMPCURSOR OUT T_CURSOR,
                                DEPTCURSOR OUT T_CURSOR)
    IS
        V_CURSOR1 T_CURSOR;
        V_CURSOR2 T_CURSOR;
    BEGIN
        OPEN V_CURSOR1 FOR SELECT * FROM EMP;
        OPEN V_CURSOR2 FOR SELECT * FROM DEPT;
        EMPCURSOR  := V_CURSOR1;
        DEPTCURSOR := V_CURSOR2;
    END OPEN_TWO_CURSORS;
END CURSPKG;
/

此Visual Basic範例執行一個 PL/SQL 儲存程序,回傳 REF CURSOR 參數,並將該值讀取為 OracleDataReader

Private Sub ReadOracleData(ByVal connectionString As String)
   Dim connection As New OracleConnection(connectionString)
   Dim command As New OracleCommand()
   Dim reader As OracleDataReader

   connection.Open()
   command.Connection = connection
   command.CommandText = "CURSPKG.OPEN_ONE_CURSOR"
   command.CommandType = CommandType.StoredProcedure
   command.Parameters.Add(New OracleParameter("N_EMPNO", OracleType.Number)).Value = 7369
   command.Parameters.Add(New OracleParameter("IO_CURSOR", OracleType.Cursor)).Direction = ParameterDirection.Output

   reader = command.ExecuteReader()
   While (reader.Read())
      ' Do something with the values.
   End While
   reader.Close()
   connection.Close()
End Sub

此Visual Basic範例執行一個 PL/SQL 儲存程序,回傳兩個 REF CURSOR 參數,並使用 OracleDataReader 讀取這些值。

Private Sub ReadOracleData(ByVal connectionString As String)
   Dim dataSet As New DataSet()
   Dim connection As New OracleConnection(connectionString)
   Dim command As New OracleCommand()
   Dim reader As OracleDataReader

   connection.Open()
   command.Connection = connection
   command.CommandText = "CURSPKG.OPEN_TWO_CURSORS"
   command.CommandType = CommandType.StoredProcedure
   command.Parameters.Add(New OracleParameter("EMPCURSOR", OracleType.Cursor)).Direction = ParameterDirection.Output
   command.Parameters.Add(New OracleParameter("DEPTCURSOR", OracleType.Cursor)).Direction = ParameterDirection.Output

   reader = command.ExecuteReader(CommandBehavior.CloseConnection)
   While (reader.Read())
      ' Do something with the values.
   End While
   reader.NextResult()
   While (reader.Read())
        ' Do something with the values.
   End While
   reader.Close()
   connection.Close()
 End Sub

這個 C# 範例建立了一個 Oracle 表格並載入資料。 你必須先執行這個範例,才能執行下一個範例,該範例示範如何使用 a OracleDataReader 來存取資料,使用 OracleType 結構。

public void Setup(string connectionString)
{
   OracleConnection connection = new OracleConnection(connectionString);
   try
   {
      connection.Open();
      OracleCommand command = connection.CreateCommand();
      command.CommandText ="CREATE TABLE OracleTypesTable (MyVarchar2 varchar2(3000),MyNumber number(28,4) PRIMARY KEY,MyDate date, MyRaw raw(255))";
      command.ExecuteNonQuery();
      command.CommandText ="INSERT INTO OracleTypesTable VALUES ('test', 2, to_date('2000-01-11 12:54:01','yyyy-mm-dd hh24:mi:ss'), '0001020304')";
      command.ExecuteNonQuery();
      command.CommandText="SELECT * FROM OracleTypesTable";
   }
   catch(Exception)
   {
   }
   finally
   {
      connection.Close();
   }
}

這個 C# 範例使用 來 OracleDataReader 存取資料,並使用多種 OracleType 結構來顯示資料。

public void ReadOracleTypesExample(string connectionString)
{
   OracleConnection connection = new OracleConnection(connectionString);
   connection.Open();
   OracleCommand command = connection.CreateCommand();
   try
   {
      command.CommandText = "SELECT * FROM OracleTypesTable";
      OracleDataReader reader = command.ExecuteReader();
      reader.Read();
      //Using the Oracle specific getters for each type is faster than
      //using GetOracleValue.
      //First column, MyVarchar2, is a VARCHAR2 data type in Oracle Server
      //and maps to OracleString.
      OracleString oraclestring1 = reader.GetOracleString(0);
      Console.WriteLine("OracleString " + oraclestring1.ToString());

      //Second column, MyNumber, is a NUMBER data type in Oracle Server
      //and maps to OracleNumber.
      OracleNumber oraclenumber1 = reader.GetOracleNumber(1);
      Console.WriteLine("OracleNumber " + oraclenumber1.ToString());

      //Third column, MyDate, is a DATA data type in Oracle Server
      //and maps to OracleDateTime.
      OracleDateTime oracledatetime1 = reader.GetOracleDateTime(2);
      Console.WriteLine("OracleDateTime " + oracledatetime1.ToString());

      //Fourth column, MyRaw, is a RAW data type in Oracle Server and
      //maps to OracleBinary.
      OracleBinary oraclebinary1 = reader.GetOracleBinary(3);

      //Calling value on a null OracleBinary throws
      //OracleNullValueException; therefore, check for a null value.
      if (oraclebinary1.IsNull==false)
      {
         foreach(byte b in oraclebinary1.Value)
         {
            Console.WriteLine("byte " + b.ToString());
         }
      }
      reader.Close();
   }
   catch(Exception e)
   {
      Console.WriteLine(e.ToString());
   }
   finally
   {
      connection.Close();
   }
}

屬性

名稱 Description
Depth

會獲得當前列巢狀深度的值。

FieldCount

取得目前數據列中的數據行數目。

HasRows

會得到一個值,表示是否 OracleDataReader 包含一列或多列。

IsClosed

表示是否 OracleDataReader 已關閉。

Item[Int32]

取得指定資料行的原生格式,指定資料行序數的值。

Item[String]

取得指定數據行名稱的原生格式指定數據行的值。

RecordsAffected

取得執行 SQL 陳述句時所變更、插入或刪除的列數。

VisibleFieldCount

取得中未被隱藏的欄位 DbDataReader 數量。

(繼承來源 DbDataReader)

方法

名稱 Description
Close()

關閉 OracleDataReader 物件。

CreateObjRef(Type)

建立一個物件,包含產生代理伺服器所需的所有相關資訊,用於與遠端物件通訊。

(繼承來源 MarshalByRefObject)
Dispose()

釋放該物件所使用的資源。

Dispose()

釋放目前類別實例 DbDataReader 所使用的所有資源。

(繼承來源 DbDataReader)
Dispose(Boolean)

釋放 未管理的資源, DbDataReader 並可選擇性地釋放受管理資源。

(繼承來源 DbDataReader)
Equals(Object)

判斷指定的物件是否等於目前的物件。

(繼承來源 Object)
GetBoolean(Int32)

取得指定之數據行的值做為布爾值。

GetByte(Int32)

取得指定數據行的值做為位元組。

GetBytes(Int32, Int64, Byte[], Int32, Int32)

從指定的欄位偏移量讀取一串位元組到緩衝區,從指定的緩衝區偏移開始,作為陣列。

GetChar(Int32)

取得指定數據行的值做為字元。

GetChars(Int32, Int64, Char[], Int32, Int32)

從指定的欄位偏移量讀取一串字元到緩衝區,從指定的緩衝區偏移開始,作為陣列。

GetData(Int32)

回傳指定欄位序數的 a IDataReader

GetData(Int32)

回傳所請求欄位的巢狀資料讀取器。

(繼承來源 DbDataReader)
GetDataTypeName(Int32)

取得源數據類型的名稱。

GetDateTime(Int32)

取得指定欄位的值作為 DateTime 物件。

GetDbDataReader(Int32)

回傳 DbDataReader 請求欄位序數的物件,該物件可用提供者專屬實作覆寫。

(繼承來源 DbDataReader)
GetDecimal(Int32)

取得指定欄位的值作為 Decimal 物件。

GetDouble(Int32)

取得指定欄位的值,為雙精度浮點數。

GetEnumerator()

回傳 和 IEnumerator 可用於資料讀取器中遍歷各列。

GetFieldType(Int32)

取得 Type 那是物件的資料型別。

GetFieldValue<T>(Int32)

取得指定數據行的值做為要求的型別。

(繼承來源 DbDataReader)
GetFieldValueAsync<T>(Int32, CancellationToken)

以異步方式取得指定數據行的值做為要求的型別。

(繼承來源 DbDataReader)
GetFieldValueAsync<T>(Int32)

以異步方式取得指定數據行的值做為要求的型別。

(繼承來源 DbDataReader)
GetFloat(Int32)

取得指定數據行的值做為單精度浮點數。

GetGuid(Int32)

取得指定欄位的值作為全域唯一識別碼(GUID)。

GetHashCode()

做為預設哈希函式。

(繼承來源 Object)
GetInt16(Int32)

取得指定數據行的值,做為16位帶正負號的整數。

GetInt32(Int32)

取得指定數據行的值,做為32位帶正負號的整數。

GetInt64(Int32)

取得指定數據行的值,做為64位帶正負號的整數。

GetLifetimeService()
已淘汰.

擷取控制這個實例存留期原則的目前存留期服務物件。

(繼承來源 MarshalByRefObject)
GetName(Int32)

取得指定數據行的名稱。

GetOracleBFile(Int32)

取得指定欄位的值作為 OracleBFile 物件。

GetOracleBinary(Int32)

取得指定欄位的值作為 OracleBinary 物件。

GetOracleDateTime(Int32)

取得指定欄位的值作為 OracleDateTime 物件。

GetOracleLob(Int32)

取得指定欄位的值作為 OracleLob 物件。

GetOracleMonthSpan(Int32)

取得指定欄位的值作為 OracleMonthSpan 物件。

GetOracleNumber(Int32)

取得指定欄位的值作為 OracleNumber 物件。

GetOracleString(Int32)

取得指定欄位的值作為 OracleString 物件。

GetOracleTimeSpan(Int32)

取得指定欄位的值作為 OracleTimeSpan 物件。

GetOracleValue(Int32)

以 Oracle 格式取得欄位在指定序數處的值。

GetOracleValues(Object[])

以 Oracle 格式取得當前列中所有屬性欄位。

GetOrdinal(String)

取得資料列序數,指定資料行的名稱。

GetProviderSpecificFieldType(Int32)

得到 Object 的 that 是底層提供者特定欄位類型的代表。

GetProviderSpecificValue(Int32)

得到 Object 的 that 是底層提供者特定欄位類型的代表。

GetProviderSpecificValues(Object[])

會獲得一組物件陣列,代表底層提供者的特定值。

GetSchemaTable()

回傳描述 Oracle DataReader 欄位元資料的 a DataTable

GetStream(Int32)

取得數據流,以從指定的數據行擷取數據。

(繼承來源 DbDataReader)
GetString(Int32)

取得指定數據行的值做為字串。

GetTextReader(Int32)

取得文字讀取器,以從數據行擷取數據。

(繼承來源 DbDataReader)
GetTimeSpan(Int32)

取得指定欄位的值為 System.TimeSpan

GetType()

取得目前實例的 Type

(繼承來源 Object)
GetValue(Int32)

取得以原生格式指定序數處的數據行值。

GetValues(Object[])

使用目前數據列的數據行值填入 物件的陣列。

InitializeLifetimeService()
已淘汰.

取得存留期服務物件,以控制這個實例的存留期原則。

(繼承來源 MarshalByRefObject)
IsDBNull(Int32)

會得到一個值,表示欄位是否包含不存在或缺失值。

IsDBNullAsync(Int32, CancellationToken)

以異步方式取得值,指出數據行是否包含不存在或遺漏的值。

(繼承來源 DbDataReader)
IsDBNullAsync(Int32)

以異步方式取得值,指出數據行是否包含不存在或遺漏的值。

(繼承來源 DbDataReader)
MemberwiseClone()

建立目前 Object的淺層複本。

(繼承來源 Object)
MemberwiseClone(Boolean)

建立目前 MarshalByRefObject 對象的淺層複本。

(繼承來源 MarshalByRefObject)
NextResult()

將 推進 OracleDataReader 到下一個結果。

NextResultAsync()

讀取語句批次的結果時,以異步方式將讀取器前進到下一個結果。

(繼承來源 DbDataReader)
NextResultAsync(CancellationToken)

讀取語句批次的結果時,以異步方式將讀取器前進到下一個結果。

(繼承來源 DbDataReader)
Read()

推進到 OracleDataReader 下一個紀錄。

ReadAsync()

以異步方式將讀取器前進到結果集中的下一筆記錄。

(繼承來源 DbDataReader)
ReadAsync(CancellationToken)

以異步方式將讀取器前進到結果集中的下一筆記錄。

(繼承來源 DbDataReader)
ToString()

傳回表示目前 物件的字串。

(繼承來源 Object)

明確介面實作

名稱 Description
IDataRecord.GetData(Int32)

關於此成員的描述,請參見 GetData(Int32)

(繼承來源 DbDataReader)
IEnumerable.GetEnumerator()

傳回逐一查看集合的列舉值。

擴充方法

名稱 Description
AsParallel(IEnumerable)

啟用查詢的平行處理。

AsQueryable(IEnumerable)

IEnumerable 轉換成 IQueryable

CanGetColumnSchema(DbDataReader)

會得到一個值,表示 a DbDataReader 是否能取得欄位結構。

Cast<TResult>(IEnumerable)

IEnumerable 的項目轉換成指定的型別。

GetColumnSchema(DbDataReader)

取得欄位結構(DbColumn 集合)的 DbDataReader

OfType<TResult>(IEnumerable)

根據指定的型別篩選 IEnumerable 的專案。

適用於