Questions tagged [sqldatareader]

Provides a way of reading a forward-only stream of rows from a SQL Server database.

The SqlDataReader is a member of the .NET framework's System.Data.SqlClient family responsible for reading data from a SQL database. The SqlDataReader is created by calling the ExecuteReader() method of the SqlCommand object, instead of directly using a constructor.

While the SqlDataReader is being used, the associated SqlConnection is busy serving the SqlDataReader, and no other operations can be performed on the SqlConnection other than closing it. This is the case until the Close method of the SqlDataReader is called. For example, you cannot retrieve output parameters until after you call Close.

Changes made to a result set by another process or thread while data is being read may be visible to the user of the SqlDataReader. However, the precise behavior is timing dependent.

For optimal performance, SqlDataReader avoids creating unnecessary objects or making unnecessary copies of data. Therefore, multiple calls to methods such as GetValue return a reference to the same object. Use caution if you are modifying the underlying value of the objects returned by methods such as GetValue.

References

MSDN Article

1132 questions
361
votes
30 answers

SQL Data Reader - handling Null column values

I'm using a SQLdatareader to build POCOs from a database. The code works except when it encounters a null value in the database. For example, if the FirstName column in the database contains a null value, an exception is thrown. employee.FirstName =…
DenaliHardtail
  • 27,362
  • 56
  • 154
  • 233
321
votes
11 answers

Can you get the column names from a SqlDataReader?

After connecting to the database, can I get the name of all the columns that were returned in my SqlDataReader?
Blankman
  • 259,732
  • 324
  • 769
  • 1,199
240
votes
27 answers

Check for column name in a SqlDataReader object

How do I check to see if a column exists in a SqlDataReader object? In my data access layer, I have create a method that builds the same object for multiple stored procedures calls. One of the stored procedures has an additional column that is not…
Michael Kniskern
  • 24,792
  • 68
  • 164
  • 231
178
votes
13 answers

Read data from SqlDataReader

I have a SQL Server 2008 database and I am working on it in the backend. I am working on asp.net/C# SqlDataReader rdr = cmd.ExecuteReader(); while (rdr.Read()) { //how do I read strings here???? } I know that the reader has…
zack
  • 7,115
  • 14
  • 53
  • 63
110
votes
6 answers

Invalid attempt to read when no data is present

private void button1_Click(object sender, EventArgs e) { string name; name = textBox5.Text; SqlConnection con10 = new SqlConnection("con strn"); SqlCommand cmd10 = new SqlCommand("select * from sumant where…
knowledgehunter
  • 1,345
  • 4
  • 11
  • 10
106
votes
6 answers

How to get number of rows using SqlDataReader in C#

My question is how to get the number of rows returned by a query using SqlDataReader in C#. I've seen some answers about this but none were clearly defined except for one that states to do a while loop with Read() method and increment a counter. My…
Tomasz Iniewicz
  • 4,379
  • 6
  • 42
  • 47
60
votes
13 answers

how to check if a datareader is null or empty

I have a datareader that return a lsit of records from a sql server database. I have a field in the database called "Additional". This field is 50% of the time empty or null. I am trying to write code that checks if this field isnull. The logic…
Jason
  • 4,899
  • 12
  • 47
  • 56
54
votes
4 answers

Multiples Table in DataReader

I normally use DataSet because It is very flexible. Recently I am assigned code optimization task , To reduce hits to the database I am changing two queries in a procedure. one Query returns the count and the other returns the actual data. That is…
muhammad kashif
  • 2,566
  • 3
  • 26
  • 49
53
votes
3 answers

What does the buffered parameter do in Dapper dot net?

Dapper dot net has a buffer parameter (a bool), but as far as I can tell the only thing it does is cast the result to a list before returning it. As per the documentation: Dapper's default behavior is to execute your sql and buffer the entire …
smdrager
  • 7,327
  • 6
  • 39
  • 49
46
votes
6 answers

SqlDataReader Get Value By Column Name (Not Ordinal Number)

Using the methods of the SqlDataReader, I can get the value of a column by passing in it's ordinal, such as the value of the first column if I pass in read.GetValue(0), or the second column if I pass in read.GetValue(1). In looking at the methods,…
user9927
  • 599
  • 1
  • 4
  • 4
43
votes
13 answers

convert from SqlDataReader to JSON

public string toJSON(SqlDataReader o) { StringBuilder s = new StringBuilder(); s.Append("["); if (o.HasRows) while (o.Read()) s.Append("{" + '"' + "Id" + '"' + ":" + o["Id"] + ", " + '"' + "CN" + '"' + ":"…
Rawhi
  • 6,155
  • 8
  • 36
  • 57
43
votes
11 answers

Is there anything faster than SqlDataReader in .NET?

I need to load one column of strings from table on SqlServer into Array in memory using C#. Is there a faster way than open SqlDataReader and loop through it. Table is large and time is critical. EDIT I am trying to build .dll and use it on server…
watbywbarif
  • 6,487
  • 8
  • 50
  • 64
41
votes
4 answers

How to check if SQLDataReader has no rows

I am trying to figure out how to check if my SqlDataReader is null or has no rows (meaning the reservation does not exist) and then display a messagebox. For some reason when I debug once it hits the While dr.Read()) code it steps out if it does not…
Tim
  • 1,209
  • 4
  • 21
  • 33
34
votes
5 answers

SqlDataReader - How to convert the current row to a dictionary

Is there an easy way to convert all the columns of the current row of a SqlDataReader to a dictionary? using (SqlDataReader opReader = command.ExecuteReader()) { // Convert the current row to a dictionary } Thanks
Martin
  • 39,309
  • 62
  • 192
  • 278
31
votes
5 answers

How to (efficiently) convert (cast?) a SqlDataReader field to its corresponding c# type?

First, let me explain the current situation: I'm reading records from a database and putting them in an object for later use; today a question about the database type to C# type conversion (casting?) arose. Let's see an example: namespace Test { …
Albireo
  • 10,977
  • 13
  • 62
  • 96
1
2 3
75 76