DataSet中表间的关系
在包含多个 DataTable 对象的 DataSet 中,可以使用 DataRelation 对象来使一个表与另一个表相关,在多个表之间导航,以及从相关表中返回子行或父行。
下面是一个主表和子表关系的示例:
Demo.aspx
运行结果如下图所示:
下面是一个主表和子表关系的示例:
Demo.aspx
<%@ Page language="c#" Codebehind="Demo.aspx.cs" AutoEventWireup="false" Inherits="ZKSTAT.Demo" %>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" >
<HTML>
<HEAD>
<title>Demo</title>
<meta name="GENERATOR" Content="Microsoft Visual Studio .NET 7.1">
<meta name="CODE_LANGUAGE" Content="C#">
<meta name="vs_defaultClientScript" content="JavaScript">
<meta name="vs_targetSchema" content="http://schemas.microsoft.com/intellisense/ie5">
<link href="Css/stu_css.css" rel="stylesheet" type="text/css">
</HEAD>
<body>
<form id="Form1" method="post" runat="server">
<FONT face="宋体"></FONT>
<DIV style="LEFT: 100px; WIDTH: 100%; POSITION: relative; HEIGHT: 100%" ms_positioning="FlowLayout"
id="DIV1" runat="server"></DIV>
</form>
</body>
</HTML>
Demo.aspx.cs<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" >
<HTML>
<HEAD>
<title>Demo</title>
<meta name="GENERATOR" Content="Microsoft Visual Studio .NET 7.1">
<meta name="CODE_LANGUAGE" Content="C#">
<meta name="vs_defaultClientScript" content="JavaScript">
<meta name="vs_targetSchema" content="http://schemas.microsoft.com/intellisense/ie5">
<link href="Css/stu_css.css" rel="stylesheet" type="text/css">
</HEAD>
<body>
<form id="Form1" method="post" runat="server">
<FONT face="宋体"></FONT>
<DIV style="LEFT: 100px; WIDTH: 100%; POSITION: relative; HEIGHT: 100%" ms_positioning="FlowLayout"
id="DIV1" runat="server"></DIV>
</form>
</body>
</HTML>
//***********************************************************
//*公司:浙江航大科技开发有限公司
//*作者:YK
//*模块:Demo
//*功能:表之间关系添加,导航表间关系
//*创建日期:
//*修改日期:
//***********************************************************
using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Web;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
using System.Data.SqlClient;
using System.Configuration;
using System.Text;
namespace ZKSTAT
{
/// <summary>
/// Demo 的摘要说明。
/// </summary>
public class Demo : System.Web.UI.Page
{
SqlConnection con = new SqlConnection(ConfigurationSettings.AppSettings["ConnectionString"].ToString());
DataSet ds;
protected System.Web.UI.HtmlControls.HtmlGenericControl DIV1;
StringBuilder stb = new StringBuilder();
private void Page_Load(object sender, System.EventArgs e)
{
// 在此处放置用户代码以初始化页面
this.GetData();
}
Web 窗体设计器生成的代码
private void GetData()
{
System.Data.SqlClient.SqlDataAdapter da = new SqlDataAdapter("SELECT * FROM Customers",con);
System.Data.SqlClient.SqlDataAdapter da1 =new SqlDataAdapter("SELECT * FROM ORDERS",con);
ds= new DataSet();
try
{
con.Open();
//将主表Customer添加到DataSet中
da.Fill(ds,"Customers");
//将子表Orders添加到DataSet中
da1.Fill(ds,"Orders");
//添加表Customer和表Orders之间的关系CustOrder;关联字段CustomerID
ds.Relations.Add("CustOrder",ds.Tables["Customers"].Columns["CustomerID"],ds.Tables["Orders"].Columns["CustomerID"]);
//当建立客户表Customer和订单表Orders之间的 DataRelation 后
//可以使用 DataRow.GetChildRows 检索特定客户行的所有订单行。
foreach (DataRow custRow in ds.Tables["Customers"].Rows)
{
//显示主表Customer记录
stb.Append("<H1>Customer</H1>");
stb.Append("<table>");
stb.Append("<tr class='tr'>");
stb.Append("<td>CustomerID</td>");
stb.Append("<td>CompanyName</td>");
stb.Append("<td>Address</td>");
stb.Append("<td>City</td>");
stb.Append("</tr>");
stb.Append("<tr>");
stb.Append("<td>"+custRow["CustomerID"].ToString()+"</td>");
stb.Append("<td>"+custRow["CompanyName"].ToString()+"</td>");
stb.Append("<td>"+custRow["Address"].ToString()+"</td>");
stb.Append("<td>"+custRow["City"].ToString()+"</td>");
stb.Append("</tr>");
stb.Append("</table>");
stb.Append("<H1>Order</H1>");
stb.Append("<table>");
stb.Append("<tr class='tr'>");
stb.Append("<td>OrderID</td>");
stb.Append("<td>OrderDate</td>");
stb.Append("<td>ShippedDate</td>");
stb.Append("</tr>");
//显示对应主表Customer下的Order记录.
foreach (DataRow orderRow in custRow.GetChildRows("CustOrder"))
{
stb.Append("<tr>");
stb.Append("<td>"+orderRow["OrderID"].ToString()+"</td>");
stb.Append("<td>"+orderRow["OrderDate"].ToString()+"</td>");
stb.Append("<td>"+orderRow["ShippedDate"].ToString()+"</td>");
stb.Append("</tr>");
}
stb.Append("</table>");
}
this.DIV1.InnerHtml = stb.ToString();
}
catch(Exception ex)
{
throw ex;
}
finally
{
con.Close();
}
}
}
}
//*公司:浙江航大科技开发有限公司
//*作者:YK
//*模块:Demo
//*功能:表之间关系添加,导航表间关系
//*创建日期:
//*修改日期:
//***********************************************************
using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Web;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
using System.Data.SqlClient;
using System.Configuration;
using System.Text;
namespace ZKSTAT
{
/// <summary>
/// Demo 的摘要说明。
/// </summary>
public class Demo : System.Web.UI.Page
{
SqlConnection con = new SqlConnection(ConfigurationSettings.AppSettings["ConnectionString"].ToString());
DataSet ds;
protected System.Web.UI.HtmlControls.HtmlGenericControl DIV1;
StringBuilder stb = new StringBuilder();
private void Page_Load(object sender, System.EventArgs e)
{
// 在此处放置用户代码以初始化页面
this.GetData();
}
Web 窗体设计器生成的代码
private void GetData()
{
System.Data.SqlClient.SqlDataAdapter da = new SqlDataAdapter("SELECT * FROM Customers",con);
System.Data.SqlClient.SqlDataAdapter da1 =new SqlDataAdapter("SELECT * FROM ORDERS",con);
ds= new DataSet();
try
{
con.Open();
//将主表Customer添加到DataSet中
da.Fill(ds,"Customers");
//将子表Orders添加到DataSet中
da1.Fill(ds,"Orders");
//添加表Customer和表Orders之间的关系CustOrder;关联字段CustomerID
ds.Relations.Add("CustOrder",ds.Tables["Customers"].Columns["CustomerID"],ds.Tables["Orders"].Columns["CustomerID"]);
//当建立客户表Customer和订单表Orders之间的 DataRelation 后
//可以使用 DataRow.GetChildRows 检索特定客户行的所有订单行。
foreach (DataRow custRow in ds.Tables["Customers"].Rows)
{
//显示主表Customer记录
stb.Append("<H1>Customer</H1>");
stb.Append("<table>");
stb.Append("<tr class='tr'>");
stb.Append("<td>CustomerID</td>");
stb.Append("<td>CompanyName</td>");
stb.Append("<td>Address</td>");
stb.Append("<td>City</td>");
stb.Append("</tr>");
stb.Append("<tr>");
stb.Append("<td>"+custRow["CustomerID"].ToString()+"</td>");
stb.Append("<td>"+custRow["CompanyName"].ToString()+"</td>");
stb.Append("<td>"+custRow["Address"].ToString()+"</td>");
stb.Append("<td>"+custRow["City"].ToString()+"</td>");
stb.Append("</tr>");
stb.Append("</table>");
stb.Append("<H1>Order</H1>");
stb.Append("<table>");
stb.Append("<tr class='tr'>");
stb.Append("<td>OrderID</td>");
stb.Append("<td>OrderDate</td>");
stb.Append("<td>ShippedDate</td>");
stb.Append("</tr>");
//显示对应主表Customer下的Order记录.
foreach (DataRow orderRow in custRow.GetChildRows("CustOrder"))
{
stb.Append("<tr>");
stb.Append("<td>"+orderRow["OrderID"].ToString()+"</td>");
stb.Append("<td>"+orderRow["OrderDate"].ToString()+"</td>");
stb.Append("<td>"+orderRow["ShippedDate"].ToString()+"</td>");
stb.Append("</tr>");
}
stb.Append("</table>");
}
this.DIV1.InnerHtml = stb.ToString();
}
catch(Exception ex)
{
throw ex;
}
finally
{
con.Close();
}
}
}
}
运行结果如下图所示: