locked
How to convert SQLce Datareader values to list? RRS feed

  • Question

  • Hi all,

    i want to know can we convert DataReader values to list without using foreach or loops.

    i mean can we assign DataReader values to List directly??

    or we have to add one bye one them using loops??? 


    sumitk

    Monday, October 22, 2012 7:06 AM

Answers

  • Hi,

    afaik, you have to use a loop


    Regards, Nico

    • Proposed as answer by Lisa Zhu Tuesday, October 23, 2012 8:00 AM
    • Marked as answer by Lisa Zhu Tuesday, October 30, 2012 6:54 AM
    Monday, October 22, 2012 7:14 AM
  • Hi,

    DataRader is a forward and read only data one by one.So to add records to list one by one we have to use while loop.

    eg:

    SqlceDataReader dr=cmd.ExecuteReader();

    while(dr.Read())

    {

       // write your code here

    }


    PS.Shakeer Hussain

    • Proposed as answer by Lisa Zhu Tuesday, October 23, 2012 8:00 AM
    • Marked as answer by Lisa Zhu Tuesday, October 30, 2012 6:54 AM
    Monday, October 22, 2012 7:17 AM
  • Hi Sumit,

    By using dataset also you have to use while or for loop to add data to list ..

    SqlceDataAdapter da=new SqlceDataAdapter(sqlQuery);
    Dataset ds=new DataSet();
    da.Fill(ds);
    for(int i=0;i<ds.Tables[0].Rows.Count;i++)
    {
     li.Add(ds.Tables[0].Rows[i]["columnName"].ToString());
    }


    PS.Shakeer Hussain

    • Proposed as answer by Lisa Zhu Tuesday, October 23, 2012 8:00 AM
    • Marked as answer by Lisa Zhu Tuesday, October 30, 2012 6:54 AM
    Monday, October 22, 2012 8:57 AM

All replies

  • Hi,

    afaik, you have to use a loop


    Regards, Nico

    • Proposed as answer by Lisa Zhu Tuesday, October 23, 2012 8:00 AM
    • Marked as answer by Lisa Zhu Tuesday, October 30, 2012 6:54 AM
    Monday, October 22, 2012 7:14 AM
  • Hi Sumitk,

    you need to use look structure for adding values in list. you can read your data reader until it reaches to end and one by one adding.

    If there are any other library available for the same, it would be doing internally this only.


    Thanks and Regards, Shailesh B. Davara

    Monday, October 22, 2012 7:16 AM
  • Hi,

    DataRader is a forward and read only data one by one.So to add records to list one by one we have to use while loop.

    eg:

    SqlceDataReader dr=cmd.ExecuteReader();

    while(dr.Read())

    {

       // write your code here

    }


    PS.Shakeer Hussain

    • Proposed as answer by Lisa Zhu Tuesday, October 23, 2012 8:00 AM
    • Marked as answer by Lisa Zhu Tuesday, October 30, 2012 6:54 AM
    Monday, October 22, 2012 7:17 AM
  • hi Syed Shakeer Hussain,

    thanx for your replaycan we use Dataset insteadof dataReader for achieve my goal??


    sumitk

    Monday, October 22, 2012 7:31 AM
  • Hi Sumit,

    By using dataset also you have to use while or for loop to add data to list ..

    SqlceDataAdapter da=new SqlceDataAdapter(sqlQuery);
    Dataset ds=new DataSet();
    da.Fill(ds);
    for(int i=0;i<ds.Tables[0].Rows.Count;i++)
    {
     li.Add(ds.Tables[0].Rows[i]["columnName"].ToString());
    }


    PS.Shakeer Hussain

    • Proposed as answer by Lisa Zhu Tuesday, October 23, 2012 8:00 AM
    • Marked as answer by Lisa Zhu Tuesday, October 30, 2012 6:54 AM
    Monday, October 22, 2012 8:57 AM