System.Data Namespace
Provides a means of reading one or more forward-only streams of result sets obtained by executing a command at a data source, and is implemented by .NET data providers that access relational databases.
Class |
Description |
OleDbDataReader |
Provides a way of reading a forward-only stream of data rows from a data source. This class cannot be inherited. |
SqlDataReader |
Provides a means of reading a forward-only stream of rows from an SQL Server™ database. This class cannot be inherited. |
The IDataReader and IDataRecord interfaces allow an inheriting class to implement a DataReader class, which provides a means of reading one or more forward-only streams of result sets. For more information about using the DataReader class, see Retrieving Data Using a DataReader. For more information about implementing .NET data providers, see Implementing a .NET Data Provider.
An application does not create an instance of the IDataReader interface directly, but initializes an instance of a class that inherits IDataReader.
Classes that inherit IDataReader must implement the inherited members, and typically define additional members to add provider-specific functionality.
Notes to Implementers: To promote consistency among .NET data providers, name the inheriting class in the form Prv Command where Prv is the uniform prefix given to all classes in a specific .NET data provider namespace. For example, Sql is the prefix of the SqlDataReader class in the System.Data.SqlClient namespace.
Users do not create an instance of a DataReader class directly. Instead, they obtain the DataReader through the ExecuteReader method of the Command object. Therefore, you should mark DataReader constructors as internal.
The following example shows one way to implement a derived class, SqlDataReader, in a Web Forms page. Here, the entire logic is encapsulated in a Page_Load event handler that essentially:
- establishes a connection to the database,
- initializes an instance of a DataReader to contain the database information,
- and then enumerates the contents of the Datareader into an HTML table.
protected void Page_Load ( Object Src, EventArgs e ) {
// specify the data source
SqlConnection myConn = new SqlConnection (
"server=localhost; trusted_connection=yes; database=northwind" );
// define the command query
String query = "SELECT ProductName, ProductDescription FROM Products";
SqlCommand myCmd = new SqlCommand ( query, myConn );
// open the connection and instantiate a datareader
myConn.Open ( );
SqlDataReader myReader = myCmd.ExecuteReader ( );
// display datareader contents into html table
// first open the table and set up the table headers
html += "<table cellspacing=1 class='data' width=90%>";
html += "<tr>";
html += "<th>Product Name</th>";
html += "<th>Description</th>";
html += "</tr>";
// loop thru the reader
while ( myReader.Read ( ) ) {
html += "<tr>";
html += "<td>" + myReader.GetString ( 0 ) + "</td>";
html += "<td>" + myReader.GetString ( 1 ) + "</td>";
html += "</tr>";
}
// close the table
html += "</table>";
// close the reader and the connection
myReader.Close ( );
myConn.Close ( );
}
</script>
protected sub Page_Load ( ByVal Src as Object, ByVal E as EventArgs )
' specify the data source
dim myConn as new SqlConnection ( & _
"server=localhost; trusted_connection=yes; database=northwind" )
' define the command query
dim query as String = "SELECT ProductName, ProductDescription FROM Products"
dim myCmd as new SqlCommand ( query, myConn )
' open the connection and instantiate a datareader
myConn.Open ( )
dim myReader as SqlDataReader = myCmd.ExecuteReader ( )
' display datareader contents into html table
' first open the table and set up the table headers
html &= "<table cellspacing=1 class='data' width=90%>"
html &= "<tr>"
html &= "<th>Product Name</th>"
html &= "<th>Description</th>"
html &= "</tr>"
' loop thru the reader
while myReader.Read ( )
html &= "<tr>"
html &= "<td>" + myReader.GetString ( 0 ) + "</td>"
html &= "<td>" + myReader.GetString ( 1 ) + "</td>"
html &= "</tr>"
end while
' close the table
html &= "</table>"
' close the reader and the connection
myReader.Close ( )
myConn.Close ( )
end sub
</script> |
|
C# |
VB |
Show me