c# Oracle数据库引用PL/SQL游标



我有名为generuj_cene_wypoz()的游标,游标在数据库中工作,但我不知道如何在c#中正确调用它,因为程序和数据库之间的连接工作。

public void question2(string name)
{
OracleCommand cmd = con.CreateCommand();
cmd.CommandText = name;
cmd.CommandType = CommandType.StoredProcedure;
OracleDataReader dr = cmd.ExecuteReader();
DataTable dt = new DataTable();
dt.Load(dr);
myDataGrid.ItemsSource = dt.DefaultView;
dr.Close();
}
private void Button_Click_1(object sender, RoutedEventArgs e)
{
question2("exec generuj_cene_wpoz()");
}
我使用 连接到数据库
Oracle.ManagedDataAccess.Client;
Oracle.ManagedDataAccess.Types;

这里有一个例子。

第一个函数返回一个ref游标在oracle

create or replace package test_PKG as 
function test_rpt (p_testid number) return sys_refcursor;
end test_PKG;
create or replace package body test_PKG as
function test_rpt (p_testid number) return sys_refcursor as
test_rpt_cur sys_refcursor;
begin 
open test_rpt_cur for
select 1 as id_info from dual;

return test_rpt_cur;
end test_rpt;
test_PKG

c#代码
public Datatable pop_test_rpt (int p_testid_app)
{
using (OracleConnection myconn = new OracleConnection(string_connection_info)
{
const string _default_cursor_name = "1";
myconn.Open();
OracleCommand cmd = new OracleCommand ("test_schema.test_PKG.test_rpt", myconn)
cmd.CommandType = CommandType.StoredProcedure;
cmd.Parameters.Add(_default_cursor_name, OracleDBType.RefCursor, ParamaterDirection.ReturnValue); 
cmd.Parameters.Add("p_testid", OracleDBType.Int32).Value = p_testidapp;
DataTable dt = new DataTable();
dt.Load(cmd.ExecuteReader());
myconn.close();
return dt;
}
}

最新更新