Iterate over .Net result set

I have the code as shown below, this "get2Rows" stored procedure returns 2 rows, I want to get the 1st row and get the column values, and then the 2nd row and get the column values. How do I need iteration?

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....
         }                            
    };                        
}
+3
source share
1 answer
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.
}

You get both strings because you iterate over them. It is up to you, how do you want to store them, maybe in a collection?

Additional information: http://www.csharp-station.com/Tutorial/AdoDotNet/lesson04

My advice was to work with one row at a time if you are using DataReader, because that is how it works. If you need all the rows in memory, you should use a DataSet instead.

+1
source

All Articles