在.NET中调用Oracle9i存储过程经验总结-阿里云开发者社区

开发者社区> 老朱教授> 正文

在.NET中调用Oracle9i存储过程经验总结

简介:
+关注继续查看
None.gif在.NET中调用Oracle9i存储过程经验总结
None.gif
None.gif
None.gif在.NET中调用Oracle9i存储过程可以用多个数据提供者,比如OralceClient,OleDb。本文将用OralceClient为例。.NET语言用C#。
None.gif
None.gif
None.gif
None.gif一、调用一个带输入、输出参数的存储过程
None.gif
None.gif首先,在Oralce中创建存储过程如下:
None.gif
None.gifcreate or replace procedure GetRecords(name_out out varchar2,age_in in varchar2) as
None.gif
None.gifbegin
None.gif
None.gifselect NAME into name_out from test where AGE = age_in;
None.gif
None.gifend;
None.gif
None.gif
None.gif
None.gif然后,在C#项目中作如下调用:
None.gif
None.gifstring connectionString = "Data Source=YXZHANG;User ID=YXZHANG;Password=YXZHANG";
None.gif
None.gifstring queryString = "getrecords";
None.gif
None.gif OracleConnection cn = new OracleConnection(connectionString);
None.gif
None.gif OracleCommand cmd = new OracleCommand(queryString,cn);
None.gif
None.gif cmd.CommandType = CommandType.StoredProcedure;
None.gif
None.gif cmd.Parameters.Add("name_out",OracleType.VarChar,20);
None.gif
None.gif cmd.Parameters["name_out"].Direction = ParameterDirection.Output;
None.gif
None.gif cmd.Parameters.Add("age_in",21);
None.gif
None.gif
None.gif
None.giftry
None.gif
ExpandedBlockStart.gifContractedBlock.gifdot.gif{
InBlock.gif
InBlock.gif cn.Open();
InBlock.gif
InBlock.gif cmd.ExecuteNonQuery();
InBlock.gif
InBlock.gif Console.WriteLine("Name is:{0}",cmd.Parameters["name_out"].Value.ToString());
InBlock.gif
InBlock.gif cn.Close();
InBlock.gif
ExpandedBlockEnd.gif }

None.gif
None.gifcatch( OracleException ex )
None.gif
ExpandedBlockStart.gifContractedBlock.gifdot.gif{
InBlock.gif
InBlock.gif Console.WriteLine("Exception occurred!");
InBlock.gif
InBlock.gif Console.WriteLine("The exception message is:{0}",ex.Message.ToString());
InBlock.gif
ExpandedBlockEnd.gif }

None.gif
None.giffinally
None.gif
ExpandedBlockStart.gifContractedBlock.gifdot.gif{
InBlock.gif
InBlock.gif Console.WriteLine("------------------End-------------------");
InBlock.gif
ExpandedBlockEnd.gif }

None.gif
None.gif小结:
None.gif
None.gif以上是很平常的调用方法,但是我在做这个示例程序的时候,却不是那么一帆风顺。这里要指出OracleClient的一个不尽如人意之处,就是对于参数名称,必须和存储过程定义中的参数同名,否则就会报错。比如将代码中的“name_out”改为别的名称的话,就会报异常。但我试着用OleDb代替OralceClient,却没有这个问题。不知道在新版本的数据提供程序中会否改进这一点?
None.gif
None.gif二、调用不返回数据的存储过程
None.gif
None.gif首先,在Oralce中创建存储过程如下:
None.gif
None.gifcreate or replace procedure insertRecord(UserID in varchar2,
None.gif
None.gifUserName in varchar2,
None.gif UserAge in varchar2) is
None.gif begin
None.gif insert into test values (UserID, UserName, UserAge);
None.gif end;
None.gif
None.gif
None.gif
None.gif然后,在C#项目中作如下调用:
None.gif
None.gif
None.gif
None.gifstring connectionString = "Data Source=YXZHANG;User ID=YXZHANG;Password=YXZHANG";
None.gif
None.gifstring queryString = "insertRecord";
None.gif
None.gifOracleConnection cn = new OracleConnection(connectionString);
None.gif
None.gifOracleCommand cmd = new OracleCommand(queryString,cn);
None.gif
None.gifcmd.CommandType = CommandType.StoredProcedure;
None.gif
None.gif cmd.Parameters.Add("UserID","007");
None.gif
None.gif cmd.Parameters.Add("UserName","Dell");
None.gif
None.gif cmd.Parameters.Add("UserAge","40");
None.gif
None.gif
None.gif
None.giftry
None.gif
ExpandedBlockStart.gifContractedBlock.gifdot.gif{
InBlock.gif
InBlock.gif cn.Open();
InBlock.gif
InBlock.gif cmd.ExecuteNonQuery();
InBlock.gif
InBlock.gif Console.WriteLine("Record inserted!");
InBlock.gif
InBlock.gif cn.Close();
InBlock.gif
ExpandedBlockEnd.gif }

