ADO.NET_03_DataSet
本文内容
- 添加表
- 读取XML文件
添加表
<%@ Page Language="C#" %>
<%@ Import Namespace="System.Data" %>
<!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></title>
<script runat="server">1:
2: protected void Page_Load(object sender, EventArgs e)3: {
4: DataSet ds = new DataSet();5: DataTable dtEmp = ADODotNet.DS.CreateEmpDataTable();
6: DataTable dtDept = ADODotNet.DS.CreateDeptDataTable();
7: ds.Tables.Add(dtEmp);
8: ds.Tables.Add(dtDept);
9: this.PrintRows(ds.Tables["emp"], "EMP表:");10: this.PrintRows(ds.Tables["dept"], "DEPT表:");11: }
12: public void PrintRows(DataTable table, string label)13: {
14: Response.Write(label + "<br />");15: if (table.Rows.Count <= 0)16: {
17: Response.Write("没有数据.");18: return;19: }
20: foreach (DataRow dr in table.Rows)21: {
22: Response.Write(dr[0].ToString() + ", " + dr[1].ToString() + "<br />");23: }
24: Response.Write("<br />");25: }
26:
</script>
</head>
<body>
<form id="form1" runat="server">
<div>
</div>
</form>
</body>
</html>
读取XML文件
<%@ Page Language="C#" %>
<%@ Import Namespace="System.Data" %>
<!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></title>
<script runat="server">1:
2: protected void Page_Load(object sender, EventArgs e)3: {
4: DataSet ds = new DataSet();5: ds.ReadXml(Server.MapPath("book.xml"));6: this.PrintRows(ds.Tables[0], "XML文件内容:");7: }
8: public void PrintRows(DataTable table, string label)9: {
10: Response.Write(label + "<br />");11: if (table.Rows.Count <= 0)12: {
13: Response.Write("没有数据.");14: return;15: }
16: foreach (DataRow dr in table.Rows)17: {
18: Response.Write(dr[0].ToString() + ", " + dr[1].ToString() + "<br />");19: }
20: Response.Write("<br />");21: }
22:
</script>
</head>
<body>
<form id="form1" runat="server">
<div>
</div>
</form>
</body>
</html>
自定义XML文件 book.xml
<?xml version="1.0" encoding="utf-8" standalone="yes" ?>
<PUBLICATION>
<BOOK>
<TITLE>C#数据库系统开发完全手册</TITLE>
<PAGES>628</PAGES>
</BOOK>
<BOOK>
<TITLE>ASP.NET程序开发范例宝典</TITLE>
<PAGES>820</PAGES>
</BOOK>
</PUBLICATION>
说明
1) XML文件的路径,既可以用绝对路径,也可以用 Server.MapPath 本地路径;
2) 当然也可以将DataSet中的内容写成XML文件的形式,这只需一个WriteXml方法即可。