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
11
votes
5 answers

Slow performance of SqlDataReader

I've query executing ~2 secs in MSSMS (returning 25K of rows) Same query used in .NET (sqlReader) exetuting few minutes! I've also tried to execute only reader (commented all code in while loop just leaving reader.Read() ) - still same! Any idea…
Maciej
  • 10,423
  • 17
  • 64
  • 97
11
votes
8 answers

How to get table name of a column from SqlDataReader

I have an SQL query I get from a configuration file, this query usually contains 3-6 joins. I need to find at run time, based on the result set represented by SqlDataReader, to find the name of the table for each column. Here are some thing that…
Nir
  • 29,306
  • 10
  • 67
  • 103
11
votes
2 answers

SqlDataReader inside SqlDataReader

How can I implement a SqlDataReader inside another SqlDataReader? My problem is I have a SqlDataReader. I am issuing while (reader.read()) and inside the while loop I have to create another SqlDataReader to read from the database. But I'm getting…
scatman
  • 14,109
  • 22
  • 70
  • 93
11
votes
10 answers

DataReader or DataSet when pulling multiple recordsets in ASP.NET

I've got an ASP.NET page that has a bunch of controls that need to be populated (e.g. dropdown lists). I'd like to make a single trip to the db and bring back multiple recordsets instead of making a round-trip for each control. I could bring back…
Chris Burgess
  • 5,787
  • 13
  • 54
  • 69
11
votes
2 answers

Timeout exception causes SqlDataReader to close?

I'm trying to pull some binary data from a database and write them to pdf files. For the most part, this is going along swimmingly, but the occasional row of data seems to throw a particular error - Timeout expired. The timeout period elapsed…
Paulie
  • 113
  • 1
  • 1
  • 5
10
votes
2 answers

'ExecuteReader requires an open and available Connection. The connection's current state is open'

A fairly large web application written in C# keeps throwing up 2 errors: 'ExecuteReader requires an open and available Connection. The connection's current state is open.' and 'Invalid attempt to call Read when reader is closed.' These errors were…
shauneba
  • 2,122
  • 2
  • 22
  • 32
10
votes
3 answers

How to make streams from BLOBs available in plain old C# objects when using SqlDataReader?

This is the scenario: We store files, e.g. relatively large documents (10-300MB), in blobs in our MSSQL database. We have a very small domain model so we use the clean SqlDataReader approach for our repository, instead of an ORM, to avoid…
Geir Sørensen
  • 103
  • 1
  • 7
10
votes
3 answers

Using SQLDataReader instead of recordset

I am new to this and had this question. Can I use SQLDataReader instead of a Recordset. I want to achieve the following result in an SQLDataReader. Dim dbConn As New ADODB.Connection Dim rs As New ADODB.Recordset Dim sqlstr As String = "SELECT…
nerts
  • 101
  • 1
  • 1
  • 3
10
votes
2 answers

How To perform a SQL Query to DataTable Operation That Can Be Cancelled

I tried to make the title as specific as possible. Basically what I have running inside a backgroundworker thread now is some code that looks like: SqlConnection conn = new SqlConnection(connstring); SqlCommand cmd = new…
David W
  • 487
  • 2
  • 5
  • 12
9
votes
9 answers

How to safely cast nullable result from sqlreader to int?

I have a table which contains null values and I need to get data from the table using SqlDataReader. I can't figure out how I can safely cast DBNull to int. I'm doing it in this way at the moment: ... reader = command.ExecuteReader(); while…
Burjua
  • 12,506
  • 27
  • 80
  • 111
9
votes
2 answers

asp.net sql datareader loop by columns

I have sql query in my asp.net webapp and the result is stored in datareader. Every row in datareader contains 10 columns. I want to fill table with these data. However, I have no idea, how to loop through columns in datareader. I need something…
polohy
  • 277
  • 1
  • 6
  • 17
9
votes
2 answers

Slow SQL data retrieval with SqlDataReader.Read() in C# vs SSMS

I am doing a simple SQL query to get lots of data. The complexity of the query is not an issue. It takes around 200ms to execute. However the amount of data seems to be the issue. We retrieve around 40k rows. Each row has 8 columns and the amount…
Kevin
  • 180
  • 3
  • 7
9
votes
2 answers

How to get the exact type of numeric columns incl. scale and precision?

Is there a way to know the exact type of a column in a DataTable? Right now I am doing this: DataTable st = dataReader.GetSchemaTable(); foreach (DataColumn col in st.Columns) { var type = col.DataType; } Now with type.Name I am able to find if…
IFlyHigh
  • 546
  • 2
  • 9
  • 20
8
votes
3 answers

ExecuteReader requires an open and available Connection. The connection's current state is closed

Ok, I asked about this very error earlier this week and had some very helpful answers and without doubt things have drastically improved since I started following the suggestions. However, now I am using the 'correct', best practice method to access…
dooburt
  • 3,010
  • 10
  • 41
  • 59
8
votes
1 answer

How can I get return value of stored procedure when using SqlDataReader?

Is it not possible to get the return value of a stored procedeure when using a datareader? The return value is always null, but the SP returns a valid int from within SSMS. myCommand.CommandText = "GetVenueVideos"; SqlParameter retVal = new…
NickG
  • 9,315
  • 16
  • 75
  • 115