None.gif
None.gifcatch( OracleException ex )
None.gif
ExpandedBlockStart.gifContractedBlock.gifdot.gif{
InBlock.gif
InBlock.gif Console.WriteLine("Exception occurred!");
InBlock.gif
InBlock.gif Console.WriteLine("The exception message is:{0}",ex.Message.ToString());
InBlock.gif
ExpandedBlockEnd.gif }

None.gif
None.giffinally
None.gif
ExpandedBlockStart.gifContractedBlock.gifdot.gif{
InBlock.gif
InBlock.gif Console.WriteLine("------------------End-------------------");
InBlock.gif
ExpandedBlockEnd.gif }

None.gif
None.gif
None.gif
None.gif
None.gif
None.gif小结:
None.gif
None.gif 不返回数据的存储过程一般有Delete, Insert, Update等。虽然它们的SQL语句不同,但是调用方面都是一样的。只要传入正确的参数,一般不会出什么问题。不过还是要注意,在使用OralceClient作为数据提供者的时候,参数名称一定要和存储过程定义中的一致!至于参数添加的顺序倒是无所谓的,因为有参数名称作为区别。
None.gif
None.gif
None.gif
None.gif三、IDENTITY 和 SEQUENCE
None.gif
None.gifSql Server中,定义一个列为递增列很容易,但我在Oracle中却怎么也找不到设置的方法。不过查了点资料后知道Oracle中有个叫Sequence的对象,产生一个唯一的序列号,类似于Sql Server中的IDENTITY。于是,我做了如下实验:
None.gif
None.gif
None.gif
None.gif首先,在Oracle中创建了一个名为TEST_SEQ的Sequence对象,SQL语句如下:
None.gif
None.gifcreate sequence TEST_SEQ
None.gif
None.gifminvalue 100
None.gif
None.gifmaxvalue 999
None.gif
None.gifstart with 102
None.gif
None.gifincrement by 1
None.gif
None.gifnocache;
None.gif
None.gif
None.gif
None.gif语法应该是比较易懂的,最小最大值分别用minvalue,maxvalue表示,初始值是102(这个数字是动态变化的,我创建的时候设的是100,后因插入了2条数据后就自动增加了2),increment当然就是步长了。在PL/SQL中可以用test_seq.nextval访问下一个序列号,用test_seq.currval访问当前的序列号。
None.gif
None.gif
None.gif
None.gif 定义完了Sequence,接下来就是创建一个存储过程InsertRecordWithSequence:
None.gif
None.gif--这次我修改了test表的定义,和前面的示例不同。其中,UserID是PK。
None.gif
None.gifcreate or replace procedure InsertRecordWithSequence(UserID out number,
None.gif
None.gif UserName in varchar2,
None.gif
None.gif UserAge in number)
None.gif
None.gifis
None.gif
None.gifbegin
None.gif
None.gif insert into test(id, name, age) --插入一条记录,PK值从Sequece获取
None.gif
None.gif values(test_seq.nextval, UserName, UserAge);
None.gif
ExpandedBlockStart.gifContractedBlock.gif/**//*返回PK值。注意Dual表的用法*/
None.gif
None.gifselect test_seq.currval into UserID from dual;
None.gif
None.gifend InsertRecordWithSequence;
None.gif
None.gif
None.gif
None.gif接下来,就是在C#中进行调用了:
None.gif
None.gifstring connectionString = "Data Source=YXZHANG;User ID=YXZHANG;Password=YXZHANG";
None.gif
None.gifstring queryString = "InsertRecordWithSequence";
None.gif
None.gif OracleConnection cn = new OracleConnection(connectionString);
None.gif
None.gif OracleCommand cmd = new OracleCommand(queryString,cn);
None.gif
None.gif cmd.CommandType = CommandType.StoredProcedure;
None.gif
None.gif cmd.Parameters.Add("UserID",OracleType.Number);
None.gif
None.gif cmd.Parameters["UserID"].Direction = ParameterDirection.Output;
None.gif
None.gif cmd.Parameters.Add("UserName","Jack");
None.gif
None.gif cmd.Parameters.Add("UserAge",40);
None.gif
None.gif
None.gif
None.giftry
None.gif
ExpandedBlockStart.gifContractedBlock.gifdot.gif{
InBlock.gif
InBlock.gif cn.Open();
InBlock.gif
InBlock.gifint rowAffected = cmd.ExecuteNonQuery();
InBlock.gif
InBlock.gif Console.WriteLine("{0}行已插入。",rowAffected);
InBlock.gif
InBlock.gif Console.WriteLine("插入行的ID为:{0}",cmd.Parameters["UserID"].Value.ToString());
InBlock.gif
InBlock.gif cn.Close();
InBlock.gif
ExpandedBlockEnd.gif }

