access SqlDataReader using foreach

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • jarod1701@gmx.de

    access SqlDataReader using foreach

    Hi everyone,

    is there a way to access every record from a SqlDataReader via
    "foreach" ?
    I want to write a class which (besides doing some other stuff)
    retrieves a recordset using SqlCommand.Exec uteReader().

    Something like:

    MyDbClass db = new MyDbClass("SELE CT * FROM tblUsers");
    foreach(MyRecor dClass record in db.getRecordSet ())
    {
    Console.WriteLi ne(MyRecordClas s["UserId"]);
    }

    I know this has something to do with GetEnumerator (which SqlDataReader
    implements) but I don't know how to start or if it's possible at all.

    I hope someone can help me out.

    Thanks in advance,

    Kevin

  • sloan

    #2
    Re: access SqlDataReader using foreach

    The syntax is:

    IDataReader dataReader = // something to get an IDataReader
    try
    {
    while (dataReader.Rea d())
    {
    if (!(dataReader.I sDBNull(0)))
    {
    Console.WriteLi ne(dataReader.G etString(0));


    }
    }
    {
    finally
    {
    if (!((dataReader == null)))
    {
    try
    {
    dataReader.Clos e();
    }
    catch
    {
    }
    }
    }

    See full example at:





    5/24/2006 Entry
    Custom Objects/Collections and Tiered Development

    The CustomerControl ler.cs file has it.





    <jarod1701@gmx. dewrote in message
    news:1161112009 .542565.217620@ k70g2000cwa.goo glegroups.com.. .
    Hi everyone,
    >
    is there a way to access every record from a SqlDataReader via
    "foreach" ?
    I want to write a class which (besides doing some other stuff)
    retrieves a recordset using SqlCommand.Exec uteReader().
    >
    Something like:
    >
    MyDbClass db = new MyDbClass("SELE CT * FROM tblUsers");
    foreach(MyRecor dClass record in db.getRecordSet ())
    {
    Console.WriteLi ne(MyRecordClas s["UserId"]);
    }
    >
    I know this has something to do with GetEnumerator (which SqlDataReader
    implements) but I don't know how to start or if it's possible at all.
    >
    I hope someone can help me out.
    >
    Thanks in advance,
    >
    Kevin
    >

    Comment

    • Yury

      #3
      Re: access SqlDataReader using foreach

      We use smth like List.ForEach:

      public int ReadAll(SqlComm and cmd, Action<IDataRec ordaction)
      {
      if (cmd == null)
      throw new ArgumentNullExc eption("cmd");

      if (action == null)
      throw new ArgumentNullExc eption("action" );

      SqlDataReader reader = null;
      try
      {
      OpenConnection( cmd);
      reader = cmd.ExecuteRead er();
      while (reader.Read())
      action(reader);
      return reader.RecordsA ffected;
      }
      finally
      {
      if (reader != null)
      reader.Close();
      CloseConnection ();
      }
      }

      and usage:
      List<ProductTyp eproducts = new List<ProductTyp e>();

      MainDB.ReadAll( cmd, delegate(IDataR ecord record) {
      ProductType pt =
      (ProductType)Co nvert.ToInt32(r ecord["ProductTyp eId"]);
      products.Add(pt );
      });

      also, you can replace delegate call with yield and use foreach then.

      Comment

      Working...