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. odbcdatareader hangs on close

odbcdatareader hangs on close

Scheduled Pinned Locked Moved C#
comtoolshelpquestion
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.
  • B Offline
    B Offline
    BlackDice
    wrote on last edited by
    #1

    I have a problem when trying to close a connection or the odbcdatareader if I stop it from doing Read() before it would normally be done it hangs. I saw something similar when I Googled this a while ago, but can't find it now. Apparently, the while(dr.Read()) doesn't actually stop even though I used a break; to get out of the while() loop. Anybody run into this before?

    My Music | My Pics | My Articles BlackDice

    E E 2 Replies Last reply
    0
    • B BlackDice

      I have a problem when trying to close a connection or the odbcdatareader if I stop it from doing Read() before it would normally be done it hangs. I saw something similar when I Googled this a while ago, but can't find it now. Apparently, the while(dr.Read()) doesn't actually stop even though I used a break; to get out of the while() loop. Anybody run into this before?

      My Music | My Pics | My Articles BlackDice

      E Offline
      E Offline
      ednrgc
      wrote on last edited by
      #2

      show is a small code snippet of the loop. I have a feeling you're missing something that is causing the loop to continue, and not actually hitting the line to break out of the loop.

      B 1 Reply Last reply
      0
      • E ednrgc

        show is a small code snippet of the loop. I have a feeling you're missing something that is causing the loop to continue, and not actually hitting the line to break out of the loop.

        B Offline
        B Offline
        BlackDice
        wrote on last edited by
        #3

        No, I don't think that's it because it breaks from the loop with no problem. I put a breakpoint on the line immediately following the loop and it goes directly there with no problem. This function is being done in a thread of its own so that the user can press 'Cancel' and also updates the UI correctly. Here's a snippet of what I'm doing:

        while (statreader.Read() && !m_bCancel)
        {
           stemp = statreader["aj_vchar"].ToString();
           //do other stuff here
           this.progressBar1.Increment(1);
                                    
        }
        this.progressBar1.Value = 0;  //breakpoint here,stops here soon as I press 'Cancel'
        statreader.Close(); //this line hangs for a LONG time (~10 minutes) 
        

        I've tried calling Close() on the connection first. I've tried calling Dispose() instead, and I've tried not closing it at all to see what happens when it goes out of scope (statreader). No matter what, this reader hangs on close depening on how many records are in the reader. When I try it on a test database that returns only a few thousand records, Close() only takes a few seconds. When I try it on a database with about 400,000 records being returned, it takes about 10 minutes for Close() to finish executing.

        My Music | My Pics | My Articles BlackDice

        E 1 Reply Last reply
        0
        • B BlackDice

          No, I don't think that's it because it breaks from the loop with no problem. I put a breakpoint on the line immediately following the loop and it goes directly there with no problem. This function is being done in a thread of its own so that the user can press 'Cancel' and also updates the UI correctly. Here's a snippet of what I'm doing:

          while (statreader.Read() && !m_bCancel)
          {
             stemp = statreader["aj_vchar"].ToString();
             //do other stuff here
             this.progressBar1.Increment(1);
                                      
          }
          this.progressBar1.Value = 0;  //breakpoint here,stops here soon as I press 'Cancel'
          statreader.Close(); //this line hangs for a LONG time (~10 minutes) 
          

          I've tried calling Close() on the connection first. I've tried calling Dispose() instead, and I've tried not closing it at all to see what happens when it goes out of scope (statreader). No matter what, this reader hangs on close depening on how many records are in the reader. When I try it on a test database that returns only a few thousand records, Close() only takes a few seconds. When I try it on a database with about 400,000 records being returned, it takes about 10 minutes for Close() to finish executing.

          My Music | My Pics | My Articles BlackDice

          E Offline
          E Offline
          ednrgc
          wrote on last edited by
          #4

          Can you show me where you're opening the connection? I'm guessing this would shed more light. Are you creating the DataReader object with the option "commandbehavior.closeconnection"?

          B 2 Replies Last reply
          0
          • E ednrgc

            Can you show me where you're opening the connection? I'm guessing this would shed more light. Are you creating the DataReader object with the option "commandbehavior.closeconnection"?

            B Offline
            B Offline
            BlackDice
            wrote on last edited by
            #5

            The following comes directly before what I posted earlier:

             using (OdbcConnection conn = new OdbcConnection(source))
                        {
                           
                            conn.Open();
                            
            
                            statcmd = new OdbcCommand("select * from tblPeople", conn);
                                statcmd = new OdbcCommand(sql, conn);
                                OdbcDataReader statreader = statcmd.ExecuteReader();
            

            No, I'm not using "commandbehavior.closeconnection" option. Never knew it existed.

            My Music | My Pics | My Articles BlackDice

            E 1 Reply Last reply
            0
            • B BlackDice

              The following comes directly before what I posted earlier:

               using (OdbcConnection conn = new OdbcConnection(source))
                          {
                             
                              conn.Open();
                              
              
                              statcmd = new OdbcCommand("select * from tblPeople", conn);
                                  statcmd = new OdbcCommand(sql, conn);
                                  OdbcDataReader statreader = statcmd.ExecuteReader();
              

              No, I'm not using "commandbehavior.closeconnection" option. Never knew it existed.

              My Music | My Pics | My Articles BlackDice

              E Offline
              E Offline
              ednrgc
              wrote on last edited by
              #6

              Just a shot in the dark, can you use a OleDb instead of ODBC and see if the problem persists?

              B 2 Replies Last reply
              0
              • E ednrgc

                Can you show me where you're opening the connection? I'm guessing this would shed more light. Are you creating the DataReader object with the option "commandbehavior.closeconnection"?

                B Offline
                B Offline
                BlackDice
                wrote on last edited by
                #7

                I tried the commandbehavior.closeconnection option; didn't make a difference

                My Music | My Pics | My Articles BlackDice

                1 Reply Last reply
                0
                • E ednrgc

                  Just a shot in the dark, can you use a OleDb instead of ODBC and see if the problem persists?

                  B Offline
                  B Offline
                  BlackDice
                  wrote on last edited by
                  #8

                  I can try that, but I just never thought to since I'm using an ODBC connection (DSN). I'll let you know if there's any change.

                  My Music | My Pics | My Articles BlackDice

                  1 Reply Last reply
                  0
                  • E ednrgc

                    Just a shot in the dark, can you use a OleDb instead of ODBC and see if the problem persists?

                    B Offline
                    B Offline
                    BlackDice
                    wrote on last edited by
                    #9

                    just tried that; same result, except this time I got some weird error about not being able to switch from COM this-or-that for over 60 seconds.

                    My Music | My Pics | My Articles BlackDice

                    1 Reply Last reply
                    0
                    • B BlackDice

                      I have a problem when trying to close a connection or the odbcdatareader if I stop it from doing Read() before it would normally be done it hangs. I saw something similar when I Googled this a while ago, but can't find it now. Apparently, the while(dr.Read()) doesn't actually stop even though I used a break; to get out of the while() loop. Anybody run into this before?

                      My Music | My Pics | My Articles BlackDice

                      E Offline
                      E Offline
                      Eric Dahlvang
                      wrote on last edited by
                      #10

                      MSDN2: DataReader.Close Method [^] Remarks You should close data readers when the consumer has completed reading data or no longer wishes to read any more data. Closing the reader ensures that output parameters, if any, are populated. Depending on the provider implementation, the Close method may wait for the data reader to finish returning all of the specified data before closing. Compare to the Terminate[^] method. Suggestion: If you cannot find a provider that supports the Terminate method, you could split your SQL into chunks of 10,000 or so: select TOP 10000 * from tblPeople where PeopleID > @nLastID order by PeopleID Looping and creating a new reader each time until you're done, or the user cancels.

                      --EricDV Sig--------- Some problems are so complex that you have to be highly intelligent and well informed just to be undecided about them. - Laurence J. Peters

                      B 1 Reply Last reply
                      0
                      • E Eric Dahlvang

                        MSDN2: DataReader.Close Method [^] Remarks You should close data readers when the consumer has completed reading data or no longer wishes to read any more data. Closing the reader ensures that output parameters, if any, are populated. Depending on the provider implementation, the Close method may wait for the data reader to finish returning all of the specified data before closing. Compare to the Terminate[^] method. Suggestion: If you cannot find a provider that supports the Terminate method, you could split your SQL into chunks of 10,000 or so: select TOP 10000 * from tblPeople where PeopleID > @nLastID order by PeopleID Looping and creating a new reader each time until you're done, or the user cancels.

                        --EricDV Sig--------- Some problems are so complex that you have to be highly intelligent and well informed just to be undecided about them. - Laurence J. Peters

                        B Offline
                        B Offline
                        BlackDice
                        wrote on last edited by
                        #11

                        EricDV wrote:

                        Depending on the provider implementation, the Close method may wait for the data reader to finish returning all of the specified data before closing.

                        Right. I knew I remembered seeing that somewhere. However, the OdbcDataReader doesn't support the Terminate() method. I had already thought about the 'Top xxxx' method, but I was trying to avoid that if possible. Plus, on the database I'm doing it on now, there's about 3 million records, and it's already going to take hours to process those without getting all these different dataSets. Thanks for your help, though :(

                        My Music | My Pics | My Articles BlackDice

                        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