<%@ Import Namespace="System.Data" %>
<%@ Import Namespace="System.Data.SqlClient" %>
<html>
<head>
<title>SqlCommand.ExecuteReader Example</title>
<link href="/shared/netdemos.css" rel="stylesheet">
<script language="C#" runat="server" src="/shared/fetchData_sql.cs" />
<script language="C#" runat="server">
string html;
protected void Page_Load ( Object src, EventArgs e ) {
// connect to data source
SqlConnection myConn = new SqlConnection (
getConnection ( "aspnet" ) );
// define the command query
String query = "SELECT CustomerID, CompanyName FROM Customers";
SqlCommand myCommand = new SqlCommand ( query, myConn );
// open the connection and instantiate a datareader
myConn.Open ( );
SqlDataReader 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=80%>";
html += "<tr>";
html += "<th>Customer ID</td>";
html += "<th><b>Company Name</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: <span class="hilite">SqlCommand.ExecuteReader</span> 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>