In
ADO.NET, a DataReader is a broad category of objects used to sequentially read data from a data source.
DataReaders provide a very efficient way to access data, and can be thought of as a
Firehose cursor from
ASP Classic, except that no
server-side
In the client–server model, server-side refers to programs and operations that run on the server. This is in contrast to client-side programs and operations which run on the client.
General concepts
Typically, a server is a computer applicati ...
cursor
Cursor may refer to:
* Cursor (user interface), an indicator used to show the current position for user interaction on a computer monitor or other display device
* Cursor (databases), a control structure that enables traversal over the records i ...
is used. A DataReader parses a
Tabular Data Stream Tabular Data Stream (TDS) is an application layer protocol used to transfer data between a database server and a client. It was initially designed and developed by Sybase Inc. for their Sybase SQL Server relational database engine in 1984, and later ...
from
Microsoft SQL Server
Microsoft SQL Server is a relational database management system developed by Microsoft. As a database server, it is a software product with the primary function of storing and retrieving data as requested by other software applications—which ...
, and other methods of retrieving data from other sources.
A DataReader is usually accompanied by a Command object that contains the query, optionally any parameters, and the connection object to run the query on.
DataReader types
There is no DataReader class in
ADO.NET, but there are a number of classes that implement the
IDataReader interface:
*
*
*
DataReaders have a small footprint and good performance because each is tailor-made to the task at hand, however this makes it more difficult to write an application that can be moved from one backend data source to another. Some provider-specific DataReaders expose types used by the underlying database - for example, values can be null in Microsoft SQL Server, but not in the
.NET Framework
The .NET Framework (pronounced as "''dot net"'') is a proprietary software framework developed by Microsoft that runs primarily on Microsoft Windows. It was the predominant implementation of the Common Language Infrastructure (CLI) until bein ...
prior to version 2.0.
Strong vs weak typing
When using a DataReader to retrieve data, the developer can choose to read field values in strongly typed manner ( ''example: '' ) or a weakly typed manner, returning then as s ( ''example:'' ). Both approaches have their pros and cons.
Using the strongly typed retrieval methods can be more cumbersome, especially without specific knowledge of the underlying data. Numeric values in the database can translate to several
.NET
The domain name net is a generic top-level domain (gTLD) used in the Domain Name System of the Internet. The name is derived from the word ''network'', indicating it was originally intended for organizations involved in networking technologies ...
types: , or . Trying to retrieve a value using the wrong type results in an exception being thrown, which stops code from running further, and slows the application down. This is also true when you use the right type, but encounter a value ( ''this can be avoided by using the boolean function of the DataReader class'' ). The benefit to this retrieval method is that data validation is performed sooner, improving the probability of data correction being possible.
Weakly typed data retrieval allows for quick code writing, and allows for the data to be used in some fashion when the developer doesn't know beforehand what types will be returned. Further, with some effort, the programmer can extract the value into a variable of the proper type by using the or methods of the DataReader.
Common errors
A DataReader can in some cases be used in place of a
DataTable, however many programmers have experienced
connection bloat
Connection may refer to:
Mathematics
* Connection (algebraic framework)
* Connection (mathematics), a way of specifying a derivative of a geometrical object along a vector field on a manifold
* Connection (affine bundle)
*Connection (composite b ...
when following this approach. A DataReader can only be used against an (already) open
database connection A database connection is a facility in computer science that allows client software to talk to database server software, whether on the same machine or not. A connection is required to send commands and receive answers, usually in the form of a res ...
; that connection isn't closed until the DataReader's method is called. If an exception is thrown while the data is being processed, for example as described in ''
Strong and weak typing
In computer programming, one of the many ways that programming languages are colloquially classified is whether the language's type system makes it strongly typed or weakly typed (loosely typed). However, there is no precise technical definition o ...
'', above, the method will never be called if the developer writes code explicitly declaring and disposing the DataReader without the use of a - block. The
C# construct is a good way to avoid this problem, as shown below in the code example.
Sample code
Sample of accessing SQL Data using DataReader
void DataTest()
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.Odbc;
using MySql.Data.MySqlClient;
namespace ConsoleApplication1;
class Program
References
{{DEFAULTSORT:Datareader
SQL data access
ADO.NET Data Access technologies
Microsoft application programming interfaces