C#調用oracle預存程序 最簡單的執行個體

來源:互聯網
上載者:User
oracle|預存程序  

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




相關文章

E-Commerce Solutions

Leverage the same tools powering the Alibaba Ecosystem

Learn more >

Apsara Conference 2019

The Rise of Data Intelligence, September 25th - 27th, Hangzhou, China

Learn more >

Alibaba Cloud Free Trial

Learn and experience the power of Alibaba Cloud with a free trial worth $300-1200 USD

Learn more >

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。