您的位置:首页 > 数据库 > Oracle

C#调用oracle存储过程(返回数据集)

2012-05-21 13:05 441 查看
表:STUDENT(NAME,SEX,SCORE)

ORACLE包和存储过程:首先在ORACLE建立PACKAGE和PACKAGE BODY,将在这里面定义函数和存储过程返回结果集。

 1,建立PACKAGE:

 CREATE OR REPLACE PACKAGE SENYI.PACK_STU

IS

 TYPE T_CURSOR IS REF CURSOR;

 PROCEDURE PRO_STUDENT(RE_CURSOR OUT T_CURSOR);

 END;

 /

 说明:其实PACKAGE只是个声明罢了。我们在这里定义了一个存储过程返回结集。

 2,建立PACKAGE BODY:

 CREATE  OR REPLACE PACKAGE BODY SENYI.PACK_STU

 IS

 PROCEDURE PRO_STUDENT(RE_CURSOR OUT T_CURSOR)

 IS

 V_CURSOR T_CURSOR;

 BEGIN

 OPEN V_CURSOR FOR

   SELECT * FROM SENYI.STUDENT;

   RE_CURSOR:=V_CURSOR;

   END;

  END;

  /

说明:这里建立PACKAGE BODY是具体的说明和使用,将采用什么方式实现。。

------------------------------

c# 部分:

        string str="server=ORCL;user id=senyi;password=orcl";

        OracleConnection con = new OracleConnection(str);

        con.Open();

        OracleParameter[] para = new OracleParameter[]{

            new OracleParameter("RE_CURSOR", OracleType.Cursor)             

        };

        para[0].Direction = System.Data.ParameterDirection.Output;

        OracleCommand cmd = new OracleCommand();

        cmd.Connection = con;

        cmd.CommandType = CommandType.StoredProcedure;

        cmd.CommandText = "SENYI.PACK_STU.PRO_STUDENT";//注意此处存储过程的调用: 包+存储过程名

        foreach (OracleParameter pa in para)

        {

            cmd.Parameters.Add(pa);

        }

       

        OracleDataAdapter da = new OracleDataAdapter();

        da.SelectCommand = cmd;

        DataSet ds = new DataSet();

        da.Fill(ds);

        con.Close();

        DataTable dt = ds.Tables[0];

        this.GridView1.DataSource = dt;

        this.GridView1.DataBind();
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息