C#调用ORCLe 带参数select存储过程--10g及以下


  一、在oracle中建立表





二建立存储过程

create or replace package stupkg   ----stupkg是包的名称
    is
        type mycursor is ref cursor;     -------定义一个名为mycursor的类型
        procedure getstudentinfo(id1 in number, cur_mycursor out mycursor);  
    end;  ----包规范中包括定义的一个mycursor类型和存储过程getstudentinfo
 




create or replace package body stupkg
    is
      procedure getstudentinfo(id1 in number, cur_mycursor out mycursor)
      is
      begin
           open cur_mycursor for select * from AUTHOR WHERE AUTHOR.id = id1;
      end getstudentinfo;
    end stupkg;


三、在VS2010调用

 1、using System.Data.OracleClient; //如何引用 该过时包 可以将.NET框架改为4  见上篇文章

 2、建立连接字符串

   string CONN_STR = "Data Source=(DESCRIPTION="
                + "(ADDRESS_LIST=(ADDRESS=(PROTOCOL=TCP)(HOST=117.131.244.186)(PORT=1521)))"
                + "(CONNECT_DATA=(SERVER=DEDICATED)(SERVICE_NAME=AANJSC)));"
                + "User Id=XAAXHQ;Password=XAAHQ;";

附代码


using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.OracleClient;
//using Oracle.DataAccess.Client;

using System.Data;

namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            string CONN_STR = "Data Source=(DESCRIPTION="
                + "(ADDRESS_LIST=(ADDRESS=(PROTOCOL=TCP)(HOST=147.239.244.186)(PORT=1521)))"
                + "(CONNECT_DATA=(SERVER=DEDICATED)(SERVICE_NAME=NSSSJSC)));"
                + "User Id=XXFFHQ;Password=FFF;";
            if (string.IsNullOrEmpty(CONN_STR))//指示指定的字符串是 null 还是 Empty 字符串。
            {
                throw new Exception("数据库访问服务出现故障,请联系xx!");
            }
            else
            {
                OracleConnection conn = null;
                try
                {
                    conn = new OracleConnection(CONN_STR);
                    conn.Open();
                }
                catch (Exception e)
                {
                    Console.WriteLine("{0}second exception caught", e);
                    Console.ReadLine();
                }
                Console.WriteLine("连接成功");
                Console.ReadLine();


                OracleDataAdapter oda = new OracleDataAdapter("select * from author",conn);
                DataSet ds = new DataSet();
                oda.Fill(ds);
                 DataTable dt = ds.Tables[0];






                  //调用存储过程

                 OracleCommand orclcomm = new OracleCommand("stupkg.getstudentinfo", conn);
                 orclcomm.CommandType = CommandType.StoredProcedure;
                 OracleParameter paramentIn = new OracleParameter("id1", OracleType.Int32);//输入参数
                 paramentIn.Value = 1;
                 paramentIn.Direction = ParameterDirection.Input;

                 OracleParameter parament = new OracleParameter("cur_mycursor", OracleType.Cursor);//输出游标参数
                 parament.Direction = ParameterDirection.Output;

                 orclcomm.Parameters.Add(paramentIn);
                 orclcomm.Parameters.Add(parament);

                 OracleDataAdapter oda2 = new OracleDataAdapter(orclcomm);
                 DataSet ds2 = new DataSet();

                 oda2.Fill(ds2);
           




                if (null != conn)
                {
                    conn.Close();
                    conn.Dispose();
                }
            }
        }
    }
}



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值