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. Database & SysAdmin
  3. Database
  4. How to use the same db connection with multiple datareaders?

How to use the same db connection with multiple datareaders?

Scheduled Pinned Locked Moved Database
csharpdatabasetutorialquestion
6 Posts 4 Posters 1 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.
  • S Offline
    S Offline
    Segal
    wrote on last edited by
    #1

    Is it possible to define one db connection only, then define multiple dataReader assoiciated with that connection in ADO.net? If yes, how to do it? Thanks!

    G A 2 Replies Last reply
    0
    • S Segal

      Is it possible to define one db connection only, then define multiple dataReader assoiciated with that connection in ADO.net? If yes, how to do it? Thanks!

      G Offline
      G Offline
      Gertjan Schuurmans
      wrote on last edited by
      #2

      In general, it's not wise to do so. In a single threaded application you might orchestrate these DataReaders to use only one connection (that means one at a time, but that's probably not what you want), but in any multi-threaded environment (like ASP.NET) you will get into all kinds of trouble. Rely on the underlying data provider to perform connection-pooling, and open a connection for each operation. For SQL Server you can specify a connectionstring a la: "Server=SVR1;Database=Northwind;...;Pooling=true" This will force the re-use of connections, and makes opening one a lot cheaper. Gertjan Schuurmans Amsterdam The Netherlands

      G 1 Reply Last reply
      0
      • G Gertjan Schuurmans

        In general, it's not wise to do so. In a single threaded application you might orchestrate these DataReaders to use only one connection (that means one at a time, but that's probably not what you want), but in any multi-threaded environment (like ASP.NET) you will get into all kinds of trouble. Rely on the underlying data provider to perform connection-pooling, and open a connection for each operation. For SQL Server you can specify a connectionstring a la: "Server=SVR1;Database=Northwind;...;Pooling=true" This will force the re-use of connections, and makes opening one a lot cheaper. Gertjan Schuurmans Amsterdam The Netherlands

        G Offline
        G Offline
        Gertjan Schuurmans
        wrote on last edited by
        #3

        ;P = Server=SVR1;Database=Northwind;...; Pooling=true" :laugh: Gertjan Schuurmans Amsterdam The Netherlands

        1 Reply Last reply
        0
        • S Segal

          Is it possible to define one db connection only, then define multiple dataReader assoiciated with that connection in ADO.net? If yes, how to do it? Thanks!

          A Offline
          A Offline
          andyharman
          wrote on last edited by
          #4

          No you can't have multiple datareaders open at the same time for the same database connection. You might want to think of the following alternatives:

          1. Open, read, then close each of your datareaders in sequence.
          2. Execute all of the queries using a single datareader (e.g. "select * from a ; select * from b ; select * from c"). The datareader is able to process multiple resultsets (for many database products).
          3. Read each of your queries into a DataSet object then do whatever processing you need against the DataSet.
          4. Open multiple database connections.

          Your choice depends upon what processing you are trying to achieve. Hope this helps. Andy Harman

          A 1 Reply Last reply
          0
          • A andyharman

            No you can't have multiple datareaders open at the same time for the same database connection. You might want to think of the following alternatives:

            1. Open, read, then close each of your datareaders in sequence.
            2. Execute all of the queries using a single datareader (e.g. "select * from a ; select * from b ; select * from c"). The datareader is able to process multiple resultsets (for many database products).
            3. Read each of your queries into a DataSet object then do whatever processing you need against the DataSet.
            4. Open multiple database connections.

            Your choice depends upon what processing you are trying to achieve. Hope this helps. Andy Harman

            A Offline
            A Offline
            Anonymous
            wrote on last edited by
            #5

            Thanks for replying! Initially what I did before is kind of as follows: static void main() { .... func1(); .... } static void func1() { .... string connectionstring = "..."; sqlconnection myconnection = new sqlconnection(connectionstring); string sqlstr1 = "..."; sqlcommand mycommand = new sqlcommand(sqlstr1, myconnection); myconnection.open() sqldatareader myreader = mycommand.executereader(); string myvalue = ""; while (myreader.read() { myvalue = myreader["..."].tostring(); processThisValue(myvalue); waitforresult(myvalue, connectionstring); } myreader.close(); myconnection.close(); } static processThisValue(string myvalue) { .......... } static void waitforresult(string inputvalue, string connectionstring) { sqlconnection myconnection2 = new sqlconnection(connectionstring); string sqlstr2 = "select xxx from xxx when xxx=" + inputvalue; sqlcommand mycommand2 = new sqlcommand(sqlstr2, myconnection2); while (true) { myconnection2.open(); sqldatareader mydatareader2 = mycommand2.executereader(); string myvalue2 = ""; while (mydatareader2.read()) { myvalue2 = mydatareader2["..."].toString(); } if (myvalue2 == "...") break; } mydatareader2.close(); myconnection2.close(); } because of the cost for db connection is expensive, so I'm thinking to use one connection for 2 readers, etc. From your point of view, how should I simplify the above process? Thanks!

            A 1 Reply Last reply
            0
            • A Anonymous

              Thanks for replying! Initially what I did before is kind of as follows: static void main() { .... func1(); .... } static void func1() { .... string connectionstring = "..."; sqlconnection myconnection = new sqlconnection(connectionstring); string sqlstr1 = "..."; sqlcommand mycommand = new sqlcommand(sqlstr1, myconnection); myconnection.open() sqldatareader myreader = mycommand.executereader(); string myvalue = ""; while (myreader.read() { myvalue = myreader["..."].tostring(); processThisValue(myvalue); waitforresult(myvalue, connectionstring); } myreader.close(); myconnection.close(); } static processThisValue(string myvalue) { .......... } static void waitforresult(string inputvalue, string connectionstring) { sqlconnection myconnection2 = new sqlconnection(connectionstring); string sqlstr2 = "select xxx from xxx when xxx=" + inputvalue; sqlcommand mycommand2 = new sqlcommand(sqlstr2, myconnection2); while (true) { myconnection2.open(); sqldatareader mydatareader2 = mycommand2.executereader(); string myvalue2 = ""; while (mydatareader2.read()) { myvalue2 = mydatareader2["..."].toString(); } if (myvalue2 == "...") break; } mydatareader2.close(); myconnection2.close(); } because of the cost for db connection is expensive, so I'm thinking to use one connection for 2 readers, etc. From your point of view, how should I simplify the above process? Thanks!

              A Offline
              A Offline
              andyharman
              wrote on last edited by
              #6

              I would recommend that you try the simplest (easiest to code and maintain) way first, then optimise if you find that its performance is not good enough. If your process is used a lot then you should find that the SQL-Server connection pooling will reduce the expense of database connections. If your process is not used a lot then it is not worth the bother of optimising the code. Andy Harman

              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