C# 通过ado.net 访问oracle

1.安装ODP(oracle data provider)

2.然后在项目中引用 Oracle.DataAccess程序集

3.接着

using Oracle.DataAccess.Client;
using Oracle.DataAccess.Types;

 

4.然后编写程序和ado.net方位sqlserver 差别不大了,就是利用下面的对象进行编程,当然,因为oracle和sqlserver有很多地方不一样,所以细节存在很大差异。

 

OracleConnection

 

OracleCommand

        OracleParameter

 

OracleDataReader

 

OracleDataAdapter

 

5. 实例

例1: 根据部门ID 获取部门名称

            string oradb = "Data Source=TESTASM;User Id=hr;Password=hr;";
            OracleConnection conn = new OracleConnection(oradb);
            conn.Open();

            OracleParameter param = new OracleParameter();
            param.OracleDbType = OracleDbType.Decimal;
            param.Value = textBox1.Text;


            OracleCommand cmd = new OracleCommand();
            cmd.Connection = conn;
            
            cmd.Parameters.Add(param);
            cmd.CommandText = "select department_name from departments where department_id = :1";
           

            if(dr.Read())
            {

                  textBox1.Text = dr.GetString(1);

            }

            conn.Dispose();

 

实例2:获取所有部门信息

string oradb = "Data Source=TESTASM;User Id=hr;Password=hr;";
            OracleConnection conn = new OracleConnection(oradb);
            conn.Open();

            OracleParameter param = new OracleParameter();
            param.OracleDbType = OracleDbType.Decimal;
            param.Value = textBox1.Text;


            OracleCommand cmd = new OracleCommand();
            cmd.Connection = conn;

  cmd.CommandText = "select department_id,department_name,city"
                + " from departments d,locations l"
                + " where d.location_id= l.location_id";
            cmd.CommandType = CommandType.Text;
            OracleDataReader dr = cmd.ExecuteReader();
            while (dr.Read())
            {
                listBox1.Items.Add("The " + dr.GetString(1) + "department is in " + dr.GetString(2));
               
            }
           
            conn.Dispose(); 

 

实例3:增加语句的缓存,以在多次运行该语句时提高性能,减少硬解析

        string conString = "User Id=hr; Password=hr; Data Source=TESTASM;";

       OracleConnection con1 = new OracleConnection();
        con1.ConnectionString = conString  + "Statement Cache Size=2";

 

实例4:增加每次提取的数据量,减少网络来回交互次数。

       OracleCommand cmd = con.CreateCommand();
       cmd.CommandText = "select * from employees";

       OracleDataReader reader = cmd.ExecuteReader();

       reader.FetchSize = cmd.RowSize * 100;

 

实例5:关联数组使用

CREATE OR REPLACE PACKAGE MYPACK AS
TYPE AssocArrayVarchar2_t is table of VARCHAR(20) index by BINARY_INTEGER;
          PROCEDURE MYSP(
            Param1 IN     AssocArrayVarchar2_t,
            Param3    OUT AssocArrayVarchar2_t);
          END MYPACK;

 

create or replace package body MYPACK as
  PROCEDURE MYSP(
  Param1 IN     AssocArrayVarchar2_t,
  Param3    OUT AssocArrayVarchar2_t)
  IS
  BEGIN
    Param3(1) := Param1(1);
    Param3(2) := Param1(2);
    Param3(3) := Param1(3);
  END MYSP;
END MYPACK;

 

      string conString = "User Id=hr; Password=hr; Data Source=TESTASM;";
            OracleConnection con1 = new OracleConnection();
            con1.ConnectionString = conString ;

            con1.Open();

            OracleCommand cmd = con1.CreateCommand();
            cmd.CommandText = "MYPACK.MYSP";
            cmd.CommandType = CommandType.StoredProcedure;

            OracleParameter param1 = cmd.Parameters.Add("param1", OracleDbType.Varchar2);
            OracleParameter param3 = cmd.Parameters.Add("param3", OracleDbType.Varchar2);
            param1.CollectionType = OracleCollectionType.PLSQLAssociativeArray;
            param3.CollectionType = OracleCollectionType.PLSQLAssociativeArray;

            param1.Direction = ParameterDirection.Input;
            param3.Direction = ParameterDirection.Output;

            param1.Value = new string[3] { "Oracle", "Database", "Rules" };
            param3.Value = null;

            param1.Size = 3;
            param3.Size = 3;

            param1.ArrayBindSize = new int[3] { 20, 20, 20 };
            param3.ArrayBindSize = new int[3] { 20, 20, 20 };

            cmd.ExecuteNonQuery();

            for (int i = 0; i < 3; i++)
            {
                Console.Write((param3.Value as OracleString[])[i]);
                Console.WriteLine();
            }

 

实例6:游标变量的使用

            string conString = "User Id=hr; Password=hr; Data Source=TESTASM;";
            OracleConnection con1 = new OracleConnection();
            con1.ConnectionString = conString;

            con1.Open();

            string cmdtxt = "BEGIN " +
                "OPEN :1 for select first_name,department_id from EMPLOYEES where department_id = 10; " +
                "OPEN :2 for select first_name,department_id from EMPLOYEES where department_id = 20; " +
                "OPEN :3 for select first_name,department_id from EMPLOYEES where department_id = 30; " +
                "END;";
            OracleCommand cmd = new OracleCommand(cmdtxt, con1);
          
            cmd.CommandType = CommandType.Text;

            OracleParameter param1 = cmd.Parameters.Add("1", OracleDbType.RefCursor);
            OracleParameter param2 = cmd.Parameters.Add("2", OracleDbType.RefCursor);
            OracleParameter param3 = cmd.Parameters.Add("3", OracleDbType.RefCursor);
        

            param1.Direction = ParameterDirection.Output;
            param2.Direction = ParameterDirection.Output;
            param3.Direction = ParameterDirection.Output;

            cmd.ExecuteNonQuery();

            OracleDataReader dr1 = ((OracleRefCursor)cmd.Parameters[0].Value).GetDataReader();
            OracleDataReader dr2 = ((OracleRefCursor)cmd.Parameters[1].Value).GetDataReader();
            OracleDataReader dr3 = ((OracleRefCursor)cmd.Parameters[2].Value).GetDataReader();

            while (dr1.Read())
            {
                Console.WriteLine("Employee Name: " + dr1.GetString(0) + ", " + "Employee Dept:" + dr1.GetDecimal(1));
            }
            Console.WriteLine();

            while (dr2.Read())
            {
                Console.WriteLine("Employee Name: " + dr2.GetString(0) + ", " + "Employee Dept:" + dr2.GetDecimal(1));
            }
            Console.WriteLine();

            while (dr3.Read())
            {
                Console.WriteLine("Employee Name: " + dr3.GetString(0) + ", " + "Employee Dept:" + dr3.GetDecimal(1));
            }
            Console.ReadLine();

            con1.Dispose();

具体细节可以参考OdpNet.pdf (ODP的官方帮助文档,安装完ODP后在对应的doc目录可以找到)

你可能感兴趣的:(oracle,String,cmd,C#,sqlserver,textbox)