Oracle Data Provider for .NET连接oracle数据库

        Oracle Data Provider for .NET有两个名字空间,分别是Oracle.DataAccess.Client和Oracle.DataAccess.Types。Oracle.DataAccess.Client用于设置数据库的连接等等通用功能,Oracle.DataAccess.Types用于设置oracle自定义的一些数据类型。使用之前,需要在项目中添加引用Oracle.DataAccesss.dll。下面举个例子说明:
       1. 建立数据库
           建立一个名为OracleTypesTable的表
           "create table OracleTypesTable (MyVarchar2 varchar2(3000),MyNumber number(28,4) Primary key ,MyDate           date,MyRaw RAW(255))";
           插入一行数据
          "insert into OracleTypesTable values ('test',4,to_date('2000-01-11
           12:54:01','yyyy-mm-dd hh24:mi:ss'),'0001020304')";
      2.c#实例

       private void Page_Load(object sender, System.EventArgs e)
  {
  
   string connstring="User Id=用户名;Password=密码;Data Source=数据源";
           //实例化OracleConnection对象
            OracleConnection conn=new OracleConnection(connstring);

            try
            {
              conn.Open(); 
              //实例化OracleCommand对象
              OracleCommand cmd=conn.CreateCommand();

              cmd.CommandText="select * from 数据源.OracleTypesTable";
              OracleDataReader oracledatareader1=cmd.ExecuteReader();
              //读取数据
              while (oracledatareader1.Read()) 
     {
             //读取并显示第一行第一列的数据 
               OracleString oraclestring1=oracledatareader1.GetOracleString(0);
               Response.Write("<br>OracleString :" +oraclestring1.ToString());

            //读取并显示第一行第二列的数据
               OracleDecimal oraclenumber1 =oracledatareader1.GetOracleDecimal(1);//.GetOracleValue(1);//GetOracleNumber(1);
               Response.Write("<br>OracleNumber :"+oraclenumber1.ToString());
      Response.Write("<br>NumberType :"+oraclenumber1.GetType());

            //读取并显示第一行第三列的数据
               OracleDate oracledatetime1=oracledatareader1.GetOracleDate(2);
               Response.Write("<br>OracleDateTime :" +oracledatetime1.ToString());

            //读取并显示第一行第四列的数据
               OracleBinary oraclebinary1=oracledatareader1.GetOracleBinary(3);
               if(oraclebinary1.IsNull==false)
      {
       foreach(byte b in oraclebinary1.Value)
       {
        Response.Write("<br>byte :" +b.ToString());
       }
      }
     }
            //释放资源
    oracledatareader1.Close(); 
   }
   catch(Exception ee)
   {
           //异常处理
    strMESS.Text=ee.Message;
   }
   finally
   {
         //关闭连接
    conn.Close();
   }
  }

你可能感兴趣的:(oracle,.net,数据库,exception,Date,insert)