C#存储过程 传入参数 传出参数 结果集

作者:卞功鑫 转载请保留:http://www.cnblogs.com/BinBinGo/p/6400928.html

//1   连接字符串
            string connectionString
                = "server=127.0.0.1;integrated security=true;database=MSPetShop4";
            // = "server=.;u;
            // = "server=.;user ;
            //2 实例化数据库连接
            using(System.Data.SqlClient.SqlConnection connection = new SqlConnection(connectionString))
            {
                //定义执行SQL语句,可以为select查询,也可以为存储过程,我们要的只是返回的结果集.
                string sql = "p_proc_name";

                //SqlCommand 表示数据库要执行的sql命令
                System.Data.SqlClient.SqlCommand command = new SqlCommand(sql, connection);
                //告知数据库现在要执行的是存储过程
                //默认为标准SQL语句,可以不用设置.
                command.CommandType = CommandType.StoredProcedure;

                //提供存储过程参数(传入参数) 这里的名称@pin和存储过程中的保持一致
                System.Data.SqlClient.SqlParameter pin = new SqlParameter("@pin", System.Data.SqlDbType.Int);
                //参数赋值
                pin.Value = 10;
                //将上面的参数加入command中
                command.Parameters.Add(pin);
               //表值参数
               System.Data.SqlClient.SqlParameter pids = new SqlParameter("@ids", ids); //ids 为datatable
pids.SqlDbType = SqlDbType.Structured;
               pids.TypeName = "dbo.EntityIdCollection";//EntityIdcollection 为自定义类别 
command.Parameters.Add(pids);

//提供存储过程参数(传出参数)这里的名称@pout和存储过程中的保持一致 System.Data.SqlClient.SqlParameter pout = new SqlParameter("@pout", System.Data.SqlDbType.Int); //声明为传出参数 Direction 参数方向 ,默认为传入参数 ParameterDirection.Input pout.Direction = ParameterDirection.Output; //将上面的参数加入command中 command.Parameters.Add(pout); //return 参数 名称@returnValue随便取,类型固定为int类型. System.Data.SqlClient.SqlParameter preturn = new SqlParameter("@returnValue",System.Data.SqlDbType.Int); //声明为传出参数 Direction 参数方向 ,默认为传入参数 ParameterDirection.Input preturn.Direction = ParameterDirection.ReturnValue; //return 在存储过程中隐藏的,但是在C#时要显式使用 command.Parameters.Add(preturn); //强大的SqlDataAdapter //可以使用 SqlDataAdapter(command) 属性实体化,也可以使用SqlDataAdapter(sql,connection)实例化. //SqlDataAdapter(command) 主要用于存储过程 //SqlDataAdapter(sql,connection) 主要用于select语句 System.Data.SqlClient.SqlDataAdapter adapter = new SqlDataAdapter(command); //用于接收adapter.Fill 返回的结果集 DataSet ds = new DataSet(); //返回集插入到 dataset ds 中 adapter.Fill(ds); //现在得到了三个东西 //一 存储过程的返回结果集 dataset //二 存储过程的 output 传出参数值 //三 存储过程的 return 值 int outputValue = (int)pout.Value; int returnValue = (int)preturn.Value; Console.WriteLine("返回了{0}个DataTable;outputValue 的值为{1};returnValue 的值为{2}", ds.Tables.Count,outputValue,returnValue); } Console.ReadLine();

存储过程

ALTER PROC p_proc_name
    (
      @pin INT ,
      @pout INT OUTPUT
    )
AS --------执行用户自定义逻辑--------------




---------返回结果集 1----------------------

    SELECT  客户 ,
            产品 ,
            数量 ,
            金额 ,
            年龄
    FROM    dbo.A

-----------返回结果集 2--------------------

    SELECT  客户 ,
            产品 ,
            数量 ,
            金额 ,
            年龄
    FROM    dbo.A
    WHERE   年龄 IS NOT NULL


  -----------设置output参数值-------------------  
  
    SET @pout = @pin * 100;

--------------returnw值-------------
               
    IF ( @pin <= 0 )
                 --return 如果没有写,其值默认为0 ,表示执行成功.
        RETURN -1;
                 --return 之后的语句不执行.