asp.net 存储过程 返回多个值

参考:参考动软.net生成器

存储过程:

-- =============================================
-- Author:     <mrcoolye,,dong>
-- Create date: <2012 9-12,,>
-- Description:    <获取订单各种状态的数量,,>
-- =============================================
Create   procedure [dbo].[up_Order_OutputOrderCountByStatus](
@order_status0    int OUTPUT ,--未确认订单
@pay_status0    int OUTPUT ,--未付款订单
@pay_status2    int OUTPUT ,--已付款订单
@order_status6 int OUTPUT ,--处理中订单
@shipping_status1    int OUTPUT , --已发货订单 
@order_status2    int OUTPUT , --已取消订单
@userid int --当前用户
) 
as

set @order_status0 = (select count(1) from ShopOrder where order_status=0 and userid=@userid)
set @pay_status0 = (select count(1) from ShopOrder where pay_status=0 and userid=@userid)
set @pay_status2 = (select count(1) from ShopOrder where order_status=2 and userid=@userid)
set @order_status6 = (select count(1) from ShopOrder where order_status=6 and userid=@userid)
set @shipping_status1 = (select count(1) from ShopOrder where shipping_status=1 and userid=@userid)
set @order_status2 = (select count(1) from ShopOrder where order_status=2 and userid=@userid)
return @order_status0;

go

程序:

/// <summary>
        /// 获取订单各种状态的数量
        /// </summary>
        /// <param name="userid">账户id</param>
        /// <returns></returns>
        public List<string> GetOrderCountByStatus(int userid)
        {
            SqlParameter[] parameters = {
                    new SqlParameter("@order_status0", SqlDbType.Int, 4),
                    new SqlParameter("@pay_status0", SqlDbType.Int, 4),
                    new SqlParameter("@pay_status2", SqlDbType.Int,4),
                    new SqlParameter("@order_status6", SqlDbType.Int,4),
                    new SqlParameter("@shipping_status1", SqlDbType.Int,4),
                    new SqlParameter("@order_status2", SqlDbType.Int,4),
                    new SqlParameter("@userid", SqlDbType.Int,4),
                    };
            parameters[0].Direction = ParameterDirection.Output;
            parameters[1].Direction = ParameterDirection.Output;
            parameters[2].Direction = ParameterDirection.Output;
            parameters[3].Direction = ParameterDirection.Output;
            parameters[4].Direction = ParameterDirection.Output;
            parameters[5].Direction = ParameterDirection.Output;
            parameters[6].Value = userid;

            int rowsAffected = 0;

            List<string> resultList = new List<string>();
            DbHelperSQL.RunProcedure("[up_Order_OutputOrderCountByStatus]", parameters,  out rowsAffected);
            resultList.Add(parameters[0].Value.ToString());
            resultList.Add(parameters[1].Value.ToString());
            resultList.Add(parameters[2].Value.ToString());
            resultList.Add(parameters[3].Value.ToString());
            resultList.Add(parameters[4].Value.ToString());
            resultList.Add(parameters[5].Value.ToString());

            return resultList;

        }

应用到的DBHelperSql是以下:

/// <summary>
        /// 执行存储过程,返回影响的行数        
        /// </summary>
        /// <param name="storedProcName">存储过程名</param>
        /// <param name="parameters">存储过程参数</param>
        /// <param name="rowsAffected">影响的行数</param>
        /// <returns></returns>
        public static int RunProcedure(string storedProcName, IDataParameter[] parameters, out int rowsAffected)
        {
            using (SqlConnection connection = new SqlConnection(connectionString))
            {
                int result;
                connection.Open();
                SqlCommand command = BuildIntCommand(connection, storedProcName, parameters);
                rowsAffected = command.ExecuteNonQuery();
                result = (int)command.Parameters["ReturnValue"].Value;
                //Connection.Close();
                return result;
            }
        }

/// <summary>
        /// 创建 SqlCommand 对象实例(用来返回一个整数值)    
        /// </summary>
        /// <param name="storedProcName">存储过程名</param>
        /// <param name="parameters">存储过程参数</param>
        /// <returns>SqlCommand 对象实例</returns>
        private static SqlCommand BuildIntCommand(SqlConnection connection, string storedProcName, IDataParameter[] parameters)
        {
            SqlCommand command = BuildQueryCommand(connection, storedProcName, parameters);
            command.Parameters.Add(new SqlParameter("ReturnValue",
                SqlDbType.Int, 4, ParameterDirection.ReturnValue,
                false, 0, 0, string.Empty, DataRowVersion.Default, null));
            return command;
        }

/// <summary>
        /// 构建 SqlCommand 对象(用来返回一个结果集,而不是一个整数值)
        /// </summary>
        /// <param name="connection">数据库连接</param>
        /// <param name="storedProcName">存储过程名</param>
        /// <param name="parameters">存储过程参数</param>
        /// <returns>SqlCommand</returns>
        private static SqlCommand BuildQueryCommand(SqlConnection connection, string storedProcName, IDataParameter[] parameters)
        {
            SqlCommand command = new SqlCommand(storedProcName, connection);
            command.CommandType = CommandType.StoredProcedure;
            foreach (SqlParameter parameter in parameters)
            {
                if (parameter != null)
                {
                    // 检查未分配值的输出参数,将其分配以DBNull.Value.
                    if ((parameter.Direction == ParameterDirection.InputOutput || parameter.Direction == ParameterDirection.Input) &&
                        (parameter.Value == null))
                    {
                        parameter.Value = DBNull.Value;
                    }
                    command.Parameters.Add(parameter);
                }
            }

            return command;
        }

 public static string connectionString = ".\SQLEXPRESS;database=TestSqlData;uid=sa;pwd=sa";         

 

mark

转载于:https://www.cnblogs.com/yfdong22/archive/2012/09/12/2681432.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值