使用DataReader操作从Oracle存储过程返回的游标

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

<script type="text/javascript">function loadTOCNode(){}</script>
Article ID:309361
Last Review:July 15, 2004
Revision:2.2
This article was previously published under Q309361
<script type="text/javascript"> var sectionFilter = "type != 'notice' && type != 'securedata' && type != 'querywords'"; var tocArrow = "/library/images/support/kbgraphics/public/en-us/downarrow.gif"; var depthLimit = 10; var depth3Limit = 10; var depth4Limit = 5; var depth5Limit = 3; var tocEntryMinimum = 1; </script> <script src="http://support.microsoft.com/common/script/gsfx/kbtoc.js?11" type="text/javascript"></script>

SUMMARY

<script type="text/javascript">loadTOCNode(1, 'summary');</script>
This step-by-step article uses the DataReader object to retrieve data from an Oracle stored procedure. You can use the DataReader to retrieve a read-only, forward-only stream of data from a database. Using the DataReader can increase application performance and reduce system overhead because only one row is kept in memory.

Back to the top

Requirements

<script type="text/javascript">loadTOCNode(2, 'summary');</script> The following list outlines the recommended hardware, software, network infrastructure, and service packs that you need:
Microsoft Windows 2000 Professional, Windows 2000 Server, Windows 2000 Advanced Server, or Windows NT 4.0 Server
Microsoft Visual Studio .NET
This article assumes that you are familiar with the following topics:
Visual C# .NET
ADO.NET fundamentals and syntax

Back to the top

Create the Oracle Tables

<script type="text/javascript">loadTOCNode(2, 'summary');</script> This sample uses tables that are defined in the Oracle Scott/Tiger schema. The Oracle Scott/Tiger schema is included with the default Oracle installation. If this schema does not exist, you must run the following table and insert scripts for the tables:
   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);
				

Back to the top

Create the Oracle Packages

<script type="text/javascript">loadTOCNode(2, 'summary');</script> Create the following Oracle package on the Oracle server:
CREATE OR REPLACE PACKAGE curspkg_join AS 
   	TYPE t_cursor IS REF CURSOR ; 
   	Procedure open_join_cursor1 (n_EMPNO IN NUMBER, io_cursor IN OUT t_cursor); 
   END curspkg_join;
   / 
				
Create the following Oracle package body on the Oracle server:
   CREATE OR REPLACE PACKAGE BODY curspkg_join AS
   Procedure open_join_cursor1 (n_EMPNO IN NUMBER, 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;
   / 
				

Back to the top

Create the Visual C# .NET Application

<script type="text/javascript">loadTOCNode(2, 'summary');</script>
1.Create a new Visual C# Windows Application project. Form1 is added to the project by default.
2.Add the following code to the top of the Code window:
using System.Data.OleDb;
					
3.Add the following code to the Form_Load event of Form1:
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();
					
4.Modify the OleDbConnection string as appropriate for your environment.
5.Press the F5 key to compile and run the application. Notice that the data from the Oracle stored procedure appears in the Debug window, and a row count appears in a message box.

Back to the top

Additional Information

<script type="text/javascript">loadTOCNode(2, 'summary');</script> Notice that the code loops through the DataReader:
while (myReader.Read())
				
This is because the DataReader reads only one line at a time.

Back to the top

REFERENCES

<script type="text/javascript">loadTOCNode(1, 'references');</script> For additional information%1, click the article number%2 below to view the article%2 in the Microsoft Knowledge Base:
176086 (http://support.microsoft.com/kb/176086/EN-US/) How To Retrieve Recordsets from Oracle Stored Procedures Using ADO
For more information about the DataReader, refer to the following topic in the Microsoft .NET Software Development Kit (SDK) documentation: Retrieving Data Using the DataReader http://msdn.microsoft.com/library/default.asp?url=/library/en-us/cpguide/html/cpcontheadonetdatareader.asp
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值