日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 人文社科 > 生活经验 >内容正文

生活经验

Calling Oracle stored procedures from Microsoft.NET

發布時間:2023/11/27 生活经验 32 豆豆
生活随笔 收集整理的這篇文章主要介紹了 Calling Oracle stored procedures from Microsoft.NET 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

摘自:http://www.c-sharpcorner.com/UploadFile/john_charles/CallingOraclestoredproceduresfromMicrosoftdotNET06222007142805PM/CallingOraclestoredproceduresfromMicrosoftdotNET.aspx
Introduction

This article is intended to illustrate how to illustrate how to call Oracle stored procedures and functions from Microsoft.NET through the Microsoft.NET Oracle provider and its object model residing in the namespace System.Data.OracleClient. I will cover several possible scenarios with advanced examples.

Executing a stored procedure

Let's begin with definitions. A procedure is a module that performs one or more actions. A function is a module that returns a value and unlike procedures a call to a function can exist only as part of an executable such as an element in an expression or the value assigned as default in a declaration of a variable.

The first example illustrates how to call an Oracle procedure passing input parameters and retrieving value by output parameters. For all the examples, we're going to use the default database ORCL which comes with the Oracle database installation. The following code in Listing 1 shows how to create a procedure named count_emp_by_dept which receives as its input parameter the department number and sends as its output parameter the number of employees in this department.

create or replace procedure count_emp_by_dept(pin_deptno number, pout_count out number)
is
begin
?select
count(*) into pout_count
?from scott.emp
?where deptno=pin_deptno;
end count_emp_by_dept;



Listing 1: Creating the procedure? count_emp_by_dept.

Now let's create a console application and add a reference to the assembly System.Data.OracleClient.dll to your project.

The code for this example is illustrated in Listing 2. The first thing to do is to import the object's class residing in the namespace System.Data.OracleClient with the using directive. Then you must set up the parameters and finally call the procedure using ExecuteNonQuery method of the OracleCommand object.

Using System;

using System.Collections.Generic;

using System.Text;

using System.Data.OracleClient;

using System.Data;

?

namespace CallingOracleStoredProc

{

??? class Program

??? {

??????? static void Main(string[] args)

??????? {

??????????? using (OracleConnection objConn = new OracleConnection("Data Source=ORCL; User ID=scott; Password=tiger"))

??????????? {

??????????????? OracleCommand objCmd = new OracleCommand();

??????????????? objCmd.Connection = objConn;

??????????????? objCmd.CommandText = "count_emp_by_dept";

??????????????? objCmd.CommandType = CommandType.StoredProcedure;

??????????????? objCmd.Parameters.Add("pin_deptno", OracleType.Number).Value = 20;

??????????????? objCmd.Parameters.Add("pout_count", OracleType.Number).Direction = ParameterDirection.Output;

?

??????????????? try

??????????????? {

??????????????????? objConn.Open();

??????????????????? objCmd.ExecuteNonQuery();

??????????????????? System.Console.WriteLine("Number of employees in department 20 is {0}", objCmd.Parameters["pout_count"].Value);

??????????????? }

??????????????? catch (Exception ex)

??????????????? {

??????????????????? System.Console.WriteLine("Exception: {0}",ex.ToString());

??????????????? }

?

??????????????? objConn.Close();

??????????? }

??????? }

??? }

}

Listing 2: The application code calling the stored procedure.

Executing a function

As function is similar to procedures except they return a value, we need to set up a return parameter. Let's see the example.

The following code in Listing 3 shows how to create a function named get_count_emp_by_dept which receives as its input parameter the department number and returns the number of employees in this department. It's very similar to the former procedure in the previous section.

create or replace function get_count_emp_by_dept(pin_deptno number)
?return number
is
?
var_count number;
begin
?select
count(*) into var_count
?from scott.emp
?where deptno=pin_deptno;
?return
var_count;
end get_count_emp_by_dept;

Listing 3: Creating an Oracle function.

Now let's see in the Listing 4 the application code which calls the function. As you can see, we need to define a return parameter to get the returned value. The other part of the code is similar for calling a procedure.

using System;

using System.Collections.Generic;

using System.Text;

using System.Data.OracleClient;

using System.Data;

?

namespace CallingOracleStoredProc

