PC Review


Reply
Thread Tools Rate Thread

datareader questions

 
 
William
Guest
Posts: n/a
 
      8th Jul 2004
Can someone please explain this to me. I am not understanding why this
works the way it does. The commented lines are the problems

Dim oleEmp as OleDbCommand
Dim myFinds as OleDataReader
Dim strSearch as String
Dim strFound as String

strSearch = "select * from employee where [first name]='" &
txtName.Text.ToString & "'"

oleEmp = New OleDbCommand(strSearch, oleConn)

Try

'*********************************
' Why does the next line of code not fill the datareader
'*********************************
myFinds = oleEmp.ExecuteReader

'*********************************
' in order for it to work I had to perform a read
'myFinds.Read()
'as-is the code throws up an exception
'
' No Data exists for the row/column.
'*********************************
strFound = myFinds.Item(2)
MsgBox(txtName.Text.ToString + "'s last name is " + strFound

Catch prob as Exception

MsgBox(prob.Message)

End Try

If I uncomment the myFinds.Read() line then everything works fine but I'm
just not understand why it needs to read. It seems to me that when the
myFinds = oleEmp.ExecuteReader executes the datareader should have info to
use. Like I said this is more of a curiousity than a problem.

And now for my question: Does the datareader return a count of the records
that were returned? From the above code you can see why I want to know if
there is more than one record returned.

Thanks




 
Reply With Quote
 
 
 
 
Tom Shelton
Guest
Posts: n/a
 
      8th Jul 2004
In article <ZojHc.31057$(E-Mail Removed)>, William wrote:
> Can someone please explain this to me. I am not understanding why this
> works the way it does. The commented lines are the problems
>
> Dim oleEmp as OleDbCommand
> Dim myFinds as OleDataReader
> Dim strSearch as String
> Dim strFound as String
>
> strSearch = "select * from employee where [first name]='" &
> txtName.Text.ToString & "'"
>
> oleEmp = New OleDbCommand(strSearch, oleConn)
>
> Try
>
> '*********************************
> ' Why does the next line of code not fill the datareader
> '*********************************
> myFinds = oleEmp.ExecuteReader
>
> '*********************************
> ' in order for it to work I had to perform a read
> 'myFinds.Read()
> 'as-is the code throws up an exception
> '
> ' No Data exists for the row/column.
> '*********************************
> strFound = myFinds.Item(2)
> MsgBox(txtName.Text.ToString + "'s last name is " + strFound
>
> Catch prob as Exception
>
> MsgBox(prob.Message)
>
> End Try
>
> If I uncomment the myFinds.Read() line then everything works fine but I'm
> just not understand why it needs to read. It seems to me that when the
> myFinds = oleEmp.ExecuteReader executes the datareader should have info to
> use. Like I said this is more of a curiousity than a problem.
>
> And now for my question: Does the datareader return a count of the records
> that were returned? From the above code you can see why I want to know if
> there is more than one record returned.
>
> Thanks
>
>
>
>


The reason your seeing the behavior you are is that a DataReader (class
that implements IDataReader) is a forward-only stream based object. In
other words, unlike using a DataAdpater, you are using a connected
datasource. All the ExecuteReader method does, is construct a handle to
this datasource. The usual way of using a reader is something like:

myReader = myCommand.ExecuteReader()

Do While (myReader.Read())
' do stuff with the data
Loop

This can execute 0 to n Times depending on the number of rows
returned... As for getting the row count - that is not supported by the
IDataReader interface. I suppose it would be possible for a individual
datasource to implement an extension to the actual object returned (but,
I don't believe the OleDB provider does this). But, it would not be
part of the generic interface and would be datasource dependent... The
closest thing is the RecordsAffected property - and that is the number
of rows inserted, changed, or deleted by an SQL statement. Not at all
what your looking for

--
Tom Shelton [MVP]
 
Reply With Quote
 
Richard Myers
Guest
Posts: n/a
 
      9th Jul 2004

"William" <(E-Mail Removed)> wrote in message
news:ZojHc.31057$(E-Mail Removed)...
> Can someone please explain this to me. I am not understanding why this
> works the way it does. The commented lines are the problems
>
> Dim oleEmp as OleDbCommand
> Dim myFinds as OleDataReader
> Dim strSearch as String
> Dim strFound as String
>
> strSearch = "select * from employee where [first name]='" &
> txtName.Text.ToString & "'"
>
> oleEmp = New OleDbCommand(strSearch, oleConn)
>
> Try
>
> '*********************************
> ' Why does the next line of code not fill the datareader
> '*********************************
> myFinds = oleEmp.ExecuteReader
>
> '*********************************
> ' in order for it to work I had to perform a read
> 'myFinds.Read()
> 'as-is the code throws up an exception
> '
> ' No Data exists for the row/column.
> '*********************************
> strFound = myFinds.Item(2)
> MsgBox(txtName.Text.ToString + "'s last name is " + strFound
>
> Catch prob as Exception
>
> MsgBox(prob.Message)
>
> End Try
>
> If I uncomment the myFinds.Read() line then everything works fine but I'm
> just not understand why it needs to read. It seems to me that when the
> myFinds = oleEmp.ExecuteReader executes the datareader should have info

to
> use. Like I said this is more of a curiousity than a problem.
>
> And now for my question: Does the datareader return a count of the

records
> that were returned? From the above code you can see why I want to know if
> there is more than one record returned.
>


As Tomn said no. If you need to determine how many records are affected by a
given SELECT statement you should try ExecuteScaler with a count statement.
If you just want to know whether the reader has any records in it you use
the statement

If dr.read then

which returns a boolean.

The reason you need to call .read is that when the datareader is returned
the forward only cursor is set to before the first record... think of it as
being parked. For returning datareaders i use the same statement over and
over which covers most of my basis

dr = myCommand.ExecuteReader(CommandBehaviour.CloseConnection)

if dr is nothing orelse not dr.read then .... return or whatever

You should also be checking for a Null condition from the field you are
trying to read before you read it i.e

If not IsDbNull(myFinds.Item(2)) then
strFindFind = cstr(myFinds.Items(2))
End if


hth
Richard


 
Reply With Quote
 
Greg Burns
Guest
Posts: n/a
 
      9th Jul 2004
Don't forget that version 1.1 of .NET Framework added the HasRows property
to the datareader.

Greg

> If you just want to know whether the reader has any records in it you use
> the statement
>
> If dr.read then
>
> which returns a boolean.



 
Reply With Quote
 
 
 
Reply

Thread Tools
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
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
How to process datareader nulls coming from datareader? JB Microsoft C# .NET 4 3rd Nov 2008 12:56 AM
datareader reader questions =?Utf-8?B?TWlrZQ==?= Microsoft C# .NET 2 11th Aug 2005 12:25 PM
DataReader questions Norton Microsoft ADO .NET 1 16th Aug 2004 03:50 PM
DataReader =?Utf-8?B?Q2hyaXM=?= Microsoft ADO .NET 1 11th Mar 2004 10:41 PM
DataReader DaveS Microsoft VB .NET 2 15th Oct 2003 04:24 PM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 07:52 AM.