None.gif
None.gifcatch( OracleException ex )
None.gif
ExpandedBlockStart.gifContractedBlock.gifdot.gif{
InBlock.gif
InBlock.gif Console.WriteLine("Exception occurred!");
InBlock.gif
InBlock.gif Console.WriteLine("The exception message is:{0}",ex.Message.ToString());
InBlock.gif
ExpandedBlockEnd.gif }

None.gif
None.giffinally
None.gif
ExpandedBlockStart.gifContractedBlock.gifdot.gif{
InBlock.gif
InBlock.gif Console.WriteLine("------------------End-------------------");
InBlock.gif
ExpandedBlockEnd.gif }

None.gif
None.gif小结:
None.gif
None.gif 使用Sequece对象可以很容易地创建唯一序列,在存储过程中的调用也十分方便,只要sequence_name.nextval以及sequence.currval就能得到下一个以及当前的序列值。倒是Dual表值得注意。
None.gif
None.gif
None.gif
None.gif四、使用DataReader读取返回的结果集
None.gif
None.gif为了让存储过程返回结果集,必须定义一个游标变量作为输出参数。这和Sql Server中有着很大的不同!并且还要用到Oracle中“包”(Package)的概念,似乎有点繁琐,但熟悉后也会觉得很方便。
None.gif
None.gif关于“包”的概念,有很多内容可以参考,在此就不赘述了。首先,我创建了一个名为TestPackage的包,包头是这么定义的:
None.gif
None.gif create or replace package TestPackage is
None.gif
None.gif type mycursor is ref cursor; -- 定义游标变量
None.gif
None.gif
None.gif
None.gif procedure GetRecords(ret_cursor out mycursor); -- 定义过程,用游标变量作为返回参数
None.gif
None.gifend TestPackage;
None.gif
None.gif
None.gif
None.gif
None.gif
None.gif包体是这么定义的:
None.gif
None.gifcreate or replace package body TestPackage is
None.gif
ExpandedBlockStart.gifContractedBlock.gif/**//*过程体*/
None.gif
None.gif procedure GetRecords(ret_cursor out mycursor) as
None.gif
None.gif begin
None.gif
None.gif open ret_cursor for select * from test;
None.gif
None.gif end GetRecords;
None.gif
None.gifend TestPackage;
None.gif
None.gif数据库中调试方法:
None.gif在包头文件中选择相映的过程,右键选择text,f9执行后,点value后面的dot.gif查看数据集的结果。
None.gif
None.gif
None.gif
None.gif 已经万事具备了,让我们前台调用试试:
None.gif
None.gifstring connectionString = "Data Source=YXZHANG;User ID=YXZHANG;Password=YXZHANG";
None.gif
None.gifstring queryString = "TestPackage.GetRecords"; //注意调用方法
None.gif

None.gif OracleConnection cn = new OracleConnection(connectionString);
None.gif
None.gif OracleCommand cmd = new OracleCommand(queryString,cn);
None.gif
None.gif cmd.CommandType = CommandType.StoredProcedure;
None.gif
None.gif cmd.Parameters.Add("ret_cursor",OracleType.Cursor); //注意这里的类型
None.gif

