473,320 Members | 1,861 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

About datareader.read()

I have some code like this:
SqlCommand cmd = new SqlCommand("get_storeid_byuser", cn);

cmd.CommandType = CommandType.StoredProcedure;
cmd.Parameters.Add("@username", SqlDbType.VarChar, 50).Value =
"osamede";
cn.Open();
SqlDataReader reader = cmd.ExecuteReader();

The StoredProcedure "get_storeid_byuser" should return a int.

I just want to know Why I have to call reader.read() before to read
data:
reader.read();
int value=(int)reader["id"]
But not
int value=(int)reader["id"];

Jul 12 '07 #1
3 10579
"Osamede.Zhang" <Os***********@gmail.comwrote in message
news:11**********************@22g2000hsm.googlegro ups.com...
The StoredProcedure "get_storeid_byuser" should return a int.

I just want to know Why I have to call reader.read() before to read
data:
reader.read();
int value=(int)reader["id"]
But not
int value=(int)reader["id"];
Because that's just the way it works...

With a DataReader, you read each record one at a time, and reading one
record advances the reader to the next etc...

However, when a DataReader is first opened, it hasn't fetched any records
yet, although it does know if the underlying query which was used to
populate it has returned any records or not - you inspect this in the
..HasRows property.

So, therefore you need to call the Read() method to advance to the first
record.
--
Mark Rae
ASP.NET MVP
http://www.markrae.net

Jul 12 '07 #2
this is because sqlserver returns resultsets. each resultset contains n
rows of n columns of data. you start at first resultset before the first
row. the reader is actually at the resultset where it can read meta info
about the rows.

remember the data is all coming back from a network stream (think of it
as reading a file, and you start at byte 0). after the last resultset
comes the return value of the proc.

-- bruce (sqlwork.com)
Osamede.Zhang wrote:
I have some code like this:
SqlCommand cmd = new SqlCommand("get_storeid_byuser", cn);

cmd.CommandType = CommandType.StoredProcedure;
cmd.Parameters.Add("@username", SqlDbType.VarChar, 50).Value =
"osamede";
cn.Open();
SqlDataReader reader = cmd.ExecuteReader();

The StoredProcedure "get_storeid_byuser" should return a int.

I just want to know Why I have to call reader.read() before to read
data:
reader.read();
int value=(int)reader["id"]
But not
int value=(int)reader["id"];
Jul 12 '07 #3
Osamede.Zhang wrote:
I have some code like this:
SqlCommand cmd = new SqlCommand("get_storeid_byuser", cn);

cmd.CommandType = CommandType.StoredProcedure;
cmd.Parameters.Add("@username", SqlDbType.VarChar, 50).Value =
"osamede";
cn.Open();
SqlDataReader reader = cmd.ExecuteReader();

The StoredProcedure "get_storeid_byuser" should return a int.

I just want to know Why I have to call reader.read() before to read
data:
reader.read();
int value=(int)reader["id"]
But not
int value=(int)reader["id"];
The DataReader does not start by reading the first record automatically.
You use the Read method both for reading and checking if there are any
more data to read.

You can compare this to the Recordset object in ADO, which automatically
reads the first record. This leaves the object in two possible states,
and you have to check the EOF property to see if the object is usable or
not. There are thousands of questions in forums about the error message
that people get because they are trying to read from a recordset that
doesn't contain any data.

Also, the method MoveNext is used to read the next record, and you check
the EOF property to see if there were any more data. It's quite common
to forget the MoveNext call in a loop, which will cause the loop to read
the first record over and over a billion times, either using 100% for
several minutes until the script times out, or creating a result that
fills the memory. There are thousand of questions in forums about this
too...

Compared to that, the Read method is quite convenient. It's MoveNext and
EOF rolled into one, so you can't forget to step forward. Also the
DataReader is always in the same state from the start. If you forget to
call Read, you will always get the same error message, you don't get
different results for different data.

--
Göran Andersson
_____
http://www.guffa.com
Jul 12 '07 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: rob | last post by:
I'm having some trouble with this.. My VB datareader seems to skip the first record in the reader. I've looked at my query in enterprise manager to confirm what I am suppose to receive back,...
6
by: Ravi | last post by:
Hi, I am not able to understand why a datareader needs a connection to the DB all the time. Here is what I tried. Sqlcommand cmd = ("select * from table1",con) // where con is the connection...
12
by: Thomas Scheiderich | last post by:
I have 2 dropdowns that are exactly the same and I don't want to re-read for each. Is there a way to do something like below where I read the data, bind to depCity1 and then bind to destCity2. ...
20
by: Mark | last post by:
Hi all, quick question , a DataView is memory resident "view" of data in a data table therefore once populated you can close the connection to the database. Garbage collection can then be used to...
1
by: Brent | last post by:
I'm having a hard time wrapping my head around how to build a multi-dimensional array of n length out of a DataReader loop. Take this pseudo-code: ======================================= public...
2
by: Sam anonymous | last post by:
This is what I have so far but if someone could show me how to read through the datareader once I can get the rest. Thank you. Private Sub lstCoursesOffered_SelectedIndexChanged(ByVal sender...
3
by: William | last post by:
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...
3
by: Ian | last post by:
I wish to use a datareader to loop through the result set of a stored procedure. (Several resultsets actually.) It's for rendering HTML, so I don't need to ever keep this data, just stream...
3
by: Johnny Jörgensen | last post by:
I've got an error that I simply cannot locate: I've got a form in which I use a datareader object to read information from a db. After the read, I close the reader and I dispose of both the...
0
by: DolphinDB | last post by:
The formulas of 101 quantitative trading alphas used by WorldQuant were presented in the paper 101 Formulaic Alphas. However, some formulas are complex, leading to challenges in calculation. Take...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
0
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.