C#调用存储过程 之返回值与输出参数

发布时间:2020-06-14 18:41:23 作者:atixujie
来源:网络 阅读:822

首先定义存储过程如下:(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();

        }
    }
}

推荐阅读:
  1. mybatis调用oracle存储过程
  2. 如何实现SQL Server存储过程在C#中调用

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

存储 create return

上一篇:Geometry 集合接口

下一篇:h3c wac361新建无线服务

相关阅读

您好,登录后才能下订单哦!

密码登录
登录注册
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》