2012-05-22 87 views
3

我有类似下面的代码,这个存储过程'get2Rows'返回2行,我想获取第1行并获取列值,然后获取第2行并获取列值。我如何迭代?遍历结果集.Net

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.... 
     }        
    };       
} 

回答

1
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来代替。