asp.net.ph

IDataReader Interface

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.

IDataReader Interface Members

Collapse   Properties

Visibility Name Value Type Accessibility
public Depth Int32 [ Get ]
public IsClosed Boolean [ Get ]
public RecordsAffected Int32 [ Get ]

Collapse   Methods

Visibility Name Parameters Return Type
public Close ( ) Void
public GetSchemaTable ( ) DataTable
public NextResult ( ) Boolean
public Read ( ) Boolean

Classes that Implement IDataReader


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.

Remarks

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.

Example

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:

  1. establishes a connection to the database,
  2. initializes an instance of a DataReader to contain the database information,
  3. 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>
  C# VB

 Show me 

Skip Navigation Links



Home
Suggested Reading


Previous page Back to top Next page

© 2000-2010 Rey Nuñez All rights reserved.

If you have any question, comment or suggestion
about this site, please send us a note

You can help support asp.net.ph