{

??? class Program

??? {

??????? static void Main(string[] args)

??????? {

??????????? using (OracleConnection objConn = new OracleConnection("Data Source=ORCL; User ID=scott; Password=tiger"))

??????????? {

??????????????? OracleCommand objCmd = new OracleCommand();

??????????????? objCmd.Connection = objConn;

??????????????? objCmd.CommandText = "get_count_emp_by_dept";

??????????????? objCmd.CommandType = CommandType.StoredProcedure;

??????????????? objCmd.Parameters.Add("pin_deptno", OracleType.Number).Value = 20;

??????????????? objCmd.Parameters.Add("return_value", OracleType.Number).Direction = ParameterDirection.ReturnValue;

?

??????????????? try

??????????????? {

??????????????????? objConn.Open();

??????????????????? objCmd.ExecuteNonQuery();

??????????????????? System.Console.WriteLine("Number of employees in department 20 is {0}", objCmd.Parameters["return_value"].Value);

??????????????? }

??????????????? catch (Exception ex)

??????????????? {

??????????????????? System.Console.WriteLine("Exception: {0}",ex.ToString());

????????? ??????}

?

??????????????? objConn.Close();

??????????? }

??????? }

??? }

}

Listing 4: The application code calling the function.

Working with cursors

You can use the REF CURSOR data type to work with Oracle result set. To retrieve the result set, you must define a REF CURSOR output parameter in a procedure or a function to pass the cursor back to your application.

Now we're going to define a procedure which opens and sends a cursor variable to our application.

Let's define the package and procedure header as shown in Listing 5.

create or replace package human_resources
as
?type t_cursor is ref cursor;
?procedure
get_employee(cur_employees out t_cursor);
end human_resources;

Listing 5: Creation of the package human_resources and the procedure get_employee.

And now the package definition as shown in Listing 6.

create or replace package body human_resources
as
?procedure
get_employee(cur_employees out t_cursor)
?is
?begin
? open
cur_employees for select * from emp;
?end get_employee;
end human_resources;

Listing 6. The creation of the package body.

Now let's see in Listing 7 the application code calling the procedure inside the package. See the name syntax for calling the procedure contained within a package [package_name].[procedure_name]. In order to get a cursor, you need to define a cursor parameter with the ParameterDirection set up to Output and finally call the ExecuteReader method in the OracleCommand instance.

Using System;

using System.Collections.Generic;

using System.Text;

using System.Data.OracleClient;

using System.Data;

?

namespace CallingOracleStoredProc

{

??? class Program

??? {

??????? private static void prvPrintReader(OracleDataReader objReader)

??????? {

??????????? for (int i = 0; i < objReader.FieldCount; i++)

??????????? {

????????????? ??System.Console.Write("{0}\t",objReader.GetName(i));

??????????? }

??????????? System.Console.Write("\n");

?

??????????? while (objReader.Read())

??????????? {

??????????????? for (int i = 0; i < objReader.FieldCount; i++)

??????????????? {

?????????????? ?????System.Console.Write("{0}\t", objReader[i].ToString());

??????????????? }

??????????????? System.Console.Write("\n");

??????????? }

??????? }

?

??????? static void Main(string[] args)

??????? {

??????????? using (OracleConnection objConn = new OracleConnection("Data Source=ORCL; User ID=scott; Password=tiger"))

??????????? {

??????????????? OracleCommand objCmd = new OracleCommand();

??????????????? objCmd.Connection = objConn;

??????????????? objCmd.CommandText = "human_resources.get_employee";

?????? ?????????objCmd.CommandType = CommandType.StoredProcedure;

??????????????? objCmd.Parameters.Add("cur_employees", OracleType.Cursor).Direction = ParameterDirection.Output;

?

??????????????? try

??????????????? {

??????????????????? objConn.Open();

???????? ???????????OracleDataReader objReader = objCmd.ExecuteReader();

??????????????????? prvPrintReader(objReader);

??????????????? }

??????????????? catch (Exception ex)

??????????????? {

??????????????????? System.Console.WriteLine("Exception: {0}",ex.ToString());

??????????????? }

?

??????????????? objConn.Close();

??????????? }

??????? }

?

??? }

}

Listing 7: The application code.

If the procedure returns more than one cursor, the DataReader object accesses them by calling the NextResult method to advance the next cursor.

Let's see the following example.

Listing 8 shows how to create the package header.

create or replace package human_resources
as
?type t_cursor is ref cursor;
?procedure
get_employee_department(cur_employees out t_cursor, cur_departments out t_cursor);
end human_resources;

Listing 8: Package reader.

The package body is shown in Listing 9.

create or replace package body human_resources
as
?procedure
get_employee_department(cur_employees out t_cursor, cur_departments out t_cursor)
?is
?begin
? open
cur_employees for select * from emp;
? open
cur_departments for select * from dept;
?end
get_employee_department;
end human_resources;

Listing 9: Creation of the package body.

Let's see the application code in Listing 10.

using System;

using System.Collections.Generic;

using System.Text;

using System.Data.OracleClient;

using System.Data;

?

namespace CallingOracleStoredProc

{

??? class Program

??? {

??????? private static void prvPrintReader(OracleDataReader objReader)

??????? {

??????????? for (int i = 0; i < objReader.FieldCount; i++)

??????????? {

??????????????? System.Console.Write("{0}\t",objReader.GetName(i));

??????????? }

??????????? System.Console.Write("\n");

?

??????????? while (objReader.Read())

??????????? {

??????????????? for (int i = 0; i < objReader.FieldCount; i++)

??????????????? {

??????????????????? System.Console.Write("{0}\t", objReader[i].ToString());

??????????????? }

??????????????? System.Console.Write("\n");

??????????? }

??????? }

?

??????? static void Main(string[] args)

?????? ?{

??????????? using (OracleConnection objConn = new OracleConnection("Data Source=ORCL; User ID=scott; Password=tiger"))

??????????? {

??????????????? OracleCommand objCmd = new OracleCommand();

??????????????? objCmd.Connection = objConn;

?????????????? ?objCmd.CommandText = "human_resources.get_employee_department";

??????????????? objCmd.CommandType = CommandType.StoredProcedure;

??????????????? objCmd.Parameters.Add("cur_employees", OracleType.Cursor).Direction = ParameterDirection.Output;

??????????? ????objCmd.Parameters.Add("cur_departments", OracleType.Cursor).Direction = ParameterDirection.Output;

?

??????????????? try

??????????????? {

??????????????????? objConn.Open();

??????????????????? OracleDataReader objReader = objCmd.ExecuteReader();

???? ???????????????prvPrintReader(objReader);

??????????????????? objReader.NextResult();

??????????????????? prvPrintReader(objReader);

??????????????? }

??????????????? catch (Exception ex)

??????????????? {

??????????????????? System.Console.WriteLine("Exception: {0}",ex.ToString());

??????????????? }

?

??????????????? objConn.Close();

??????????? }

??????? }

?

??? }

}


Listing 10: The application code.

Working with DataSet and DataAdapter

The final example shows how to fill and update a DataSet object through a DataAdapter object.

The first thing to do is create four CRUD procedure to the emp table. ?Listing 11 shows how to create the package header.

create or replace package human_resources
as
?
type t_cursor is ref cursor;
?procedure
select_employee(cur_employees out t_cursor);
?procedure insert_employee(p_empno number, p_ename varchar2, p_job varchar2, p_mgr number, p_hiredate date, p_sal number, p_comm number, p_deptno number);
?procedure update_employee(p_empno number, p_ename varchar2, p_job varchar2, p_mgr number, p_hiredate date, p_sal number, p_comm number, p_deptno number);
?procedure delete_employee(p_empno number);
end
human_resources;

Listing 11: The creation of the package header.

Now let's define the package body as shown in Listing 12

create or replace package body human_resources
as
?procedure
select_employee(cur_employees out t_cursor)
?is
?begin
?? open
cur_employees for select empno, ename, job, mgr, hiredate, sal, comm, deptno from emp;
?end select_employee;
?procedure insert_employee(p_empno number, p_ename varchar2, p_job varchar2, p_mgr number, p_hiredate date, p_sal number, p_comm number, p_deptno number)
?is
?begin
?? update
emp
?? set ename=p_ename, job=p_job, mgr=p_mgr, hiredate=p_hiredate, sal=p_sal, comm=p_comm, deptno=p_deptno
?? where empno=p_empno;
?end insert_employee;
?procedure update_employee(p_empno number, p_ename varchar2, p_job varchar2, p_mgr number, p_hiredate date, p_sal number, p_comm number, p_deptno number)
?is
?begin
?? insert into
emp
?? values(p_empno,p_ename,p_job,p_mgr,p_hiredate,p_sal,p_comm,p_deptno);
?end update_employee;
?procedure delete_employee(p_empno number)
?is
?begin
??? delete from
emp
??? where empno=p_empno;
?end delete_employee;
end human_resources;

