<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="DataReaderMultiple" %>
Untitled Page
File: Default.aspx.cs
using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Text;
using System.Data.SqlClient;
public partial class DataReaderMultiple : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
string connectionString = "Data Source=localhost;Initial Catalog=Northwind;Integrated Security=SSPI";
SqlConnection con = new SqlConnection(connectionString);
string sql = @"SELECT TOP 5 EmployeeID, FirstName, LastName FROM Employees;" +
"SELECT TOP 5 ContactName, ContactTitle FROM Customers;" +
"SELECT TOP 5 SupplierID, CompanyName, ContactName FROM Suppliers";
SqlCommand cmd = new SqlCommand(sql, con);
con.Open();
SqlDataReader reader = cmd.ExecuteReader();
StringBuilder htmlStr = new StringBuilder("");
int i = 0;
do
{
htmlStr.Append("Rowset: ");
htmlStr.Append(i.ToString());
htmlStr.Append("
");
while (reader.Read())
{
htmlStr.Append("");
for (int field = 0; field < reader.FieldCount; field++)
{
htmlStr.Append(reader.GetName(field).ToString());
htmlStr.Append(": ");
htmlStr.Append(reader.GetValue(field).ToString());
htmlStr.Append(" ");
}
htmlStr.Append(" ");
}
htmlStr.Append("
");
i++;
} while (reader.NextResult());
reader.Close();
con.Close();
HtmlContent.Text = htmlStr.ToString();
}
}
File: Web.config