Home All Groups Group Topic Archive Search About

sqlreader Do...while problem

Author
27 Mar 2005 3:28 PM
Agnes
drBranchForm = sqldatareader
  For Each drBranch In dtUserBranch
                Do While drBranchForm.Read
                      .......... Problem (A)
                Loop
          Next

I found that the datarreader only loop onces , ?? How can I make it restart
the sqldatareader ???
Thanks

Author
27 Mar 2005 5:40 PM
Ken Tucker [MVP]
Hi,

        Here is an working example.  Would have to see more of your code to
figure out the problem you are having.

Dim conn As SqlConnection

Dim strConn As String

Dim drCustomer As SqlDataReader

Dim daCustomer As SqlDataAdapter

Dim cmd As SqlCommand

Dim ds As New DataSet

strConn = "Server = " & Environment.MachineName & ";"

strConn &= "Database = NorthWind;"

strConn &= "Integrated Security = SSPI;"

conn = New SqlConnection(strConn)

cmd = New SqlCommand("Select * from Customers", conn)

conn.Open()

drCustomer = cmd.ExecuteReader

Do While drCustomer.Read

Trace.WriteLine(drCustomer.Item("CustomerID").ToString)

Loop

conn.Close()



Ken

-----------------------------------

"Agnes" <ag***@dynamictech.com.hk> wrote in message
news:%232YzbGuMFHA.3164@TK2MSFTNGP10.phx.gbl...
drBranchForm = sqldatareader
  For Each drBranch In dtUserBranch
                Do While drBranchForm.Read
                      .......... Problem (A)
                Loop
          Next

I found that the datarreader only loop onces , ?? How can I make it restart
the sqldatareader ???
Thanks
Author
30 Mar 2005 11:25 PM
Gary Lam
Do you know if you have many transaction you need to call NextResult ??

again, need to provide us more code please ^_^ From http://www.developmentnow.com/g/38_2005_3_0_0_371001/sqlreader-Do---while-problem.htm Posted via DevelopmentNow.com Groups http://www.developmentnow.com
Author
30 Mar 2005 11:29 PM
Gary Lam
However you can just try something like this.


    Dim  drBranchForm As SqlDataReader
     drBranchForm = myCommand.ExecuteReader()
    ' Always call Read before accessing data.
    While  drBranchForm.Read()
        Console.WriteLine(( drBranchForm.GetInt32(0) & ", " &  drBranchForm.GetString(1)))
    End While
    ' always call Close when done reading.
     drBranchForm.Close()

take out the FOR EACH loop first.

Hope this can help ^_^
From http://www.developmentnow.com/g/38_2005_3_0_0_371001/sqlreader-Do---while-problem.htm Posted via DevelopmentNow.com Groups http://www.developmentnow.com