如何从一个存储过程填充多个 GridView 控件?
我在 SQL Server 中有一个存储过程,它返回七个结果集。我想从 ASP.NET 调用此存储过程,并使用结果填充 ASP.NET 页面上的七个 GridView。我正在使用 SqlDataReader 来获取数据,但是我正在努力使用 C# 代码来填充 GridView。
我创建了一个 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();
}
}
}
}
我确实在网络上搜索了以下示例这个,但找不到任何东西。
您知道资源,或者有一个小例子可以分享吗?
谢谢。
I have a stored procedure in SQL Server which returns seven result sets. I would like to call this stored procedure from ASP.NET and populate seven GridViews on my ASP.NET page with the results. I am using a SqlDataReader to get the data, however I'm struggling with the C# code to fill the GridViews.
I have created a DAL class to get the data and I have this method in there:
public SqlDataReader CheckDataIntegrity()
{
SqlCommand cmd = new SqlCommand("cc.DataCheck");
return MultipleResults(cmd);
}
The helper method MultipleResults looks like this:
private SqlDataReader MultipleResults(SqlCommand cmd)
{
SqlConnection con = new SqlConnection(_connectionString);
cmd.Connection = con;
con.Open();
SqlDataReader dr = cmd.ExecuteReader();
con.Close();
return dr;
}
I'm trying to call the component on my page with something like:
private void FillGridViews()
{
DBUtil DB = new DBUtil();
using (SqlDataReader dr = DB.CheckDataIntegrity())
{
if (dr.HasRows)
{
while (dr.Read())
{
GridView1.DataSource = dr;
GridView1.DataBind();
}
}
}
}
I did search the web for an example of this, but couldn't find anything.
Do you know of a resource, or have a small example to share?
Thanks.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您应该使用 DataSet,它可以将多个表绑定到 GridView。
由于您要绑定到 7 个不同的 GridView,因此您需要这样做。如果你按照你的代码来做:
它只会绑定到1个GridView。
额外:
也许这个链接是您问题的答案?
http://www.codeguru.com/csharp/csharp/cs_network /database/article.php/c8715
You should use DataSet which can have multiple tables to be bound to your GridViews.
Since you are binding to 7 different GridView, you need to do it this way. If you do it according to your code:
It will only bind to 1 GridView.
added:
maybe this link is an answer to your question?
http://www.codeguru.com/csharp/csharp/cs_network/database/article.php/c8715
您需要使用 .NextResult() 方法< DataReader 的 /a> 从第一个结果集前进到下一个结果集。如果集合中存在更多结果,则该方法返回 True;如果不存在更多结果集,则该方法返回 False。
调用 .NextResult() 后,您可以将 GridView 绑定到当前结果集。
您的代码可能如下所示:
You need to use the .NextResult() method of your DataReader to advance from the first result set to the next. The method returns True if more results exist in your set, or False if no more result sets exist.
After calling .NextResult(), you can then bind your GridView to the current result set.
Your code might look like this: