首页 > 开发 > 综合 > 正文

C#調用oracle存儲過程 最簡單的實例

2024-07-21 02:26:57
字体:
来源:转载
供稿:网友

oracle方面
1.創建oracle過程存儲
create or replace procedure proce_test(paramin in varchar2,paramout out varchar2,paraminout in out varchar2)
as
  varparam varchar2(28);
begin
  varparam:=paramin;
  paramout:=varparam|| paraminout; 
end;
2.測試過程存儲
declare
  param_out varchar2(28);
  param_inout varchar2(28);
begin
  param_inout:='ff';  
  proce_test('dd',param_out,param_inout);  
  dbms_output.put_line(param_out);
end;

c#方面
引用oracle組件
using system;
using system.data;
using system.data.oracleclient;   

namespace webapplication4
{
     public class oraoprater
     {
         private oracleconnection conn=null;
         private oraclecommand cmd=null;
         public oraoprater()
         {
              string mconn="data source=ora9i.ora.com;user id=ora;password=ora";  //連接數據庫
              conn=new oracleconnection(mconn);
              try
              {
                   conn.open();
                   cmd=new oraclecommand();
                   cmd.connection=conn;
              }
              catch(exception e)
              {
                   throw e;
              }
         }

         public string spexefor(string m_a,string m_b)
         {
                //存儲過程的參數聲明
              oracleparameter[] parameters={
                                 new oracleparameter("paramin",oracletype.varchar,20),
                                new oracleparameter("paramout",oracletype.varchar,20),
                                 new oracleparameter("paraminout",oracletype.varchar,20)
                                                };
              parameters[0].value=m_a;
              parameters[2].value=m_b;
              parameters[0].direction=parameterdirection.input;
              parameters[1].direction=parameterdirection.output;
              parameters[2].direction=parameterdirection.inputoutput;
              try
              {
                   runprocedure("proce_test",parameters);
                  return parameters[1].value.tostring();
              }
              catch(exception e)
              {
                   throw e;
              }
         }

         private void runprocedure(string storedprocname,oracleparameter[] parameters)
         {
              cmd.commandtext=storedprocname;//聲明存儲過程名
              cmd.commandtype=commandtype.storedprocedure;
              foreach(oracleparameter parameter in parameters)
              {
                   cmd.parameters.add(parameter);
              }
              cmd.executenonquery();//執行存儲過程
         }
     }
}
測試結果:ddff


发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表