None.gif cmd.Parameters["ret_cursor"].Direction = ParameterDirection.Output;
None.gif
None.gif
None.gif
None.giftry
None.gif
ExpandedBlockStart.gifContractedBlock.gifdot.gif{
InBlock.gif
InBlock.gif cn.Open();
InBlock.gif
InBlock.gif OracleDataReader dr = cmd.ExecuteReader();
InBlock.gif
InBlock.gifint i = 1;
InBlock.gif
InBlock.gifwhile( dr.Read() )
InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gifdot.gif{
InBlock.gif
InBlock.gif Console.WriteLine("Record {0}:",i++);
InBlock.gif
InBlock.gif Console.WriteLine("ID:{0} Name:{1} Age:{2}",
InBlock.gif
InBlock.gifdr.GetOracleNumber(0),
InBlock.gif
InBlock.gifdr.GetOracleString(1),
InBlock.gif
InBlock.gifdr.GetOracleNumber(2));
InBlock.gif
InBlock.gif Console.WriteLine();
InBlock.gif
ExpandedSubBlockEnd.gif }

InBlock.gif
InBlock.gif dr.Close(); //用完DataReader对象要记得及时关闭
InBlock.gif

InBlock.gif cn.Close(); //DataReader对象未关闭之前,不能关闭连接
InBlock.gif

ExpandedBlockEnd.gif }

None.gif
None.gifcatch( OracleException ex )
None.gif
ExpandedBlockStart.gifContractedBlock.gifdot.gif{
InBlock.gif
InBlock.gif Console.WriteLine("Exception occurred!");
InBlock.gif
InBlock.gif Console.WriteLine("The exception message is:{0}",ex.Message.ToString());
InBlock.gif
ExpandedBlockEnd.gif }

None.gif
None.giffinally
None.gif
ExpandedBlockStart.gifContractedBlock.gifdot.gif{
InBlock.gif
InBlock.gif Console.WriteLine("------------------End-------------------");
InBlock.gif
ExpandedBlockEnd.gif }

None.gif
None.gif
None.gif
None.gif请看结果:
None.gif
None.gifRecord 1:
None.gif
None.gifID:100 Name:Tony Age:23
None.gif
None.gif
None.gif
None.gifRecord 2:
None.gif
None.gifID:101 Name:Jack Age:40
None.gif
None.gif
None.gif
None.gif------------------End-------------------
None.gif
None.gif
None.gif
None.gif小结:
None.gif
None.gif 包是Oracle特有的概念,Sql Server中找不到相匹配的东西。在我看来,包有点像VC++的类,包头就是.h文件,包体就是.cpp文件。包头只负责定义,包体则负责具体实现。如果包返回多个游标,则DataReader会按照您向参数集合中添加它们的顺序来访问这些游标,而不是按照它们在过程中出现的顺序来访问。可使用DataReader的NextResult()方法前进到下一个游标。
None.gif
None.gif
None.gif
None.gif五、用返回的结果集填充DataSet
None.gif
None.gifADO.NET舍弃了原来ADO中RecordSet的概念,而使用全新的DataSet来替代。DataSet可以提供更加强大的功能!有了前面的基础,利用ADO.NET从Oralce返回DataSet也是非常简单的。主要思想就是用DataAdapter接收返回的游标,再用DataAdapter的Fill()方法填充数据集。在下面的例子中,我仍用了前一个例子中建好的TestPackage包。
None.gif
None.gif
None.gif
None.gif下面是前台调用主要代码:
None.gif
None.gifstring connectionString = "Data Source=YXZHANG;User ID=YXZHANG;Password=YXZHANG";
None.gif
None.gifstring queryString = "TestPackage.GetRecords";
None.gif
None.gif OracleConnection cn = new OracleConnection(connectionString);
None.gif
None.gif OracleCommand cmd = new OracleCommand(queryString,cn);
None.gif
None.gif cmd.CommandType = CommandType.StoredProcedure;
None.gif
None.gif cmd.Parameters.Add("ret_cursor",OracleType.Cursor);
None.gif
None.gif cmd.Parameters["ret_cursor"].Direction = ParameterDirection.Output;
None.gif
None.gif
None.gif
None.giftry
None.gif
ExpandedBlockStart.gifContractedBlock.gifdot.gif{
InBlock.gif
InBlock.gif cn.Open();
InBlock.gif
InBlock.gif OracleDataAdapter da = new OracleDataAdapter(cmd);
InBlock.gif
InBlock.gif DataSet ds = new DataSet();
InBlock.gif
InBlock.gif da.Fill(ds,"TestTable");
InBlock.gif
InBlock.gif cn.Close();
InBlock.gif
InBlock.giffor( int i = 0;i <= ds.Tables["TestTable"].Rows.Count-1;i++ )
InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gifdot.gif{
InBlock.gif
InBlock.gifstring id = ds.Tables["TestTable"].Rows[i]["ID"].ToString();
InBlock.gif
InBlock.gifstring name = ds.Tables["TestTable"].Rows[i]["NAME"].ToString();
InBlock.gif
InBlock.gifstring age = ds.Tables["TestTable"].Rows[i]["AGE"].ToString();
InBlock.gif
InBlock.gif Console.WriteLine("Record {0}:",i+1);
InBlock.gif
InBlock.gif Console.WriteLine("ID:{0}\tName:{1}\tAge:{2}\n",id,name,age);
InBlock.gif
ExpandedSubBlockEnd.gif }

InBlock.gif
ExpandedBlockEnd.gif }

None.gif
None.gifcatch( OracleException ex )
None.gif
ExpandedBlockStart.gifContractedBlock.gifdot.gif{
InBlock.gif
InBlock.gif Console.WriteLine("Exception occurred!");
InBlock.gif
InBlock.gif Console.WriteLine("The exception message is:{0}",ex.Message.ToString());
InBlock.gif
ExpandedBlockEnd.gif }

None.gif
None.giffinally
None.gif
ExpandedBlockStart.gifContractedBlock.gifdot.gif{
InBlock.gif
InBlock.gif Console.WriteLine("------------------End-------------------");
InBlock.gif
ExpandedBlockEnd.gif }

None.gif
None.gif小结:
None.gif
None.gif 程序调用后的结果和刚才用DataReader调用的结果一样。这里只说明怎样利用ADO.NET调用Oracle存储过程,以及怎样填充至数据集中。至于怎样操纵DataSet,不是本文的讨论范围。有兴趣的读者可以参考MSDN以及相关书籍。
None.gif
None.gif
None.gif
None.gif六、用DataAdapter更新数据库
None.gif
None.gif通常用DataAdapter取回DataSet,将会对DataSet进行一些修改,继而更新数据库(如果只是为了获取数据,微软推荐使用DataReader代替DataSet)。然而,通过存储过程更新数据库,并不是那么简单,不能简单地通过DataAdapter的Update()方法进行更新。必须手动为DataAdapter添加InsertCommand, DeleteCommand, UpdateCommand,因为存储过程对这些操作的细节是不知情的,必须人为给出。
None.gif
None.gif为了达成这个目标,我完善了之前的TestPackage包,包头如下:
None.gif
None.gif create or replace package TestPackage is
None.gif
None.gif type mycursor is ref cursor;
None.gif
None.gif procedure UpdateRecords(id_in in number,newName in varchar2,newAge in number);
None.gif
None.gif procedure SelectRecords(ret_cursor out mycursor);
None.gif
None.gif procedure DeleteRecords(id_in in number);
None.gif
None.gif procedure InsertRecords(name_in in varchar2, age_in in number);
None.gif
None.gifend TestPackage;
None.gif
None.gif
None.gif
None.gif包体如下:
None.gif
None.gifcreate or replace package body TestPackage is
None.gif procedure UpdateRecords(id_in in number, newName in varchar2, newAge in number) as
None.gif begin
None.gif update test set age = newAge, name = newName where id = id_in;
None.gif end UpdateRecords;
None.gif
None.gif procedure SelectRecords(ret_cursor out mycursor) as
None.gif begin
None.gif open ret_cursor for select * from test;
None.gif end SelectRecords;
None.gif
None.gif procedure DeleteRecords(id_in in number) as
None.gif begin
None.gif delete from test where id = id_in;
None.gif end DeleteRecords;
None.gif
None.gif procedure InsertRecords(name_in in varchar2, age_in in number) as
None.gif begin
None.gif insert into test values (test_seq.nextval, name_in, age_in);
None.gif
None.gif--test_seq是一个已建的Sequence对象,请参照前面的示例
None.gif
None.gifend InsertRecords;
None.gif end TestPackage;
None.gif
None.gif
None.gif
None.gif前台调用代码如下,有点繁琐,请耐心阅读:
None.gif
None.gifstring connectionString = "Data Source=YXZHANG;User ID=YXZHANG;Password=YXZHANG";
None.gif
None.gifstring queryString = "TestPackage.SelectRecords";
None.gif
None.gif OracleConnection cn = new OracleConnection(connectionString);
None.gif
None.gif OracleCommand cmd = new OracleCommand(queryString,cn);
None.gif
None.gif cmd.CommandType = CommandType.StoredProcedure;
None.gif
None.gif cmd.Parameters.Add("ret_cursor",OracleType.Cursor);
None.gif
None.gif cmd.Parameters["ret_cursor"].Direction = ParameterDirection.Output;
None.gif
None.gif
None.gif
None.giftry
None.gif
ExpandedBlockStart.gifContractedBlock.gifdot.gif{
InBlock.gif
InBlock.gif cn.Open();
InBlock.gif
InBlock.gif OracleDataAdapter da = new OracleDataAdapter(cmd);
InBlock.gif
InBlock.gif DataSet ds = new DataSet();
InBlock.gif
InBlock.gif da.Fill(ds,"TestTable");
InBlock.gif
InBlock.gif cn.Close();
InBlock.gif
InBlock.gifint count = ds.Tables["TestTable"].Rows.Count;
InBlock.gif
InBlock.gif
InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif/**//*打印原始记录*/
InBlock.gif
InBlock.gif Console.WriteLine("Old Records is:");
InBlock.gif
InBlock.giffor( int i = 0;i <= count - 1;i++ )
InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gifdot.gif{
InBlock.gif
InBlock.gifstring id = ds.Tables["TestTable"].Rows[i]["ID"].ToString();
InBlock.gif
InBlock.gifstring name = ds.Tables["TestTable"].Rows[i]["NAME"].ToString();
InBlock.gif
InBlock.gifstring age = ds.Tables["TestTable"].Rows[i]["AGE"].ToString();
InBlock.gif
InBlock.gif
InBlock.gif
InBlock.gif Console.WriteLine("Record {0}:",i+1);
InBlock.gif
InBlock.gif Console.WriteLine("ID:{0}\tName:{1}\tAge:{2}\n",id,name,age);
InBlock.gif
ExpandedSubBlockEnd.gif }

InBlock.gif
InBlock.gif
InBlock.gif
InBlock.gif da.SelectCommand = cmd; //为DataAdapter指定SelectCommand
InBlock.gif

InBlock.gif
InBlock.gif
InBlock.gif OracleCommand updateCmd = new OracleCommand("TestPackage.UpdateRecords",cn);
InBlock.gif
InBlock.gif updateCmd.CommandType = CommandType.StoredProcedure;
InBlock.gif
InBlock.gif updateCmd.Parameters.Add("id_in",OracleType.Number,3,"ID");
InBlock.gif
InBlock.gif updateCmd.Parameters.Add("newName",OracleType.VarChar,20,"NAME");
InBlock.gif
InBlock.gif updateCmd.Parameters.Add("newAge",OracleType.Number,3,"AGE");
InBlock.gif
InBlock.gif da.UpdateCommand = updateCmd; //为DataAdapter指定UpdateCommand
InBlock.gif

InBlock.gif
InBlock.gif
InBlock.gif OracleCommand deleteCmd = new OracleCommand("TestPackage.DeleteRecords",cn);
InBlock.gif
InBlock.gif deleteCmd.CommandType = CommandType.StoredProcedure;
InBlock.gif
InBlock.gif deleteCmd.Parameters.Add("id_in",OracleType.Number,3,"ID");
InBlock.gif
InBlock.gif da.DeleteCommand = deleteCmd; //为DataAdapter指定DeleteCommand
InBlock.gif

InBlock.gif
InBlock.gif
InBlock.gif OracleCommand insertCmd = new OracleCommand("TestPackage.InsertRecords",cn);
InBlock.gif
InBlock.gif insertCmd.CommandType = CommandType.StoredProcedure;
InBlock.gif
InBlock.gif insertCmd.Parameters.Add("name_in",OracleType.VarChar,20,"NAME");
InBlock.gif
InBlock.gif insertCmd.Parameters.Add("age_in",OracleType.Number,3,"AGE");
InBlock.gif
InBlock.gif da.InsertCommand = insertCmd; //为DataAdapter指定InsertCommand
InBlock.gif

InBlock.gif
InBlock.gif
InBlock.gif DataTable newTable = ds.Tables["TestTable"];
InBlock.gif
InBlock.gif
InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif/**//*修改第一条记录*/
InBlock.gif
InBlock.gif newTable.Rows[0]["AGE"] = 22;
InBlock.gif
InBlock.gif newTable.Rows[0]["NAME"] = "John";
InBlock.gif
InBlock.gif
InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif/**//*删除一条记录*/
InBlock.gif
InBlock.gif newTable.Rows[2].Delete();
InBlock.gif
InBlock.gif
InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif/**//*插入一条记录*/
InBlock.gif
InBlock.gif DataRow newRow = newTable.NewRow();
InBlock.gif
InBlock.gif newRow["NAME"] = "Bob";
InBlock.gif
InBlock.gif newRow["AGE"] = 99;
InBlock.gif
InBlock.gif newTable.Rows.Add(newRow);
InBlock.gif
InBlock.gif
InBlock.gif
InBlock.gif cn.Open();
InBlock.gif
InBlock.gif da.Update(newTable); //将更改更新到数据库
InBlock.gif

InBlock.gif newTable.Clear(); //清空DataTable
InBlock.gif

InBlock.gif da.Fill(newTable); //获取更改后的数据
InBlock.gif

InBlock.gif cn.Close();
InBlock.gif
InBlock.gif
InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif/**//*打印新记录*/
InBlock.gif
InBlock.gif Console.WriteLine("New Records is:");
InBlock.gif
InBlock.giffor( int i = 0;i <= newTable.Rows.Count - 1;i++ )
InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gifdot.gif{
InBlock.gif
InBlock.gifstring id = newTable.Rows[i]["ID"].ToString();
InBlock.gif
InBlock.gifstring name = newTable.Rows[i]["NAME"].ToString();
InBlock.gif
InBlock.gifstring age = newTable.Rows[i]["AGE"].ToString();
InBlock.gif
InBlock.gif
InBlock.gif
InBlock.gif Console.WriteLine("Record {0}:",i+1);
InBlock.gif
InBlock.gif Console.WriteLine("ID:{0}\tName:{1}\tAge:{2}\n",id,name,age);
InBlock.gif
ExpandedSubBlockEnd.gif }

InBlock.gif
ExpandedBlockEnd.gif }

