使用c#呼叫oracle儲存過程獲取儲存過程引數資訊
阿新 • • 發佈:2018-12-31
本文介紹如何獲取儲存過程的引數資訊,並介紹如何在c#程式碼中使用ODP.NET呼叫oracle儲存過程返回結果集:
1:建立包:
SQL> create or replace package FORP_PROC_INFO is
2
3 -- Author : zhanghaopeng
4 -- Created : 2005-12-14
5 -- Purpose : 獲取儲存過程引數資訊
6
7 TYPE type_cur IS REF CURSOR; --定義遊標變數用於返回記錄集
8
9 PROCEDURE get_porc_pram(
10 proc_name in varchar2, --過程名稱
11 v_cur out type_cur --返回遊標
12 );
13
14 end FORP_PROC_INFO;
15 /
Package created
SQL>
2:建立包體:
SQL> create or replace package body FORP_PROC_INFO is
2
3
4 PROCEDURE get_porc_pram(
5 proc_name in varchar2, --過程名稱
6 v_cur out type_cur --返回遊標
7 )
8 AS
9 v_SQL varchar2(1000);
10
11 begin
12
13 v_sql:=' SELECT argument_name PARAMETER_NAME, data_type TYPE_NAME,in_out PARAMETER_TYPE,CHAR_LENGTH CHARACTER_OCTET_LENGTH,DATA_PRECISION NUMERIC_PRECISION,DATA_SCALE NUMERIC_SCALE from all_arguments WHERE object_id=( select object_id from all_objects where object_name=upper(trim(''' || proc_name || ''')))';
14
15
16
17 open v_cur for v_sql;
18
19 end get_porc_pram;
20
21 end FORP_PROC_INFO;
22 /
Package body created
3.用c#程式碼呼叫過程獲取過程引數資訊:
oracleConnection1.Open();
string strSQL = @"forp_proc_info.get_porc_pram";
Oracle.DataAccess.Client.OracleDataAdapter da = new Oracle.DataAccess.Client.OracleDataAdapter();
Oracle.DataAccess.Client.OracleCommand cmd = new Oracle.DataAccess.Client.OracleCommand(strSQL,oracleConnection1);
cmd.CommandType = CommandType.StoredProcedure;
Oracle.DataAccess.Client.OracleParameter pram = new Oracle.DataAccess.Client.OracleParameter("proc_name",Oracle.DataAccess.Client.OracleDbType.Varchar2);
pram.Value = "spo_addcontact";
cmd.Parameters.Add(pram);
Oracle.DataAccess.Client.OracleParameter pram1 = new Oracle.DataAccess.Client.OracleParameter("v_cur",Oracle.DataAccess.Client.OracleDbType.RefCursor);
pram1.Direction = ParameterDirection.Output;
cmd.Parameters.Add(pram1);
da.SelectCommand = cmd;
DataSet ds = new DataSet();
da.Fill(ds);
this.dataGrid1.DataSource=ds.Tables[0].DefaultView;
1:建立包:
SQL> create or replace package FORP_PROC_INFO is
2
3 -- Author : zhanghaopeng
4 -- Created : 2005-12-14
5 -- Purpose : 獲取儲存過程引數資訊
6
7 TYPE type_cur IS REF CURSOR; --定義遊標變數用於返回記錄集
8
9 PROCEDURE get_porc_pram(
10 proc_name in varchar2, --過程名稱
11 v_cur out type_cur --返回遊標
12 );
13
14 end FORP_PROC_INFO;
15 /
Package created
SQL>
2:建立包體:
SQL> create or replace package body FORP_PROC_INFO is
2
3
4 PROCEDURE get_porc_pram(
5 proc_name in varchar2, --過程名稱
6 v_cur out type_cur --返回遊標
7 )
8 AS
9 v_SQL varchar2(1000);
10
11 begin
12
13 v_sql:=' SELECT argument_name PARAMETER_NAME, data_type TYPE_NAME,in_out PARAMETER_TYPE,CHAR_LENGTH CHARACTER_OCTET_LENGTH,DATA_PRECISION NUMERIC_PRECISION,DATA_SCALE NUMERIC_SCALE from all_arguments WHERE object_id=( select object_id from all_objects where object_name=upper(trim(''' || proc_name || ''')))';
14
15
16
17 open v_cur for v_sql;
18
19 end get_porc_pram;
20
21 end FORP_PROC_INFO;
22 /
Package body created
3.用c#程式碼呼叫過程獲取過程引數資訊:
oracleConnection1.Open();
string strSQL = @"forp_proc_info.get_porc_pram";
Oracle.DataAccess.Client.OracleDataAdapter da = new Oracle.DataAccess.Client.OracleDataAdapter();
Oracle.DataAccess.Client.OracleCommand cmd = new Oracle.DataAccess.Client.OracleCommand(strSQL,oracleConnection1);
cmd.CommandType = CommandType.StoredProcedure;
Oracle.DataAccess.Client.OracleParameter pram = new Oracle.DataAccess.Client.OracleParameter("proc_name",Oracle.DataAccess.Client.OracleDbType.Varchar2);
pram.Value = "spo_addcontact";
cmd.Parameters.Add(pram);
Oracle.DataAccess.Client.OracleParameter pram1 = new Oracle.DataAccess.Client.OracleParameter("v_cur",Oracle.DataAccess.Client.OracleDbType.RefCursor);
pram1.Direction = ParameterDirection.Output;
cmd.Parameters.Add(pram1);
da.SelectCommand = cmd;
DataSet ds = new DataSet();
da.Fill(ds);
this.dataGrid1.DataSource=ds.Tables[0].DefaultView;