Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. Multiple Data Readers in C#

Multiple Data Readers in C#

Scheduled Pinned Locked Moved C#
questioncsharpdatabasetutorial
11 Posts 3 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • N Offline
    N Offline
    NJdotnetdev
    wrote on last edited by
    #1

    Question: How can I have multiple datareaders writing data in a loop? Example:

    For (int i=0; i<column.count;i++)
    {
    Step 1:
    Run the following query to get datareader values:
    select [] Column[i], count(Column[i]) AS COUNT
    from <Table Name>
    group by [] Column[i]

    Step 2:
    From the first row of datareader create a string.
    }

    Expected output when DataReader1 returns 3 rows and DataReader returns 3 rows: Sample: --> Row#1 from first dataReader --> Row#1 from second dataReader --> Row#2 from first dataReader --> Row#2 from second dataReader --> Row#3 from first dataReader --> Row#3 from second dataReader

    P S 2 Replies Last reply
    0
    • N NJdotnetdev

      Question: How can I have multiple datareaders writing data in a loop? Example:

      For (int i=0; i<column.count;i++)
      {
      Step 1:
      Run the following query to get datareader values:
      select [] Column[i], count(Column[i]) AS COUNT
      from <Table Name>
      group by [] Column[i]

      Step 2:
      From the first row of datareader create a string.
      }

      Expected output when DataReader1 returns 3 rows and DataReader returns 3 rows: Sample: --> Row#1 from first dataReader --> Row#1 from second dataReader --> Row#2 from first dataReader --> Row#2 from second dataReader --> Row#3 from first dataReader --> Row#3 from second dataReader

      P Offline
      P Offline
      PIEBALDconsult
      wrote on last edited by
      #2

      If you want multiple DataReaders you will need multiple Commands and Connections as well -- a Connection can have only one active Command executing against it at one time.

      N 1 Reply Last reply
      0
      • P PIEBALDconsult

        If you want multiple DataReaders you will need multiple Commands and Connections as well -- a Connection can have only one active Command executing against it at one time.

        N Offline
        N Offline
        NJdotnetdev
        wrote on last edited by
        #3

        Thanks for your reply. Can I have a list of SqlConnection, SqlDataReader & SqlCommand? Eg: List SQLConnections = new ();

        P 1 Reply Last reply
        0
        • N NJdotnetdev

          Thanks for your reply. Can I have a list of SqlConnection, SqlDataReader & SqlCommand? Eg: List SQLConnections = new ();

          P Offline
          P Offline
          PIEBALDconsult
          wrote on last edited by
          #4

          Sure you can, but I recommend making a class that holds a Connection, a Command, and a DataReader -- then make a list of those.

          N 1 Reply Last reply
          0
          • P PIEBALDconsult

            Sure you can, but I recommend making a class that holds a Connection, a Command, and a DataReader -- then make a list of those.

            N Offline
            N Offline
            NJdotnetdev
            wrote on last edited by
            #5

            Thanks a ton.. :)

            1 Reply Last reply
            0
            • N NJdotnetdev

              Question: How can I have multiple datareaders writing data in a loop? Example:

              For (int i=0; i<column.count;i++)
              {
              Step 1:
              Run the following query to get datareader values:
              select [] Column[i], count(Column[i]) AS COUNT
              from <Table Name>
              group by [] Column[i]

              Step 2:
              From the first row of datareader create a string.
              }

              Expected output when DataReader1 returns 3 rows and DataReader returns 3 rows: Sample: --> Row#1 from first dataReader --> Row#1 from second dataReader --> Row#2 from first dataReader --> Row#2 from second dataReader --> Row#3 from first dataReader --> Row#3 from second dataReader

              S Offline
              S Offline
              SledgeHammer01
              wrote on last edited by
              #6

              I think you're trying to solve a problem that doesn't exist. The DataReader portion of the code is very fast (assuming you aren't overusing .ToString() ;). The expense is executing the query itself and getting the data back over the wire. If you want to process the data in different threads, thats a different story. Use ONE DataReader and dump the data into a List. Then use Parallel.ForEach on the List.

              N P 2 Replies Last reply
              0
              • S SledgeHammer01

                I think you're trying to solve a problem that doesn't exist. The DataReader portion of the code is very fast (assuming you aren't overusing .ToString() ;). The expense is executing the query itself and getting the data back over the wire. If you want to process the data in different threads, thats a different story. Use ONE DataReader and dump the data into a List. Then use Parallel.ForEach on the List.

                N Offline
                N Offline
                NJdotnetdev
                wrote on last edited by
                #7

                Thanks for the information. If I am not wrong, this command is available for .NET 4 and above. If yes, then it might not work for me as I am working on 3.5. Please Advise.

                S 1 Reply Last reply
                0
                • S SledgeHammer01

                  I think you're trying to solve a problem that doesn't exist. The DataReader portion of the code is very fast (assuming you aren't overusing .ToString() ;). The expense is executing the query itself and getting the data back over the wire. If you want to process the data in different threads, thats a different story. Use ONE DataReader and dump the data into a List. Then use Parallel.ForEach on the List.

                  P Offline
                  P Offline
                  PIEBALDconsult
                  wrote on last edited by
                  #8

                  If it's tons of data I wouldn't want to hold it all in memory without good reason. (And it doesn't look like threading.)

                  S 1 Reply Last reply
                  0
                  • N NJdotnetdev

                    Thanks for the information. If I am not wrong, this command is available for .NET 4 and above. If yes, then it might not work for me as I am working on 3.5. Please Advise.

                    S Offline
                    S Offline
                    SledgeHammer01
                    wrote on last edited by
                    #9

                    Well, you can use multi-threading on the List in 3.5 without the Parallel.ForEach. Parallel.ForEach just makes it trivial and handles everything for you. As Pie mentioned, is it a lot of data? If its not a lot of data, just expensive processing, I'd dump it in a List and multi-thread process the List. If it is a lot of data, are you sure you need all that data at once? If its a lot of data AND you need it all at once, can you limit your app to run on a 64-bit OS? If so, memory won't really be an issue (as long as your machine has enough RAM). If its a lot of data AND you need it all at once AND you can't restrict to a 64-bit OS AND/OR it would be too much data to keep in memory, etc. and you really need to do the multi-threaded reader approach, is the data coming from a table or pre-computed? If so, you could have a paging approach.... have each reader grab x records on its own connection, etc, but that approach will kill performance as calling a SP and spinning up the connection and disposing it is relatively expensive.

                    1 Reply Last reply
                    0
                    • P PIEBALDconsult

                      If it's tons of data I wouldn't want to hold it all in memory without good reason. (And it doesn't look like threading.)

                      S Offline
                      S Offline
                      SledgeHammer01
                      wrote on last edited by
                      #10

                      From his original post, it looks like he is trying to implement multi-threaded processing of the table, just in the wrong way.

                      P 1 Reply Last reply
                      0
                      • S SledgeHammer01

                        From his original post, it looks like he is trying to implement multi-threaded processing of the table, just in the wrong way.

                        P Offline
                        P Offline
                        PIEBALDconsult
                        wrote on last edited by
                        #11

                        I considered that, but I don't think so. Of course, the situation is unclear.

                        1 Reply Last reply
                        0
                        Reply
                        • Reply as topic
                        Log in to reply
                        • Oldest to Newest
                        • Newest to Oldest
                        • Most Votes


                        • Login

                        • Don't have an account? Register

                        • Login or register to search.
                        • First post
                          Last post
                        0
                        • Categories
                        • Recent
                        • Tags
                        • Popular
                        • World
                        • Users
                        • Groups