None.gif
None.gifcatch( OracleException ex )
None.gif
ExpandedBlockStart.gifContractedBlock.gifdot.gif{
InBlock.gif
InBlock.gif Console.WriteLine("Exception occurred!");
InBlock.gif
InBlock.gif Console.WriteLine("The exception message is:{0}",ex.Message.ToString());
InBlock.gif
ExpandedBlockEnd.gif }

None.gif
None.giffinally
None.gif
ExpandedBlockStart.gifContractedBlock.gifdot.gif{
InBlock.gif
InBlock.gif Console.WriteLine("------------------End-------------------");
InBlock.gif
ExpandedBlockEnd.gif}

None.gif
None.gif
None.gif
None.gif运行结果如下:
None.gif
None.gifOld Records is:
None.gif
None.gifRecord 1:
None.gif
None.gifID:100 Name:Tony Age:23
None.gif
None.gif
None.gif
None.gifRecord 2:
None.gif
None.gifID:101 Name:Jack Age:34
None.gif
None.gif
None.gif
None.gifRecord 3:
None.gif
None.gifID:103 Name:newAdd Age:100
None.gif
None.gif
None.gif
None.gifNew Records is:
None.gif
None.gifRecord 1:
None.gif
None.gifID:100 Name:John Age:22
None.gif
None.gif
None.gif
None.gifRecord 2:
None.gif
None.gifID:101 Name:Jack Age:34
None.gif
None.gif
None.gif
None.gifRecord 3:
None.gif
None.gifID:104 Name:Bob Age:99
None.gif
None.gif
None.gif
None.gif------------------End-------------------
None.gif
None.gif
None.gif
None.gif小结:
None.gif
None.gif使用Update()方法更新数据库是非常有用的,但是只局限于单个表,对于表连接的情况,还是直接用OracleCommand的ExcuteNonQuery()方法去执行更新操作比较方便。
None.gif
None.gif
None.gif
None.gif七、本文总结:
None.gif
None.gif在.NET中利用ADO.NET调用Oralce9i中的存储过程,函数,包等,可以提高效率,也能完成一些复杂的业务逻辑。然而调用Oracle存储过程和Sql Server中有着很大不同,最需要明确的一个思想就是:在Oracle的过程中,所有返回值必须以输出参数的形式出现。在Oracle中,要返回一个结果集,就必须把REF CURSOR作为返回参数,因此就要用到包。包类似于C++中的类,封装了一些全局变量、常量和函数,在函数中又可以有自己的私有变量。通过返回给应用程序的游标,应用程序就可以用DataReader或者DataAdapter接收它,进而做一些处理。还还可以用DataAdapter的Update()方法更新数据库,当然它需要你给出更新数据库的一些操作(为其定义处理过程)。
None.gif
None.gif由于笔者是边学边写,以上示例不免有些不妥之处,请多多指教!



