转自:http://www.cnblogs.com/csdm/archive/2010/02/02/1661808.html
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Dataset.aspx.cs" Inherits="Dataset" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title>遍历Dataset并输出数据</title>
</head>
<body>
<form id="form1" runat="server">
<div>
</div>
</form>
</body>
</html>
CS:
using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Data.SqlClient;
public partial class Dataset : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
//先来建立数据库
DataSet Forum = new DataSet("Forum");
//再来建立两个数据表
DataTable tbClass = new DataTable("tbClass");
DataTable tbBoard = new DataTable("tbBoard");
//把两个数据表加入数据库
Forum.Tables.Add(tbClass);
Forum.Tables.Add(tbBoard);
//建立tbClass两列
DataColumn ClassID = new DataColumn("ClassID", typeof(System.String));
DataColumn ClassName = new DataColumn("ClassName", typeof(System.String));
//设定ClassID列不允许为空
ClassID.AllowDBNull = false;
//把列加入tbClass表
tbClass.Columns.Add(ClassID);
tbClass.Columns.Add(ClassName);
//设定tdClass表的主键
tbClass.PrimaryKey = new DataColumn[] { ClassID };
//建立tbBoard的三列
DataColumn BoardID = new DataColumn("BoardID", typeof(System.String));
DataColumn BoardName = new DataColumn("BoardName", typeof(System.String));
DataColumn BoardClassID = new DataColumn("BoardClassID", typeof(System.String));
//设定BoardID列不允许为空
BoardID.AllowDBNull = false;
//把列加入tbBoard表
tbBoard.Columns.Add(BoardID);
tbBoard.Columns.Add(BoardName);
tbBoard.Columns.Add(BoardClassID);
//设定tbBoard表的主键
tbBoard.PrimaryKey = new DataColumn[] { BoardID };
// 为两个表各加入5条记录
for (int i = 1; i <= 5; i++)
{
//实例化tbClass表的行
DataRow tbClassRow = tbClass.NewRow();
//为行中每一列赋值
tbClassRow["ClassID"] = Guid.NewGuid();
tbClassRow["ClassName"] = string.Format("分类{0}", 1);
//把行加入tbClass表
tbClass.Rows.Add(tbClassRow);
//实例化tbBoard表的行
DataRow tbBoardRow = tbBoard.NewRow();
//为行中每一列赋值
tbBoardRow["BoardID"] = Guid.NewGuid();
tbBoardRow["BoardName"] = string.Format("版块{0}", i);
tbBoardRow["BoardclassID"] = tbClassRow["ClassID"];
//把行加入tbBoard表
tbBoard.Rows.Add(tbBoardRow);
}
//DataRow tbClassRow = tbClass.NewRow();
//for (int i = 1; i <= 5; i++)
//{
// tbClassRow["ClassID"] = Guid.NewGuid();
// tbClassRow["ClassName"] = string.Format("分类{0}", i);
// tbClass.Rows.Add(tbClassRow);
//}
//构建输出字符串
System.Text.StringBuilder htmlStr = new System.Text.StringBuilder();
//遍历Forum中的所有表(使用foreach)遍历
foreach (DataTable dt in Forum.Tables)
{
//表开始
htmlStr.Append("<table border='1' cellspacing='0' cellPadding='5' style='font-size:9pt;font:宋体'>");
//表头开始
htmlStr.Append("<tr style='backgound-color:#F0F0F0'>");
//遍历表中所有列(使用for遍历)
for (int i = 0; i < dt.Columns.Count; i++)
{
htmlStr.Append(string.Format("<td><strong>{0}</strong></td>", dt.Columns[i].ColumnName));
}
//表头结束
htmlStr.Append("</tr>");
//遍历表中所有行
for (int i = 0; i < dt.Rows.Count; i++)
{
//数据行开始
htmlStr.Append("<tr>");
//遍历行中列
for (int j = 0; j < dt.Columns.Count; j++)
{
if (!dt.Rows[i].IsNull(j))
htmlStr.Append(string.Format("<td>{0}</td>", dt.Rows[i][j]));
}
//数据行结束
htmlStr.Append("</tr>");
}
//表结束
htmlStr.Append("</table><br>");
}
Response.Write(htmlStr);
}
}