【问题标题】:How to populate many GridView controls from one stored procedure?如何从一个存储过程中填充多个 GridView 控件?
【发布时间】:2010-11-18 12:11:07
【问题描述】:

我在 SQL Server 中有一个存储过程,它返回七个结果集。我想从 ASP.NET 调用这个存储过程,并在我的 ASP.NET 页面上用结果填充七个 GridView。我正在使用 SqlDataReader 来获取数据,但是我正在努力使用 C# 代码来填充 GridViews。

我创建了一个 DAL 类来获取数据,并且我在其中有这个方法:

public SqlDataReader CheckDataIntegrity()
{
    SqlCommand cmd = new SqlCommand("cc.DataCheck");
    return MultipleResults(cmd);
}

辅助方法 MultipleResults 如下所示:

private SqlDataReader MultipleResults(SqlCommand cmd)
{
    SqlConnection con = new SqlConnection(_connectionString);
    cmd.Connection = con;

    con.Open();
    SqlDataReader dr = cmd.ExecuteReader();
    con.Close();
    return dr;

}

我正在尝试使用以下内容调用页面上的组件:

private void FillGridViews()           
{
    DBUtil DB = new DBUtil();
    using (SqlDataReader dr = DB.CheckDataIntegrity())
    {
        if (dr.HasRows)
        {
            while (dr.Read())
            {
                GridView1.DataSource = dr;
                GridView1.DataBind();
            }
        }
    }
} 

我确实在网上搜索过这个例子,但找不到任何东西。

你知道一个资源,或者有一个小例子可以分享吗?

谢谢。

【问题讨论】:

  • 在你的 multipleResults 方法中,你应该用 using 语句包围你的 SqlConnection。
  • @Laraeu - 这没什么区别,我仍然得到Invalid attempt to call HasRows when reader is closed.
  • 我在答案的 cmets 中看到了您遇到的问题。我会考虑更改 MultipleResults 的参数以接受 sqlconnection,这样您就可以有效地管理打开和关闭。

标签: c# asp.net multiple-resultsets


【解决方案1】:

您应该使用可以将多个表绑定到您的 GridView 的 DataSet。

var dataset = GetDataSetForMy7GridViews();

// also valid: dataset.Tables["TableName"];
GridView1.DataSource = dataset.Tables[0]; 
GridView1.DataBind();

GridView2.DataSource = dataset.Tables[1];
GridView2.DataBind();

GridView3.DataSource = dataset.Tables[2];
GridView3.DataBind();

GridView4.DataSource = dataset.Tables[3];
GridView4.DataBind();

GridView5.DataSource = dataset.Tables[4];
GridView5.DataBind();

GridView6.DataSource = dataset.Tables[5];
GridView6.DataBind();

GridView7.DataSource = dataset.Tables[6];
GridView7.DataBind();

由于您要绑定到 7 个不同的 GridView,因此您需要这样做。如果你按照你的代码来做:

>             while (dr.Read())
>             {
>                 GridView1.DataSource = dr;
>                 GridView1.DataBind();
>             }

它只会绑定到 1 个 GridView。

补充说: 也许这个链接是你问题的答案? http://www.codeguru.com/csharp/csharp/cs_network/database/article.php/c8715

【讨论】:

  • 这正是我的想法,最好的简洁,得到我的 +1。
  • 感谢您的解决方案。我试图远离数据集,因为与 SqlDataReader 相比性能较低。但是,如果我无法让 SqlDataReader 解决方案工作,我可能不得不使用 DataSet。感谢您的回复。
  • 马克,我在我的答案中添加了一个链接,看看它是否对你有帮助。
【解决方案2】:

您需要使用 DataReader 的 .NextResult() method 从第一个结果集前进到下一个结果集。如果集合中存在更多结果,则该方法返回 True,如果不存在更多结果集,则返回 False。

调用 .NextResult() 后,您可以将 GridView 绑定到当前结果集。

您的代码可能如下所示:

SqlDataReader dr = DB.CheckDBIntegrity();

while (!dr.NextResult())
    {
       // bind results to appropriate grid
    }

【讨论】:

  • 我在问题中更新了我的代码,但我收到了这个错误:Invalid attempt to call HasRows when reader is closed.,这令人困惑。
  • @Mark:这是因为您在读取/使用数据读取器中的数据之前已经关闭了与数据库的连接。
  • 我已经从 MultipleResults 方法中删除了 con.Close() 但我得到了同样的错误:Invalid attempt to call HasRows when reader is closed.
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-06-29
  • 1970-01-01
相关资源
最近更新 更多