Listing 12: The package body creation.

And finally, let's see the application code in Listing 13. As you can see, to fill the data table, we need to define the CRUD (create, read, update, delete) operations through the OracleCommand and associate it to the DataAdapter. I fill the data table, and print out a message with the number of employees so far, and then add a new row representing one employee entity.

using System;

using System.Collections.Generic;

using System.Text;

using System.Data.OracleClient;

using System.Data;

?

namespace CallingOracleStoredProc

{

??? class Program

??? {

??????? static void Main(string[] args)

??????? {

??????????? using (OracleConnection objConn = new OracleConnection("Data Source=ORCL; User ID=scott; Password=tiger"))

??????????? {

??????????????? OracleDataAdapter objAdapter = new OracleDataAdapter();

?

??????????????? OracleCommand objSelectCmd = new OracleCommand();

??????????????? objSelectCmd.Connection = objConn;

??????????????? objSelectCmd.CommandText = "human_resources.select_employee";

??????????????? objSelectCmd.CommandType = CommandType.StoredProcedure;

??????????????? objSelectCmd.Parameters.Add("cur_employees", OracleType.Cursor).Direction = ParameterDirection.Output;

??????????????? objAdapter.SelectCommand = objSelectCmd;

?

??????????????? OracleCommand objInsertCmd = new OracleCommand();

??????????????? objInsertCmd.Connection = objConn;

??????????????? objInsertCmd.CommandText = "human_resources.insert_employee";

??????????????? objInsertCmd.CommandType = CommandType.StoredProcedure;

??????????????? objInsertCmd.Parameters.Add("p_empno", OracleType.Number, 4, "empno");

??????????????? objInsertCmd.Parameters.Add("p_ename", OracleType.VarChar, 10, "ename");

??????????????? objInsertCmd.Parameters.Add("p_job", OracleType.VarChar, 9, "job");

??????????????? objInsertCmd.Parameters.Add("p_mgr", OracleType.Number, 4, "mgr");

??????????????? objInsertCmd.Parameters.Add("p_hiredate", OracleType.DateTime,12, "hiredate");

??????????????? objInsertCmd.Parameters.Add("p_sal", OracleType.Number, 7, "sal");

??????????????? objInsertCmd.Parameters.Add("p_comm", OracleType.Number, 7, "comm");

??????????????? objInsertCmd.Parameters.Add("p_deptno", OracleType.Number, 7, "deptno");

??????????????? objAdapter.InsertCommand = objInsertCmd;

?

??????????????? OracleCommand objUpdateCmd = new OracleCommand();

??????????????? objUpdateCmd.Connection = objConn;

??????????????? objUpdateCmd.CommandText = "human_resources.update_employee";

??????????????? objUpdateCmd.CommandType = CommandType.StoredProcedure;

??????????????? objUpdateCmd.Parameters.Add("p_empno", OracleType.Number, 4, "empno");

??????????????? objUpdateCmd.Parameters.Add("p_ename", OracleType.VarChar, 10, "ename");

??????????????? objUpdateCmd.Parameters.Add("p_job", OracleType.VarChar, 9, "job");

??????????????? objUpdateCmd.Parameters.Add("p_mgr", OracleType.Number, 4, "mgr");

??????????????? objUpdateCmd.Parameters.Add("p_hiredate", OracleType.DateTime, 10, "hiredate");

??????????????? objUpdateCmd.Parameters.Add("p_sal", OracleType.Number, 7, "sal");

??????????????? objUpdateCmd.Parameters.Add("p_comm", OracleType.Number, 7, "comm");

??????????????? objUpdateCmd.Parameters.Add("p_deptno", OracleType.Number, 7, "deptno");

??????????????? objAdapter.UpdateCommand = objUpdateCmd;

?

??????????????? OracleCommand objDeleteCmd = new OracleCommand();

??????????????? objDeleteCmd.Connection = objConn;

??????????????? objDeleteCmd.CommandText = "human_resources.delete_employee";

??????????????? objDeleteCmd.CommandType = CommandType.StoredProcedure;

??????????????? objDeleteCmd.Parameters.Add("p_empno", OracleType.Number, 4, "empno");

??????????????? objAdapter.DeleteCommand = objDeleteCmd;

?

??????????????? try

??????????????? {

??????????????????? DataTable dtEmp = new DataTable();

??????????????????? objAdapter.Fill(dtEmp);

?

????? ??????????????System.Console.WriteLine("Employee count = {0}", dtEmp.Rows.Count );

??????????????????? dtEmp.Rows.Add(7935, "John", "Manager", 7782, DateTime.Now,1300,0,10);

?

??????????????????? objAdapter.Update(dtEmp);

?

??????????????? }

??????????????? catch (Exception ex)

??????????????? {

??????????????????? System.Console.WriteLine("Exception: {0}",ex.ToString());

??????????????? }

?

??????????????? objConn.Close();

??????????? }

??????? }

?

??? }

}

