2012-05-22 49 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來代替。