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