<%@ Import Namespace="System.Data" %>
<%@ Import Namespace="System.Data.OleDb" %>
<html>
<head>
<title>OleDbCommand.ExecuteReader Example</title>
<link href="/shared/netdemos.css" rel="stylesheet">
<script language="C#" runat="server">
string html;
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>
</head>
<body>
<!-- #include virtual="~/shared/top.inc" -->
<div class="header"><h2>ADO.NET Primer: <b>OleDbCommand.ExecuteReader</b> Example</h2></div>
<hr size=1 width=92%>
<p align="center">
<%= html %>
</p>
<hr size=1 width=92%>
<!-- #include virtual="~/shared/viewsrc.inc" -->
</body>
</html>