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
31
votes
1 answer

How to read multiple resultset from SqlDataReader?

I have a SP from that I am trying to return 2 result set from, and in my .cs file i am trying something like this: dr = cmd.ExecuteReader(); while (dr.Read()) { RegistrationDetails regDetails = new RegistrationDetails() { FName =…
Rocky
  • 4,454
  • 14
  • 64
  • 119
31
votes
2 answers

What 'length' parameter should I pass to SqlDataReader.GetBytes()

I have a SqlDataReader and need to read a varbinary(max) column from it using the SqlDataReader.GetBytes() method. This method populates a byte array and therefore needs to know what length of data to read. This is where I get confused.. Clearly I…
Martyn
  • 1,446
  • 2
  • 19
  • 30
29
votes
5 answers

SqlDataReader vs SqlDataAdapter: which one has the better performance for returning a DataTable?

I want to know which one has the better performance for returning a DataTable. Here for SqlDataReader I use DataTable.Load(dr) Using SqlDataReader: public static DataTable populateUsingDataReader(string myQuery) { DataTable dt = new…
Satinder singh
  • 10,100
  • 16
  • 60
  • 102
26
votes
4 answers

SQLDataReader Row Count

I am trying to get the number of rows that were returned by iterating the reader. But I always get 1 when I run this code? Did I screw up something in this? int count = 0; if (reader.HasRows) { while (reader.Read()) { count++; …
atrljoe
  • 8,031
  • 11
  • 67
  • 110
26
votes
6 answers

How to implement one to many relationship

I have a one to many relationship coming from a stored procedure. I have several one to many relationships in the query and i am trying to map these fields to a C# object. The problem i am having is i get duplicate data because of the one to many…
Luke101
  • 63,072
  • 85
  • 231
  • 359
25
votes
5 answers

How to get a bit value with SqlDataReader and convert it to bool?

I am retrieving user information from a database using a simple query. select * from dbo.[User] u where u.Email = @email I then try to get the value of a column, called IsConfirmed (which is represented as a bit type column in the database) and…
Yulian
  • 6,262
  • 10
  • 65
  • 92
24
votes
5 answers

Should I call SqlDataReader.HasRows if I am calling SqlReader.Read

Trying to see if it is beneficial to add an if (dr.HasRows) before the while (dr.read()) function. I mean, technically if it doesn't have rows it isn't going to read, so would it matter if you checked this first? using (SqlDataReader dr =…
Joshua Volearix
  • 313
  • 1
  • 2
  • 11
22
votes
4 answers

Why use the GetOrdinal() Method of the SqlDataReader

What's the difference between reading a value from an SqlDataReader using this syntax: Dim reader As SqlClient.SqlDataReader reader("value").ToString() OR Dim reader As SqlClient.SqlDataReader reader.GetString(reader.GetOrdinal("value"))
Tesseract
  • 1,547
  • 4
  • 15
  • 16
21
votes
3 answers

Enforce only single row returned from DataReader

I seem to write this quite a lot in my code: using (var reader = cmd.ExecuteReader()) { if (reader.Read()) { result = new User((int)reader["UserId"], reader["UserName"].ToString()); } if (reader.Read()) { throw…
fearofawhackplanet
  • 52,166
  • 53
  • 160
  • 253
21
votes
2 answers

yield return vs. return IEnumerable

I've noticed something curious about reading from an IDataReader within a using statement that I can't comprehend. Though I'm sure the answer is simple. Why is it that whilst inside the using (SqlDataReader rd) { ... } if I directly perform a yield…
pim
  • 12,019
  • 6
  • 66
  • 69
21
votes
5 answers

Unable to cast object of type 'System.Int32' to type 'System.String' in DataReader.GetString()

I was trying to add data from a database to a ComboBox. try { SqlCeCommand com = new SqlCeCommand("select * from Category_Master", con); SqlCeDataReader dr = com.ExecuteReader(); while(dr.Read()) { string name =…
Kamal
  • 469
  • 2
  • 8
  • 19
20
votes
6 answers

How to get nullable DateTime out of the database

My SQL Server database contains nullable DateTime values. How can I convert them to a nullable DateTime object in my application in C#? This is what I would think it would look like, but it doesn't: DateTime? dt = (DateTime?) sqldatareader[0];
pikachu
  • 1,031
  • 2
  • 11
  • 20
20
votes
2 answers

Million inserts: SqlBulkCopy timeout

We already have a running system that handles all connection-strings (db2, oracle, MSServer). Currently, We are using ExecuteNonQuery() to do some inserts. We want to improve the performance, by using SqlBulkCopy() instead of ExecuteNonQuery(). We…
billybob
  • 2,859
  • 6
  • 35
  • 55
20
votes
4 answers

Why is DataTable faster than DataReader

So we have had a heated debate at work as to which DataAccess route to take: DataTable or DataReader. DISCLAIMER I am on the DataReader side and these results have shaken my world. We ended up writing some benchmarks to test the speed differences.…
Shai Cohen
  • 6,074
  • 4
  • 31
  • 54
19
votes
3 answers

Make DbDataReader start reading again from the beginning of the result set

How to make dr.Read(); start reading again from the beginning if a condition is satisfied? Something like: SqlDataReader dr = command.ExecuteReader(); for(int i=0; dr.Read() ; i++){ if(condition ){ //let dr.Read() start reading from the…
Aan
  • 12,247
  • 36
  • 89
  • 150
1
2
3
75 76