首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >遍历结果集.Net

问遍历结果集.Net
EN

Stack Overflow用户
提问于 2012-05-23 00:28:47
回答 1查看 579关注 0票数 3

我的代码如下所示,这个存储过程'get2Rows‘返回2行,我想获取第一行并获取列值,然后是第二行并获取列值。我需要如何迭代?

代码语言:javascript
复制
using (System.Data.Common.DbCommand dbCommand = DataAccess.Instance().Database.GetStoredProcCommand("get2Rows"))
{       
    using (IDataReader reader = DataAccess.Instance().Database.ExecuteReader(dbCommand))
    {
         while (reader.Read())//here i want to get the 1st row
         {
             //here i want to get the columns of the 1st row....
         }                            
    };                        
}
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2012-05-23 00:31:27

代码语言:javascript
复制
while (reader.Read())//here i want to get the 1st row
{
    //here i want to get the columns of the 1st row....
    int firstColumn = Convert.ToInt32(dr[0]["intColumn"].ToString());
    string secondColumn = dr[0]["stringColumn"].ToString();
    // etc.
}

您将获得这两行,因为您正在遍历它们。你想如何存储它们由你决定,也许是在一个集合中?

更多信息:http://www.csharp-station.com/Tutorial/AdoDotNet/lesson04

我的建议是,如果您使用的是DataReader,那么应该一次处理一行,因为这就是它的操作方式。如果您希望所有行都在内存中,则应该使用DataSet。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/10706166

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档