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. Sql multiple queries [modified]

Sql multiple queries [modified]

Scheduled Pinned Locked Moved C#
databasesaleshelptutorialquestion
6 Posts 4 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.
  • J Offline
    J Offline
    JelleM
    wrote on last edited by
    #1

    Hello, I want to use multiple queries (so, in the while-loop of a reader, i want to use another query + reader). The folowwing code will give an example: SqlCommand myCommand = new SqlCommand("SELECT date, customerid FROM orders ORDER BY date ASC", connect); SqlDataReader myReader = myCommand.ExecuteReader(); while (myReader.Read()) { SqlCommand myCommand2 = new SqlCommand("SELECT customer FROM customers WHERE id = " + myReader["customerid"] + " ORDER BY date ASC", connect); SqlDataReader myReader2 = myCommand2.ExecuteReader(); while (myReader2.Read()) listBestellingen.Items.Add(myReader["date"] + " " + myReader2["customer"]); } myReader.Close(); But he sais i allready have a reader open (which is the truth), and that i have to close it, but that's a no can do).. Can somebody help me? Thanks! -- modified at 16:20 Thursday 25th May, 2006

    S C A 3 Replies Last reply
    0
    • J JelleM

      Hello, I want to use multiple queries (so, in the while-loop of a reader, i want to use another query + reader). The folowwing code will give an example: SqlCommand myCommand = new SqlCommand("SELECT date, customerid FROM orders ORDER BY date ASC", connect); SqlDataReader myReader = myCommand.ExecuteReader(); while (myReader.Read()) { SqlCommand myCommand2 = new SqlCommand("SELECT customer FROM customers WHERE id = " + myReader["customerid"] + " ORDER BY date ASC", connect); SqlDataReader myReader2 = myCommand2.ExecuteReader(); while (myReader2.Read()) listBestellingen.Items.Add(myReader["date"] + " " + myReader2["customer"]); } myReader.Close(); But he sais i allready have a reader open (which is the truth), and that i have to close it, but that's a no can do).. Can somebody help me? Thanks! -- modified at 16:20 Thursday 25th May, 2006

      S Offline
      S Offline
      ShailenSukul
      wrote on last edited by
      #2

      A datareader keeps the connection open exclusively for the duration of its read operation. There are 2 ways to rectify this: 1. use another connection for the second datareader 2. Read the contents of first datareader into an arraylist, close the datareader and then use the same connection for the second datareader (recommended). With Regards Shane Sukul BSc Mcsd.Net Mcsd Mcad .Net Architect/Developer Ashlen Consulting Services P/L

      C 1 Reply Last reply
      0
      • J JelleM

        Hello, I want to use multiple queries (so, in the while-loop of a reader, i want to use another query + reader). The folowwing code will give an example: SqlCommand myCommand = new SqlCommand("SELECT date, customerid FROM orders ORDER BY date ASC", connect); SqlDataReader myReader = myCommand.ExecuteReader(); while (myReader.Read()) { SqlCommand myCommand2 = new SqlCommand("SELECT customer FROM customers WHERE id = " + myReader["customerid"] + " ORDER BY date ASC", connect); SqlDataReader myReader2 = myCommand2.ExecuteReader(); while (myReader2.Read()) listBestellingen.Items.Add(myReader["date"] + " " + myReader2["customer"]); } myReader.Close(); But he sais i allready have a reader open (which is the truth), and that i have to close it, but that's a no can do).. Can somebody help me? Thanks! -- modified at 16:20 Thursday 25th May, 2006

        C Offline
        C Offline
        Colin Angus Mackay
        wrote on last edited by
        #3

        You can only have one active reader per connection. You must open a new connection to run two readers simultaneously on the same database. You also don't close your second reader which means it won't get closed until the garbage collector cleans it up - and that may not be for a long time. However, you can do all this with one SQL command:

        SELECT orders.[date], cusomter.customer
        FROM orders
        INNER JOIN customer ON orders.customerid = customer.id
        ORDER BY [date] ASC

        So your code would look like this:

        SqlCommand = myCommand = new SqlCommand("SELECT orders.[date], cusomter.customer"+
        "FROM orders INNER JOIN customer ON orders.customerid = customer.id ORDER BY [date] ASC";
        SqlDataReader myReader = myCommand.ExecuteReader();
        while(myReader.Read())
        {
        listBestellingen.Items.Add(myReader["date"] + " " + myReader["customer"]);
        }
        myReader.Close();

        Does this help?


        "On two occasions, I have been asked [by members of Parliament], 'Pray, Mr. Babbage, if you put into the machine wrong figures, will the right answers come out?' I am not able to rightly apprehend the kind of confusion of ideas that could provoke such a question." --Charles Babbage (1791-1871) My: Website | Blog

        J 1 Reply Last reply
        0
        • S ShailenSukul

          A datareader keeps the connection open exclusively for the duration of its read operation. There are 2 ways to rectify this: 1. use another connection for the second datareader 2. Read the contents of first datareader into an arraylist, close the datareader and then use the same connection for the second datareader (recommended). With Regards Shane Sukul BSc Mcsd.Net Mcsd Mcad .Net Architect/Developer Ashlen Consulting Services P/L

          C Offline
          C Offline
          Colin Angus Mackay
          wrote on last edited by
          #4

          Or a third option to produce a better SQL command that only requires one reader.


          "On two occasions, I have been asked [by members of Parliament], 'Pray, Mr. Babbage, if you put into the machine wrong figures, will the right answers come out?' I am not able to rightly apprehend the kind of confusion of ideas that could provoke such a question." --Charles Babbage (1791-1871) My: Website | Blog

          1 Reply Last reply
          0
          • J JelleM

            Hello, I want to use multiple queries (so, in the while-loop of a reader, i want to use another query + reader). The folowwing code will give an example: SqlCommand myCommand = new SqlCommand("SELECT date, customerid FROM orders ORDER BY date ASC", connect); SqlDataReader myReader = myCommand.ExecuteReader(); while (myReader.Read()) { SqlCommand myCommand2 = new SqlCommand("SELECT customer FROM customers WHERE id = " + myReader["customerid"] + " ORDER BY date ASC", connect); SqlDataReader myReader2 = myCommand2.ExecuteReader(); while (myReader2.Read()) listBestellingen.Items.Add(myReader["date"] + " " + myReader2["customer"]); } myReader.Close(); But he sais i allready have a reader open (which is the truth), and that i have to close it, but that's a no can do).. Can somebody help me? Thanks! -- modified at 16:20 Thursday 25th May, 2006

            A Offline
            A Offline
            Arjan Einbu
            wrote on last edited by
            #5

            SQL Server 2005 supports having multiple readers open at the same time. (A feature called MARS). You need to add MultipleActiveResultSets=True to the connectionstring to make this work for your connection.

            string connectionString = "Data Source=localhost;Initial Catalog=AdventureWorks;IntegratedSecurity=SSPI;MultipleActiveResultSets=True";

            1 Reply Last reply
            0
            • C Colin Angus Mackay

              You can only have one active reader per connection. You must open a new connection to run two readers simultaneously on the same database. You also don't close your second reader which means it won't get closed until the garbage collector cleans it up - and that may not be for a long time. However, you can do all this with one SQL command:

              SELECT orders.[date], cusomter.customer
              FROM orders
              INNER JOIN customer ON orders.customerid = customer.id
              ORDER BY [date] ASC

              So your code would look like this:

              SqlCommand = myCommand = new SqlCommand("SELECT orders.[date], cusomter.customer"+
              "FROM orders INNER JOIN customer ON orders.customerid = customer.id ORDER BY [date] ASC";
              SqlDataReader myReader = myCommand.ExecuteReader();
              while(myReader.Read())
              {
              listBestellingen.Items.Add(myReader["date"] + " " + myReader["customer"]);
              }
              myReader.Close();

              Does this help?


              "On two occasions, I have been asked [by members of Parliament], 'Pray, Mr. Babbage, if you put into the machine wrong figures, will the right answers come out?' I am not able to rightly apprehend the kind of confusion of ideas that could provoke such a question." --Charles Babbage (1791-1871) My: Website | Blog

              J Offline
              J Offline
              JelleM
              wrote on last edited by
              #6

              TNX! This helped me out!

              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