本文转自高海东博客园博客,原文链接:http://www.cnblogs.com/ghd258/archive/2006/02/10/328604.html,如需转载请自行联系原作者

版权声明:本文内容由阿里云实名注册用户自发贡献,版权归原作者所有,阿里云开发者社区不拥有其著作权,亦不承担相应法律责任。具体规则请查看《阿里云开发者社区用户服务协议》和《阿里云开发者社区知识产权保护指引》。如果您发现本社区中有涉嫌抄袭的内容,填写侵权投诉表单进行举报,一经查实,本社区将立刻删除涉嫌侵权内容。

相关文章
NetApp发布多款产品 支持存储云计算
本文讲的是NetApp发布多款产品 支持存储云计算,NetApp公司推出了支持云计算架构的产品和服务,用于其企业级客户的数据中心。
1593 0
大促场景系统稳定性保障实践经验总结
11月11日0点刚过26秒,天猫双11的订单创建峰值就达到58.3万笔/秒,阿里云又一次扛住全球最大规模流量洪峰!58.3万笔/秒,这一数字是2009年第一次天猫双11的1457倍。
7157 0
Asp.Net Web API 2第三课——.NET客户端调用Web API
原文:Asp.Net Web API 2第三课——.NET客户端调用Web API Asp.Net Web API 导航     Asp.Net Web API第一课——入门http://www.cnblogs.com/aehyok/p/3432158.html       Asp.Net Web API第二课——CRUD操作http://www.cnblogs.com/aehyok/p/3434578.html 前言 本教程演示从一个控制台应用程序,使用HttpClient调用Web API。
1082 0
JAVAONE 2016会议总结
有幸获得阿里赞助,前去旧金山参加Oracle举办的JAVAONE 2016开发者大会,JAVAONE作为Java界最顶级的盛会,会议内容将展现出数千万开发者最关心的问题,以及java的未来发展方向。大会有很多的会场,参会者可以根据自己的喜好选择不同的会场进行参会,大会共持续5天,每天都有数十场会..
3160 0
诡异的druid链接池链接断开故障经验总结
诡异的druid链接池链接断开故障经验总结 标签: druid mysql mysqlReplication haproxy shardingJDBC 背景 症状 排查 修复 背景 最近在陆续做机房升级相关工作,配合DBA对产线数据库链接方式做个调整,将原来直接链接读库的地址切换到统一的读负载均衡的代理 haproxy 上,方便机柜和服务器的搬迁。
2783 0
+关注
2857
文章
0
问答
文章排行榜
最热
最新
相关电子书
更多
文娱运维技术
立即下载
《SaaS模式云原生数据仓库应用场景实践》
立即下载
《看见新力量:二》电子书
立即下载