用c#返回oracle函数的数据

本文关键字:数据 函数 oracle 返回 | 更新日期: 2023-09-27 18:01:39

我刚刚开始阅读有关存储过程的内容。谁能帮我从c#调用oracle存储过程?调用函数时,只显示表格,不显示函数数据

create or replace FUNCTION queue_sms_gad1
( psUserID varchar
,pUserIP varchar
,pSchema varchar
,psKieu varchar
,psNetwork VARCHAR
,piSeq VARCHAR)
return sys_refcursor IS
cf sys_refcursor;
sx varchar2(3000);
iqueue_id number := TO_NUMBER(piSeq);
snet varchar2(100);
BEGIN
snet := '''' || REPLACE(psNetwork, ',', ''',''') || '''';    
sx := 'select  a.rowid rid
      from tt_lan_guisms_ct a, tt_lan_guisms b, ad_nw_number d
      where  a.lan_guisms_id = b.id
          and (SUBSTR(a.mobile,0,4) = d.first_number or SUBSTR(a.mobile,0,5) = d.first_number)
          and d.nw_type in ('||snet||')';
if psKieu = 'VIBER' THEN
    sx := sx || ' and b.loaisp_id in (13,14) and a.state in (0,4)';
ELSE
    sx := sx || ' and ((b.loaisp_id = 4 and a.state in (0,4)) or (b.loaisp_id = 14 and a.state = 5))';
end if;
sx := sx ||' order by a.priority desc, a.ngay_tao';
sx := 'update tt_lan_guisms_ct set state = 2, INTERTAL_GET_QUEUE_ID = '|| iqueue_id ||'
       where INTERTAL_GET_QUEUE_ID =0  -- DK nay phai co de tranh ban tin duoc SMS nhieu lan
         and state in (0,4,5)
         and rowid in
         (select rid  from ('|| sx ||') where rownum < 2)';
EXECUTE IMMEDIATE sx;
COMMIT;
sx:='select  a.mobile, b.content, a.contact_id, a.lan_guisms_id langui_id, d.nw_type nettype, b.loaisp_id
    from '||pSchema||'tt_lan_guisms_ct a
        ,'||pSchema||'tt_lan_guisms b
        ,'||pSchema||'ad_nw_number d
    where  a.lan_guisms_id = b.id
        and INTERTAL_GET_QUEUE_ID = :1
        and a.state = 2
        and (SUBSTR(a.mobile,0,4) = d.first_number or SUBSTR(a.mobile,0,5) = d.first_number)';
OPEN cf FOR sx USING iqueue_id;
RETURN cf;
exception
when others then
    open cf for 'select '' '' mobile, '' '' content, '' '' contact_id, 0 langui_id,
                              '''' service_no, '''' brandname, '''' nettype, 0 loaisp_id,
                              '''' vina_flow, '''' contractid, '''' labelid, '''' tempid
            from dual where 1=0';
    return cf;
END;
c#中的

和调用函数

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Oracle.DataAccess.Client;
using Oracle.DataAccess.Types;
using System.Threading;
namespace WindowsFormsApplication2
{
public partial class Form1 : Form
{
    public Form1()
    {
        InitializeComponent();
    }
    private OracleConnection con;
    private void connect()
    {
        string oradb = "Data Source=(xxx)";
        try
        {
            con = new OracleConnection(oradb);
            con.Open();
        }
        catch (Exception s)
        {
            MessageBox.Show(s.Message, "That bai", MessageBoxButtons.OK, MessageBoxIcon.Error);
        }
    }
    private DataTable dt = new DataTable("T_cursor");
    private OracleDataAdapter da = new OracleDataAdapter();
    private void disconnect()
    {
        con.Close();
        con.Dispose();
        con = null;
    }
    private void GetData()
    {
        try
        {
            OracleCommand cmd = new OracleCommand();
            cmd.CommandType = CommandType.StoredProcedure;
            cmd.CommandText = "g3g4.queue_sms_gad1";
            cmd.Parameters.Add("v_RefCursor", OracleDbType.RefCursor, ParameterDirection.ReturnValue);
            cmd.Parameters.Add("admin", OracleDbType.Varchar2, 200, ParameterDirection.Input);
            cmd.Parameters.Add("localhost", OracleDbType.Varchar2,200, ParameterDirection.Input);
            cmd.Parameters.Add("G3G4.", OracleDbType.Varchar2, 200, ParameterDirection.Input);
            cmd.Parameters.Add("GSM_MODEM", OracleDbType.Varchar2, 200, ParameterDirection.Input);
            cmd.Parameters.Add("VT", OracleDbType.Varchar2, 200, ParameterDirection.InputOutput);
            cmd.Parameters.Add("999", OracleDbType.Varchar2, 200, ParameterDirection.Input);
            cmd.Connection = con;

            cmd.ExecuteReader(); //Chay function
            da.SelectCommand = cmd; //Gan command cho da
            da.Fill(dt); //Nap du lieu cho table
            dataGridView1.DataSource = dt; //Load du lieu len DataGridview
        }
        catch (Exception ex)
        {
            MessageBox.Show(ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
        } 
    }
    private void Form1_Load(object sender, EventArgs e)
    {
        connect();
        GetData();
    }
}
}

感谢帮助!!!!

用c#返回oracle函数的数据

我相信这就是REF CuRSOR可以使用的地方。首先要向存储过程传递一个REF CURSOR。然后,在存储过程中执行select操作之前,将有类似于以下内容:

OPEN oCur FOR
SELECT * FROM .....