VerySource

 找回密码
 立即注册
搜索
热搜: 活动 交友 discuz
查看: 1266|回复: 6

如何用数组返回多个存储过程的值

[复制链接]

1

主题

7

帖子

6.00

积分

新手上路

Rank: 1

积分
6.00
发表于 2020-1-28 17:40:01 | 显示全部楼层 |阅读模式

以下是微软 petshop4 的代码,但只有输入参数,没有返回参数

/// <summary>
        /// Execute a SqlCommand that returns a resultset against the database specified in the connection string
        /// using the provided parameters.
        /// </summary>
        /// <remarks>
        /// e.g.:  
        ///  SqlDataReader r = ExecuteReader(connString, CommandType.StoredProcedure, "PublishOrders", new SqlParameter("@prodid", 24));
        /// </remarks>
        /// <param name="connectionString">a valid connection string for a SqlConnection</param>
        /// <param name="commandType">the CommandType (stored procedure, text, etc.)</param>
        /// <param name="commandText">the stored procedure name or T-SQL command</param>
        /// <param name="commandParameters">an array of SqlParamters used to execute the command</param>
        /// <returns>A SqlDataReader containing the results</returns>
        public static SqlDataReader ExecuteReader(string connectionString, CommandType cmdType, string cmdText, params SqlParameter[] commandParameters) {
            SqlCommand cmd = new SqlCommand();
            SqlConnection conn = new SqlConnection(connectionString);

            // we use a try/catch here because if the method throws an exception we want to
            // close the connection throw code, because no datareader will exist, hence the
            // commandBehaviour.CloseConnection will not work
            try {
                PrepareCommand(cmd, conn, null, cmdType, cmdText, commandParameters);
                SqlDataReader rdr = cmd.ExecuteReader(CommandBehavior.CloseConnection);
                cmd.Parameters.Clear();
                return rdr;
            }
            catch {
                conn.Close();
                throw;
            }
        }


/// <summary>
        /// Prepare a command for execution
        /// </summary>
        /// <param name="cmd">SqlCommand object</param>
        /// <param name="conn">SqlConnection object</param>
        /// <param name="trans">SqlTransaction object</param>
        /// <param name="cmdType">Cmd type e.g. stored procedure or text</param>
        /// <param name="cmdText">Command text, e.g. Select * from Products</param>
        /// <param name="cmdParms">SqlParameters to use in the command</param>
        private static void PrepareCommand(SqlCommand cmd, SqlConnection conn, SqlTransaction trans, CommandType cmdType, string cmdText, SqlParameter[] cmdParms) {

            if (conn.State != ConnectionState.Open)
                conn.Open();

            cmd.Connection = conn;
            cmd.CommandText = cmdText;

            if (trans != null)
                cmd.Transaction = trans;

            cmd.CommandType = cmdType;

            if (cmdParms != null) {
                foreach (SqlParameter parm in cmdParms)
                    cmd.Parameters.Add(parm);
            }
        }
    }



调用方法
SqlParameter[] parms = new SqlParameter[]
        {
                        new SqlParameter("@DealId", SqlDbType.VarChar, 10)
                };


parms[0].Value = "";
GridView1.DataSource = SqlHelper.GetDataset(SqlHelper.ConnectionStringLocalTransaction, CommandType.StoredProcedure, "P_DealedProcess", parms);

        GridView1.DataBind();


回复

使用道具 举报

0

主题

58

帖子

32.00

积分

新手上路

Rank: 1

积分
32.00
发表于 2020-2-24 16:15:01 | 显示全部楼层

SqlParameter[] parms = new SqlParameter[]
        {
                        new SqlParameter("@DealId", SqlDbType.VarChar, 10)
                           new SqlParameter .... //定义输出参数
                };


parms[0].Value = "";
GridView1.DataSource = SqlHelper.GetDataset(SqlHelper.ConnectionStringLocalTransaction, CommandType.StoredProcedure, "P_DealedProcess", parms);

object out = parms[1].Value ; //out 为输出参数的值。
        GridView1.DataBind();

回复

使用道具 举报

1

主题

7

帖子

6.00

积分

新手上路

Rank: 1

积分
6.00
 楼主| 发表于 2020-3-1 01:45:02 | 显示全部楼层
everever30
  
               我是想写一个通用的返回存储过程的多个值的函数,不知怎么写
回复

使用道具 举报

1

主题

7

帖子

6.00

积分

新手上路

Rank: 1

积分
6.00
 楼主| 发表于 2020-3-1 14:00:02 | 显示全部楼层
cmd.Parameters.Clear(); 已将数组删除了,object out = parms[1].Value ; //out 为输出参数的值。
这句报错
回复

使用道具 举报

0

主题

322

帖子

115.00

积分

新手上路

Rank: 1

积分
115.00
发表于 2020-3-3 19:30:01 | 显示全部楼层
cmd.Parameters.Clear();


object[] ary = new object[cmd.Parameters.Count];
                        for(int i=0;i<ary.Length;i++)
                        {
                                ary[i] =  cmd.Parameters[i].Value;
                        }


最后返回ary,

lz 是这个意思吗?
回复

使用道具 举报

1

主题

7

帖子

6.00

积分

新手上路

Rank: 1

积分
6.00
 楼主| 发表于 2020-3-25 10:30:02 | 显示全部楼层
shenmue024
   

           是这样的,正在测试
回复

使用道具 举报

1

主题

7

帖子

6.00

积分

新手上路

Rank: 1

积分
6.00
 楼主| 发表于 2020-5-23 09:30:02 | 显示全部楼层
shenmue024

          在函数应该有一个数组参数,来接收object[] ary 的值

       问题1
                怎样把动态数组(数组开始使用前,不知道返回几个值)作参数
       问题2
                怎样把object[] ary 的值传给动态数组参数
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

Archiver|手机版|CopyRight © 2008-2023|verysource.com ( 京ICP备17048824号-1 )

快速回复 返回顶部 返回列表