ado.net oledb 调用oracle存储过程返回结果集

来源:互联网 发布:xampp apache无法启动 编辑:程序博客网 时间:2024/04/30 07:56

数据库脚本:

   CREATE TABLE DEPT
   (DEPTNO NUMBER(2,0) NOT NULL,
   DNAME VARCHAR2(14) NULL,
   LOC VARCHAR2(13) NULL,
   PRIMARY KEY (DEPTNO)
   );

   INSERT INTO Dept VALUES(11,'Sales','Texas');
   INSERT INTO Dept VALUES(22,'Accounting','Washington');
   INSERT INTO Dept VALUES(33,'Finance','Maine');

   CREATE TABLE EMP
   (EMPNO NUMBER(4,0) NOT NULL,
   ENAME VARCHAR2(10) NULL,
   JOB VARCHAR2(9) NULL,
   MGR NUMBER(4,0) NULL,
   SAL NUMBER(7,2) NULL,
   COMM NUMBER(7,2) NULL,
   DEPTNO NUMBER(2,0) NULL,
   FOREIGN KEY (DEPTNO) REFERENCES DEPT(DEPTNO), 
   PRIMARY KEY (EMPNO)
   );

   INSERT INTO Emp VALUES(123,'Bob','Sales',555,35000,12,11);
   INSERT INTO Emp VALUES(321,'Sue','Finance',555,42000,12,33);
   INSERT INTO Emp VALUES(234,'Mary','Account',555,33000,12,22);
  
   CREATE OR REPLACE PACKAGE curspkg_join AS
    TYPE t_cursor IS REF CURSOR ;
    Procedure open_join_cursor1 (io_cursor IN OUT t_cursor);
   END curspkg_join;
  
   CREATE OR REPLACE PACKAGE BODY curspkg_join AS
   Procedure open_join_cursor1 (io_cursor IN 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_join_cursor1;
   END curspkg_join;
  
   --drop table dept;
   --drop table emp;
   --drop PACKAGE curspkg_join; 

 

调用代码:

OleDbConnection Oraclecon = new OleDbConnection("Provider=MSDAORA.1;Password=tiger;"
              + "User ID=scott;Data Source=OracleServer;Persist Security Info=True");
Oraclecon.Open();
OleDbCommand myCMD =  new OleDbCommand
("{call curspkg_join.open_join_cursor1(?, {resultset 0, io_cursor})}", Oraclecon);
myCMD.Parameters.Add("ID", OleDbType.Numeric, 4).Value = 0;
OleDbDataReader myReader;
myReader = myCMD.ExecuteReader();
int x;
int count;
count = 0;
while (myReader.Read())

{
for (x = 0; x <= myReader.FieldCount - 1; x++)
Console.Write(myReader.GetValue(x) + " ");

     
Console.WriteLine();
count += 1;
}


MessageBox.Show(count + " Rows Returned.");
myReader.Close();
Oraclecon.Close();

 

原文:http://support.microsoft.com/default.aspx?scid=kb;en-us;309361

How To Use a DataReader Against an Oracle Stored Procedure in Visual C# .NET

这里仅仅摘出了必要的代码,调试通过! 这里需要注意的是不是所有类型的列都可以直接select回来,TIMESTAMP类型的列就需要先to_char, 不同的数据类型在oracle里和.net表示的方法不一样, 用oledb的时候又不会自动转换,所以对于特殊的类型会出错,稳妥起见所有特殊类型都以字符串返回.

create or replace procedure sp_sm_administrators_select(
admins_cursor out GLOBALPKG.t_cursor
)
is
begin
  open admins_cursor for
  select
      id,
      user_id,
      user_name,
      user_psw,
      to_char(create_date) as create_date,
      memo
  from sm_administrators
  order by create_date;
end sp_sm_administrators_select;

原创粉丝点击