分享方式:


OracleType

Oracle 的 .NET Framework 資料提供者包括數個可讓您使用 Oracle 資料型別的結構。 這些結構包括 OracleNumberOracleString

注意

如需這些結構的完整清單,請參閱 System.Data.OracleClient

建立 Oracle 資料表

此範例建立 Oracle 資料表並為其載入資料。 您必須先執行此範例,才能執行下一個範例。

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

從 Oracle 資料表擷取資料

此範例使用 OracleDataReader 存取資料,並使用數個 OracleType 結構顯示資料。

public void ReadOracleTypesExample(string connectionString)
   {
   OracleConnection myConnection =
      new OracleConnection(connectionString);
   myConnection.Open();
   OracleCommand myCommand = myConnection.CreateCommand();

   try
      {
      myCommand.CommandText = "SELECT * from OracleTypesTable";
      OracleDataReader oracledatareader1 = myCommand.ExecuteReader();
      oracledatareader1.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 =
        oracledatareader1.GetOracleString(0);
      Console.WriteLine("OracleString " + oraclestring1.ToString());

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

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

      //Fourth column, MyRaw, is a RAW data type in Oracle Server and
      //maps to OracleBinary.
      OracleBinary oraclebinary1 =
        oracledatareader1.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());
         }
      }
      oracledatareader1.Close();
   }
   catch(Exception e)
   {
       Console.WriteLine(e.ToString());
   }
   finally
   {
       myConnection.Close();
   }
}

另請參閱