Results 1 to 9 of 9

Thread: SQL-connecton problem

  1. #1

    Thread Starter
    Lively Member
    Join Date
    May 2001
    Location
    Falkenberg, Sweden
    Posts
    76

    SQL-connecton problem

    I've got a problem on my aspx-page when I'm using SQL-connections.

    What I'm trying to do is this:

    1. Open a Connection (Conn1)
    2. Open a datareader (based on Conn1)
    3. Loop through the reader
    4. Run a SQLcommand inside the "reader-loop" and use the Conn1 as connection.

    But it doesn't work!

    I gets an error-msg where it says that conn1 is locked as long as the datareader uses it.

    Does this mean that I must create a new connetion-object for each command/reader a want to use in the same time? That sounds hard if I'm using nestled loops with readers/commands.

    Any ideas? Thanks in advance!
    ________________________
    Fredrik Klarqvist

  2. #2
    Frenzied Member Shawn N's Avatar
    Join Date
    Dec 2001
    Location
    Houston
    Posts
    1,631
    Shouldn't your data reader be based off a SqlCommand?
    Please rate my post.

  3. #3

    Thread Starter
    Lively Member
    Join Date
    May 2001
    Location
    Falkenberg, Sweden
    Posts
    76
    Yes, the reader is based on a command, but that command is based on a connection....
    ________________________
    Fredrik Klarqvist

  4. #4
    Frenzied Member Shawn N's Avatar
    Join Date
    Dec 2001
    Location
    Houston
    Posts
    1,631
    Try this:

    VB Code:
    1. Dim conSopon As SqlConnection
    2. Dim sqdCmd As SqlCommand
    3. Dim rdrSQL As SqlDataReader
    4.  
    5. Dim dsn As String = ConfigurationSettings.AppSettings("dsn")
    6. Dim cmbArray As New ArrayList()
    7.  
    8. conSopon = New SqlConnection(dsn)
    9. sqdCmd = New SqlCommand("SELECT pIndex FROM Referer", conSopon)
    10. sqdCmd.Connection.Open()
    11. rdrSQL = sqdCmd.ExecuteReader
    12.  
    13. Do While rdrSQL.Read
    14.      cmbArray.Add(Trim(rdrSQL("pIndex")))
    15. Loop
    16.  
    17. rdrSQL.Close()
    18. sqdCmd.Connection.Close()

    Are you invoking the "Close" method off the reader?
    Please rate my post.

  5. #5

    Thread Starter
    Lively Member
    Join Date
    May 2001
    Location
    Falkenberg, Sweden
    Posts
    76
    Yes, that works, but if you're using another command inside the loop, it doensn't work.

    Try this:

    Code:
    Dim strCN as string = "CONNECTION_STRING"
    Dim strQuery as string = "SELECT * FROM table"
    Dim cnSQL as new sqlConnection(strCN)
    Dim cmdSQL as sqlCommand(strQuery, cnSQL)
    Dim rdSQL as sqlDataReader
    
    dim cmdExecSQL as new sqlCommand(cnSQL)
    
    cnSQL.open
    rdSQL = cmdSQL.ExecuteReader
    
    do until rdSQL.read = false
    
    'Execute the second command-object
    cmdExecSQL.commandText = "INSERT INTO table2 (COL) VALUES ('" & rdSQL.items(0) &"')"
    cmdExecSQL.executeNonQuery
    
    loop
    
    cmdExecSQL.close
    
    rdSQL.close
    cnSQL.close
    This doesn't work, because it complains about that the connection can't execute the second Command, as it it in use by the reader...Any ideas?
    ________________________
    Fredrik Klarqvist

  6. #6
    Frenzied Member Shawn N's Avatar
    Join Date
    Dec 2001
    Location
    Houston
    Posts
    1,631
    Oh, I get you now. From the looks of it you're going to have to create a second command that isn't associated with the reader in the loop. That's the only thing I can think of.

    VB Code:
    1. Dim strCN as string = "CONNECTION_STRING"
    2. Dim strQuery as string = "SELECT * FROM table"
    3. Dim cnSQL as new sqlConnection(strCN)
    4. Dim cmdSQL as sqlCommand(strQuery, cnSQL)
    5. Dim rdSQL as sqlDataReader
    6.  
    7. dim cmdExecSQL as new sqlCommand(cnSQL)
    8. dim cmdExecSQL2 as new sqlCommand(cnSQL)
    9.  
    10. cnSQL.open
    11. rdSQL = cmdSQL.ExecuteReader
    12.  
    13. do until rdSQL.read = false
    14.  
    15. 'Execute the second command-object
    16. cmdExecSQL2.commandText = "INSERT INTO table2 (COL) VALUES ('" & rdSQL.items(0) &"')"
    17. cmdExecSQL2.executeNonQuery
    18.  
    19. loop
    20.  
    21. cmdExecSQL.close
    22.  
    23. rdSQL.close
    24. cnSQL.close
    Please rate my post.

  7. #7
    Junior Member
    Join Date
    Feb 2002
    Location
    London UK
    Posts
    29
    The datareader hogs the connection. When a datareader has a connection (ie before datareader.close) you cannot use that connection for anything else.

    Cheers
    Simon

  8. #8
    Lively Member
    Join Date
    Feb 2001
    Location
    KL Malaysia
    Posts
    64
    I think there's a more effecient way to execute the command... using stored procedures, and combination of stored procedures. It works perfectly in Access databases, I know SQL Server is more powerful, but I have not a slightest clue how to do it in SQL. That didn't quite help hehe. But exploring stored procedures is the way to go.

  9. #9

    Thread Starter
    Lively Member
    Join Date
    May 2001
    Location
    Falkenberg, Sweden
    Posts
    76
    Yes, I'm using Stored Procedures (just didn't use it in the example), but I still need a connection to execute them....

    Thanks anyway!
    ________________________
    Fredrik Klarqvist

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width