Listing 12: The application code.

轉載于:https://www.cnblogs.com/NRabbit/archive/2009/07/10/1736182.html

總結

以上是生活随笔為你收集整理的Calling Oracle stored procedures from Microsoft.NET的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。

主站蜘蛛池模板: 欧美人xxxx| 欧美一区二区精品 | 人与动物av| 日本少妇高潮 | 韩国三级hd中文字幕的背景音乐 | 一区二区三区视频免费 | 欧美狠狠操 | 日本高清视频在线观看 | 亚洲av综合一区二区 | 中文字幕成人在线视频 | 激情四射网站 | 超在线视频| 96久久 | 看黄色一级片 | 日韩在线观看网址 | www.五月激情 | 青青操视频在线观看 | 亚洲成人午夜在线 | 亚洲精品一 | 体内射精一区二区 | 九九碰| 全部孕妇毛片丰满孕妇孕交 | 亚洲国产日韩欧美在线观看 | 影音先锋制服丝袜 | 国产精品嫩草影院桃色 | 国产黑丝一区二区 | www国产黄色 | 二级黄色录像 | 日日撸夜夜撸 | 成人在线观看免费高清 | 麻豆影音先锋 | 日韩在线视 | 午夜精品免费观看 | 成人高潮片免费视频 | 久久黄色一级片 | 911国产| 97成人精品视频在线观看 | 久久国产网 | 伊人久久爱| 波多野结衣亚洲天堂 | 午夜精品欧美 | 精品视频在线播放 | 超碰在线网站 | 人妻丰满熟妇av无码区不卡 | 青青色在线观看 | 亚洲自拍天堂 | 亚洲天堂五月天 | 成人免费黄色小视频 | 久久久久久久中文字幕 | 人人妻人人爽欧美成人一区 | 精品黄色av| 国产美女极度色诱视频www | 国产视频123 | 成人激情开心 | 亚洲AV无码国产精品播放在线 | 色999日韩 | 亚洲巨乳av | 一二区视频| 色婷婷91| 欧美鲁鲁 | 国产9区| 播放灌醉水嫩大学生国内精品 | www.色亚洲| 永久免费av | 欧美亚洲色综久久精品国产 | 亚洲综合视频在线 | 简单av网| 一区二区三区久久久久 | 成人免费网站视频 | 日日碰碰 | 午夜精品久久久久久久久久久久 | 免费网站91| 四虎国产精品成人免费入口 | 欧美cccc极品丰满hd | 陪读偷伦初尝小说 | 日本在线免费视频 | 亚洲一区二区三区四区五区xx | 啪啪福利社| 久久久久美女 | 精品欧美乱码久久久久久1区2区 | 咪咪成人网 | 极品白嫩的小少妇 | 亚洲25p| 国产91精品久久久久 | 亚洲精品免费在线 | 毛片久久久久久 | 操丰满女人| 少女忠诚电影高清免费 | 97精品久久| 草草免费视频 | jvid乐乐| 国产精品探花一区二区在线观看 | 国产精品伦一区二区三区 | 人人插人人 | 亚洲欧美自拍另类 | 在线视频国产一区 | 爱av在线 | 久久这里只有精品首页 | 三级免费网址 |