Results 1 to 2 of 2
  1. #1
    Join Date
    Sep 2003
    Posts
    2

    Unanswered: Multiple open DataReaders required

    Hi,

    i want to execute a SELECT-statement while anotherone still runs.
    For better understanding i'll briefly explain my situation requirements:

    i request "Trends" from a DB-Table "PW_Trends" and get up to 4 rows as a result.
    There are typical fields like "TrendTitle", "TrendID", ... and so on.
    Each of these Trends has (up to 8) Tags - to get those Tags i use the TrendID I get from the SELECT-statement above, as a parameter for another SELECT-statement in which I request "Tags" from a DB-Table "PW_Tags"

    so it's like this:
    ====================
    System.Data.SqlClient.SqlDataReader dr_getTrends = sqlCommand4.ExecuteReader();
    while (dr_getTrends.Read())
    {
    ...
    string TrendID = dr_getTrends[ "TrendID" ].ToString();
    ...
    sqlCommand6.Parameters[ "@TrendID" ].Value = TrendID;
    System.Data.SqlClient.SqlDataReader dr_getTags= sqlCommand6.ExecuteReader();
    ...
    }
    ====================

    As you'll mayble already have noticed, i haven't closed the first DataReader before I open the other one - that's why I get the Error
    "There is already an open DataReader associated with this Connection which must be closed first."

    I understand the problem, but I am unsure about how to implement, so it will work best.

    I think there are two ways I can handle this:

    1)
    - I open the first DataReader
    - I save all Trend-Data into arrays (very uncomfortable?)
    - I close the first DataReader
    - I open the 2nd one
    - I use the saved TrendID in the array to fill the required Parameter of the 2nd SELECT-statement
    - I close the 2nd DataReader
    - finished

    OR:

    2)
    - I simply create another sqlConnection-object to use for the 2nd SELECT-statement (where the Connection-Parameters would be the same as in the first one)
    - finished

    I don't think it's a really good idea to use more than one sqlConnection at a time, particularly if they obtain exactly same parameters.
    On the other side I don't like to mess with many arrays if not necessary.

    that's why i would appreciate any comments, or even other ways to solve this problem,

    thank you,

    Sebastian

  2. #2
    Join Date
    Sep 2003
    Posts
    2
    I have choosen the 1st way now, but would still like to hear your opinion about my situation.

    thanks,

    Sebastian

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •