C#调用存储过程之返回值与输出参数-创新互联

首先定义存储过程如下:(sqlserver 2008)

成都创新互联是一家集网站建设,塔城企业网站建设,塔城品牌网站建设,网站定制,塔城网站建设报价,网络营销,网络优化,塔城网站推广为一体的创新建站企业,帮助传统企业提升企业形象加强企业竞争力。可充分满足这一群体相比中小企业更为丰富、高端、多元的互联网需求。同时我们时刻保持专业、时尚、前沿,时刻以成就客户成长自我,坚持不断学习、思考、沉淀、净化自己,让我们为更多的企业打造出实用型网站。

use studb2008
go
create procedure proc_test
@num int=-1 output
as
  set @num=10 --输出参数
  return 2  --返回值
  go

然后在vs中写如下c#代码:

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

           string s = @"Data Source=.\sql2008express;Initial Catalog=studb2008;User ID=sa;Password=sa";
            SqlConnection con = new SqlConnection(s);
            SqlCommand command = new SqlCommand();
            command.Connection = con;
            command.CommandText = "proc_test"; //proc_test为存储过程的名字
           command.CommandType = CommandType.StoredProcedure; //设置执行的类型
            SqlParameter para = new SqlParameter("@a",SqlDbType.Int);//任意定义一个变量,来接收返回值参数
            para.Direction = ParameterDirection.ReturnValue;   //注意这里1 表示接收返回值
            command.Parameters.Add(para);
            SqlParameter para2 = new SqlParameter("@num", SqlDbType.Int); //第二个变量来接收存储过程的输出参数
            para2.Direction = ParameterDirection.Output;   //注意这里2 表示接收输出值
          command.Parameters.Add(para2);
            con.Open();
            command.ExecuteNonQuery();
            int n = (int)command.Parameters["@a"].Value;
            int n2 = (int)command.Parameters["@num"].Value;
            Console.WriteLine(“n:”+n+":n2="+n2); //分别输出返回值和输出参数的值。分别是2和10
            Console.ReadLine();
            con.Close();

       }
    }
}

创新互联www.cdcxhl.cn,专业提供香港、美国云服务器,动态BGP最优骨干路由自动选择,持续稳定高效的网络助力业务部署。公司持有工信部办法的idc、isp许可证, 机房独有T级流量清洗系统配攻击溯源,准确进行流量调度,确保服务器高可用性。佳节活动现已开启,新人活动云服务器买多久送多久。


网站名称:C#调用存储过程之返回值与输出参数-创新互联
转载来于:http://myzitong.com/article/ceppsj.html