CodeGuru Forums -
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic Newsletters VB Forums Developer.com


Newest CodeGuru.com Articles:

  • Installing SQL Server 2008
  • Writing UDFs for Firebird Embedded SQL Server
  • [Updated] Shutdown Manager
  • Building Windows Azure Cloud Service Applications with Azure Storage and the Azure SDK

  • Search CodeGuru:
     



    Go Back   CodeGuru Forums > .NET Programming > ADO.NET
    FAQ Members List Calendar Search Today's Posts Mark Forums Read

    ADO.NET Question or answer on topics related to the ADO technology.

    Reply
     
    Thread Tools Search this Thread Rate Thread Display Modes
      #1    
    Old October 29th, 2009, 04:34 AM
    Lars_V_J's Avatar
    Lars_V_J Lars_V_J is offline
    Member
     
    Join Date: Jun 2001
    Location: Denmark
    Posts: 274
    Lars_V_J is an unknown quantity at this point (<10)
    Problems with running cursor and insert

    Hi,

    I have a cursor running (a reader), and then I want to try to insert things that I find into another table.

    I then get
    There is already an open DataReader associated with this Connection which must be closed first.

    Why can't I have a reader running on one table while inserting into another?
    I run MSSQL 2000.

    Also, I've heard that I can't have two active readers running on a MSSQL 2000 at the same time. I sometimes have nested cursor loops, so I hope there's a way around this.

    Thanks in advance
    Reply With Quote
      #2    
    Old October 31st, 2009, 04:19 AM
    Alsvha Alsvha is offline
    Member +
     
    Join Date: Feb 2005
    Location: Denmark
    Posts: 601
    Alsvha is a glorious beacon of light (400+)Alsvha is a glorious beacon of light (400+)Alsvha is a glorious beacon of light (400+)Alsvha is a glorious beacon of light (400+)Alsvha is a glorious beacon of light (400+)
    Re: Problems with running cursor and insert

    If you use a "reader" (datareader) it is is most languages a read only - forward only - structure.
    So you can't insert into that reader for those reasons.
    Furthermore that reader "claims" a connection to the database, so you can't reuse the connection until that connection is closed (closing the reader), which is why you get the message that you get.

    Depending on what you need, you need to use a different architecture or methodology.
    You might just need to create another connection, you can use for updating the other table.
    It is a "problem" with readers, that you need to be careful with the connections and closing the reader after use, because otherwise you'll run out of possible connections to the database. Readers are fast, but they have their restrictions and should be used with care.

    Last edited by Alsvha; October 31st, 2009 at 04:22 AM.
    Reply With Quote
      #3    
    Old October 31st, 2009, 05:10 AM
    Lars_V_J's Avatar
    Lars_V_J Lars_V_J is offline
    Member
     
    Join Date: Jun 2001
    Location: Denmark
    Posts: 274
    Lars_V_J is an unknown quantity at this point (<10)
    Re: Problems with running cursor and insert

    I now push things into a DataSet. But I think that the reader is nearly useless, then. It is understandable that it can't allow writing to the table you read from. But it most certainly ought to be possible to change another table.
    If one have to make another connection, then transactions will become useless.

    But the DataSet works, fortunately :-)

    Thank you for your time :-)
    Reply With Quote
      #4    
    Old November 1st, 2009, 03:40 AM
    Alsvha Alsvha is offline
    Member +
     
    Join Date: Feb 2005
    Location: Denmark
    Posts: 601
    Alsvha is a glorious beacon of light (400+)Alsvha is a glorious beacon of light (400+)Alsvha is a glorious beacon of light (400+)Alsvha is a glorious beacon of light (400+)Alsvha is a glorious beacon of light (400+)
    Re: Problems with running cursor and insert

    A reader is far from useless, for example you use a reader to populate the dataset
    If you need things done in a transaction, then several methods exists for this as well, right from a transaction scope in .NET down to the transactions in the database.

    The issue with a reader is it is a structure made for fast reading and nothing else, and to facilitate that, it needs/consumes a connection until closed. (A common cause of leaks in many solutions)
    When you need more complex/other things done, other tools exists and must(can) be used.
    Reply With Quote
    Reply

    Bookmarks
    Go Back   CodeGuru Forums > .NET Programming > ADO.NET


    Thread Tools Search this Thread
    Search this Thread:

    Advanced Search
    Display Modes Rate This Thread
    Rate This Thread:

    Posting Rules
    You may not post new threads
    You may not post replies
    You may not post attachments
    You may not edit your posts

    BB code is On
    Smilies are On
    [IMG] code is On
    HTML code is Off
    Forum Jump


    All times are GMT -5. The time now is 09:55 AM.



    Acceptable Use Policy

    internet.comMediabistrojusttechjobs.comGraphics.com

    WebMediaBrands Corporate Info


    Advertise | Newsletters | Feedback | Submit News

    Legal Notices | Licensing | Permissions | Privacy Policy


    Powered by vBulletin® Version 3.7.3
    Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.
    Copyright WebMediaBrands Inc. 2002-2009