asp.net.ph

OleDbDataReader Class

System.Data.OleDb Namespace


Provides a way of reading a forward-only stream of data rows from a data source.

OleDbDataReader Class Members

Collapse   Properties

Visibility Name Value Type Accessibility
public Depth Int32 [ Get ]
public FieldCount Int32 [ Get ]
public HasRows Boolean [ Get ]
public IsClosed Boolean [ Get ]
public Item ( String name ) Object [ Get ]
public Item ( Int32 index ) Object [ Get ]
public RecordsAffected Int32 [ Get ]
public VisibleFieldCount Int32 [ Get ]

Collapse   Methods

Visibility Name Parameters Return Type
public Close ( ) Void
public GetBoolean ( Int32 ordinal ) Boolean
public GetByte ( Int32 ordinal ) Byte
public GetBytes ( Int32 ordinal , Int64 dataIndex , Byte buffer , Int32 bufferIndex , Int32 length ) Int64
public GetChar ( Int32 ordinal ) Char
public GetChars ( Int32 ordinal , Int64 dataIndex , Char buffer , Int32 bufferIndex , Int32 length ) Int64
public GetData ( Int32 ordinal ) OleDbDataReader
public GetDataTypeName ( Int32 index ) String
public GetDateTime ( Int32 ordinal ) DateTime
protected GetDbDataReader ( Int32 ordinal ) DbDataReader
public GetDecimal ( Int32 ordinal ) Decimal
public GetDouble ( Int32 ordinal ) Double
public GetEnumerator ( ) IEnumerator
public GetFieldType ( Int32 index ) Type
public GetFloat ( Int32 ordinal ) Single
public GetGuid ( Int32 ordinal ) Guid
public GetInt16 ( Int32 ordinal ) Int16
public GetInt32 ( Int32 ordinal ) Int32
public GetInt64 ( Int32 ordinal ) Int64
public GetName ( Int32 index ) String
public GetOrdinal ( String name ) Int32
public GetSchemaTable ( ) DataTable
public GetString ( Int32 ordinal ) String
public GetTimeSpan ( Int32 ordinal ) TimeSpan
public GetValue ( Int32 ordinal ) Object
public GetValues ( Object values ) Int32
public IsDBNull ( Int32 ordinal ) Boolean
public NextResult ( ) Boolean
public Read ( ) Boolean

Remarks

To create an OleDbDataReader, you must call the ExecuteReader method of the OleDbCommand object, rather than directly using a constructor.

While the OleDbDataReader is in use, the associated OleDbConnection is busy serving the OleDbDataReader, and no other operations can be performed on the OleDbConnection other than closing it. This is the case until the Close method of the OleDbDataReader is called.

IsClosed and RecordsAffected are the only properties that you can call after the OleDbDataReader is closed. Though the RecordsAffected property may be accessed at any time while the OleDbDataReader exists, always call Close before returning the value of RecordsAffected to ensure an accurate return value.

Example

The following example shows one way to implement an OleDbDataReader 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
   OleDbConnection myConn = new OleDbConnection (
      "Provider=Microsoft.ACE.OLEDB.12.0; Data Source=" + 
      Server.MapPath ( "~/app_data/dbtutor.mdb" ) );

   // define the command query
   String query = "SELECT ProductName, ProductDescription FROM Products";
   OleDbCommand myCommand = new OleDbCommand ( query, myConn );

   // open the connection and instantiate a datareader
   myConn.Open ( );
   OleDbDataReader myReader = myCommand.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