You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

C# ASP.NET中提取含输入输出参数及双结果集的SQL Server存储过程数据并加载至GridView的问题咨询

解决SQL Server存储过程多结果集与输出参数问题

咱们先把你的两个问题拆解开,一步步解决:

问题1:@dateess_out输出参数始终为NULL

你这里明显混淆了输出参数和结果集的用法:输出参数只能传递单个值,而你提到第二个结果集是多条日期数据,这说明存储过程里应该是通过SELECT语句返回这些日期,而非给@dateess_out赋值。要么是存储过程根本没给这个参数赋值(自然返回NULL),要么你完全不需要这个参数——直接从结果集里取日期数据就好,建议直接删掉这个无用的输出参数,避免后续混淆。

问题2:获取第二个结果集并绑定到GridView

原来的代码用了ExecuteNonQuery(),这个方法只适合执行不返回结果集的SQL操作(比如INSERT/UPDATE/DELETE)。要获取存储过程返回的多个结果集,你得用ExecuteReader(),再通过SqlDataReader.NextResult()切换结果集。具体步骤如下:

  1. 用SqlDataReader读取第一个单行结果集;
  2. 调用reader.NextResult()切换到第二个多行列的日期结果集;
  3. 将第二个结果集填充到DataTable,再绑定到GridView控件。

重构后的完整代码示例

public string penth_slqDAYS(string connectionString) {
    int currentMonth = DateTime.Now.Month;
    int currentYear = DateTime.Now.Year;
    try {
        using (SqlConnection connection = new SqlConnection(connectionString)) {
            using (SqlCommand command1 = new SqlCommand("penthhmera_proc", connection)) {
                command1.CommandType = CommandType.StoredProcedure;

                // 配置输入参数
                command1.Parameters.Add("@prs_nmb_pen", SqlDbType.VarChar, 7).Value = prs_nmb_lb1.Text.Trim();
                command1.Parameters.Add("@month_pen", SqlDbType.Int).Value = currentMonth;
                command1.Parameters.Add("@year_int", SqlDbType.Int).Value = currentYear;

                // 配置有效输出参数(移除无用的@dateess_out)
                SqlParameter daysOutParam = command1.Parameters.Add("@days_out", SqlDbType.Int);
                daysOutParam.Direction = ParameterDirection.Output;
                SqlParameter messageOutParam = command1.Parameters.Add("@message_out", SqlDbType.VarChar, 50);
                messageOutParam.Direction = ParameterDirection.Output;

                connection.Open();
                // 用ExecuteReader获取多结果集
                using (SqlDataReader reader = command1.ExecuteReader()) {
                    // 处理第一个单行结果集(如果需要读取这里的数据,可在reader.Read()后通过列名获取)
                    if (reader.HasRows) {
                        reader.Read();
                        // 示例:var firstResultValue = reader["YourColumnName"].ToString();
                    }

                    // 切换到第二个日期结果集
                    if (reader.NextResult()) {
                        DataTable dateTable = new DataTable();
                        dateTable.Load(reader);
                        // 绑定到GridView(替换成你实际的控件ID)
                        yourGridViewID.DataSource = dateTable;
                        yourGridViewID.DataBind();
                    }
                }

                // 读取输出参数值
                days_penthwork_tx.Text = daysOutParam.Value.ToString();
                message_tx.Text = messageOutParam.Value.ToString();
            }
        }
        return "success";
    } catch (Exception e) {
        return e.ToString();
    }
}

额外注意事项

  • 把代码里的yourGridViewID替换成你WebForm中实际的GridView控件ID;
  • 如果第一个单行结果集有需要使用的数据,可在reader.Read()后通过reader["对应列名"]读取;
  • 确保存储过程中没有多余的未赋值输出参数,避免维护时混淆;
  • 全程用using包裹数据库相关对象,无需手动调用connection.Close(),using会自动释放资源。

内容的提问来源于stack exchange,提问作者David Dan

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.04.29 04:08:14