点击这里给我发消息 点击这里给我发消息

C# 调用SQL存储过程

添加时间:2013-12-7
    相关阅读: 数据库 SQL Oracle C#
 

10,调用SQL存储过程

代码:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;              //注意
using System.Data.OracleClient; //Oracle数据库

namespace ConsoleApplication4
{
    class Program
    {
        static void Main(string[] args)
        {

            OracleConnection thisConnection = new OracleConnection(@"Data Source=orcl;User ID=system;Password=myoracle;Unicode=True");

            thisConnection.Open();//此处可以不用打开    

            OracleCommand thisCommand = thisConnection.CreateCommand();

            thisCommand.CommandType = CommandType.StoredProcedure;
            thisCommand.CommandText = "or_test";

            int rows = 0;
            string rtnStr;
            try
            {
                OracleParameter[] parm = new OracleParameter[2];
                //in
                parm[0] = new OracleParameter("myid", OracleType.Number, 3);
                parm[1] = new OracleParameter("myname", OracleType.VarChar, 50);

                //指明参数是输入还是输出型
                parm[0].Direction = ParameterDirection.Input;
                parm[1].Direction = ParameterDirection.Output;


                //给参数赋值
                parm[0].Value = 350;


                //传递参数给Oracle命令

                thisCommand.Parameters.Add(parm[0]);
                thisCommand.Parameters.Add(parm[1]);

                rows = thisCommand.ExecuteNonQuery();//返回行数

                //取出返回值
                rtnStr = Convert.ToString(parm[1].Value);//res
                Console.WriteLine(rtnStr);
                Console.WriteLine(Convert.ToString(rows));
            }
            catch (Exception er)
            {
                string merr = "err:" + er.ToString();
                Console.WriteLine(merr);
            }
            finally
            {
                thisConnection.Close();
                thisConnection.Dispose();//释放所有资源
                thisCommand.Parameters.Clear();
                thisCommand.Dispose();//释放所有资源
            }
        }
    }
}

存储过程:

咨询热线:020-85648757 85648755 85648616 0755-27912581 客服:020-85648756 0755-27912581 业务传真:020-32579052
广州市网景网络科技有限公司 Copyright◎2003-2008 Veelink.com. All Rights Reserved.
广州商务地址:广东省广州市黄埔大道中203号(海景园区)海景花园C栋501室
= 深圳商务地址:深圳市宝源路华丰宝源大厦606
研发中心:广东广州市天河软件园海景园区 粤ICP备05103322号 工商注册