Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chriss Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

DataReader Row Count 1

Status
Not open for further replies.

LonnieJohnson

Programmer
Apr 16, 2001
2,628
US
Is there a property or method that will let me know how many rows are in a data reader before I iterate through all the rows when running a function?

ProDev, MS Access Applications
Visit me at ==>
May God bless you beyond your imagination!!!
 
Nope not a change you will have to loop twice once to count second time to do what you want.

Christiaan Baes
Belgium

If you want to get an answer read this FAQ faq796-2540
There's no such thing as a winnable war - Sting
 
Thanks. Not what I wanted to hear, but what I needed to hear. I tried doing that and don't seem to be able to excecute the second loop.

I get a count now, but my actions are not happening see code
Do I have to reset the reader somehow? I'm a novice at .Net. Thanks.

Code:
        Dim cnn As New SqlConnection(ConnString("sec"))
        Dim cmd As New SqlCommand(strSQL, cnn)
        Dim dr As SqlDataReader

        cnn.Open()

        Dim cnt As Integer

        dr = cmd.ExecuteReader(CommandBehavior.CloseConnection)

        Do While dr.Read()
            cnt += 1
        Loop

        MsgBox(cnt)

        Do While dr.Read()
            MsgBox(dr("userID"))
        Loop

ProDev, MS Access Applications
Visit me at ==>
May God bless you beyond your imagination!!!
 
yep you will have to reset the datareader I think that you will have to close it before you can refill it

so

Code:
Dim cnn As New SqlConnection(ConnString("sec"))
        Dim cmd As New SqlCommand(strSQL, cnn)
        Dim dr As SqlDataReader

        cnn.Open()

        Dim cnt As Integer

        dr = cmd.ExecuteReader(CommandBehavior.CloseConnection)

        Do While dr.Read()
            cnt += 1
        Loop

        MsgBox(cnt)

        dr.close
        dr = cmd.ExecuteReader(CommandBehavior.CloseConnection)

        Do While dr.Read()
            MsgBox(dr("userID"))
        Loop

Christiaan Baes
Belgium

If you want to get an answer read this FAQ faq796-2540
There's no such thing as a winnable war - Sting
 
Datareaders are forward only read. I personally like DataReaders, as they work faster against the back end Database than the dataadaptor does, and dont come with what for me is unneccassary baggage such as the insert/update/delete commands.

A good tip also is to develop a routine which converts a datareader to a datatable. All that you need to do to accomplish this is create the table schema from the datareaders columnms collection, and then read through the datareader and subsequently populate the table.




Sweep
...if it works dont mess with it
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top