DataSet中表间的关系

在包含多个 DataTable 对象的 DataSet 中,可以使用 DataRelation 对象来使一个表与另一个表相关,在多个表之间导航,以及从相关表中返回子行或父行。
下面是一个主表和子表关系的示例:
Demo.aspx
ExpandedBlockStart.gif ContractedBlock.gif <% dot.gif @ Page language="c#" Codebehind="Demo.aspx.cs" AutoEventWireup="false" Inherits="ZKSTAT.Demo"  %>
None.gif
<! DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN"  >
None.gif
< HTML >
None.gif    
< HEAD >
None.gif        
< title > Demo </ title >
None.gif        
< meta  name ="GENERATOR"  Content ="Microsoft Visual Studio .NET 7.1" >
None.gif        
< meta  name ="CODE_LANGUAGE"  Content ="C#" >
None.gif        
< meta  name ="vs_defaultClientScript"  content ="JavaScript" >
None.gif        
< meta  name ="vs_targetSchema"  content ="http://schemas.microsoft.com/intellisense/ie5" >
None.gif        
< link  href ="Css/stu_css.css"  rel ="stylesheet"  type ="text/css" >
None.gif    
</ HEAD >
None.gif    
< body >
None.gif        
< form  id ="Form1"  method ="post"  runat ="server" >
None.gif            
< FONT  face ="宋体" ></ FONT >
None.gif            
< DIV  style ="LEFT: 100px; WIDTH: 100%; POSITION: relative; HEIGHT: 100%"  ms_positioning ="FlowLayout"
None.gif                id
="DIV1"  runat ="server" ></ DIV >
None.gif        
</ form >
None.gif    
</ body >
None.gif
</ HTML >
Demo.aspx.cs
None.gif // ***********************************************************
None.gif
// *公司:浙江航大科技开发有限公司
None.gif
// *作者:YK
None.gif
// *模块:Demo
None.gif
// *功能:表之间关系添加,导航表间关系
None.gif
// *创建日期:
None.gif
// *修改日期:
None.gif
// ***********************************************************
None.gif
using  System;
None.gif
using  System.Collections;
None.gif
using  System.ComponentModel;
None.gif
using  System.Data;
None.gif
using  System.Drawing;
None.gif
using  System.Web;
None.gif
using  System.Web.SessionState;
None.gif
using  System.Web.UI;
None.gif
using  System.Web.UI.WebControls;
None.gif
using  System.Web.UI.HtmlControls;
None.gif
using  System.Data.SqlClient;
None.gif
using  System.Configuration;
None.gif
using  System.Text;
None.gif
namespace  ZKSTAT
ExpandedBlockStart.gifContractedBlock.gif
dot.gif {
ExpandedSubBlockStart.gifContractedSubBlock.gif    
/**//// <summary>
InBlock.gif    
/// Demo 的摘要说明。
ExpandedSubBlockEnd.gif    
/// </summary>

InBlock.gif    public class Demo : System.Web.UI.Page
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
InBlock.gif        SqlConnection con 
= new SqlConnection(ConfigurationSettings.AppSettings["ConnectionString"].ToString());
InBlock.gif        DataSet ds;
InBlock.gif        
protected System.Web.UI.HtmlControls.HtmlGenericControl DIV1;
InBlock.gif        StringBuilder stb 
= new StringBuilder();
InBlock.gif        
private void Page_Load(object sender, System.EventArgs e)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
// 在此处放置用户代码以初始化页面
InBlock.gif
            this.GetData();
ExpandedSubBlockEnd.gif        }

InBlock.gif
ContractedSubBlock.gifExpandedSubBlockStart.gif        
Web 窗体设计器生成的代码#region Web 窗体设计器生成的代码
InBlock.gif        
override protected void OnInit(EventArgs e)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
//
InBlock.gif            
// CODEGEN: 该调用是 ASP.NET Web 窗体设计器所必需的。
InBlock.gif            
//
InBlock.gif
            InitializeComponent();
InBlock.gif            
base.OnInit(e);
ExpandedSubBlockEnd.gif        }

InBlock.gif        
ExpandedSubBlockStart.gifContractedSubBlock.gif        
/**//// <summary>
InBlock.gif        
/// 设计器支持所需的方法 - 不要使用代码编辑器修改
InBlock.gif        
/// 此方法的内容。
ExpandedSubBlockEnd.gif        
/// </summary>

InBlock.gif        private void InitializeComponent()
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{    
InBlock.gif            
this.Load += new System.EventHandler(this.Page_Load);
InBlock.gif
ExpandedSubBlockEnd.gif        }

ExpandedSubBlockEnd.gif        
#endregion

InBlock.gif
InBlock.gif        
private void GetData()
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            System.Data.SqlClient.SqlDataAdapter da 
= new SqlDataAdapter("SELECT * FROM Customers",con);
InBlock.gif            System.Data.SqlClient.SqlDataAdapter da1 
=new SqlDataAdapter("SELECT * FROM ORDERS",con);
InBlock.gif            ds
= new DataSet();
InBlock.gif            
try
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{                
InBlock.gif                con.Open();
InBlock.gif                
//将主表Customer添加到DataSet中
InBlock.gif
                da.Fill(ds,"Customers");
InBlock.gif                
//将子表Orders添加到DataSet中
InBlock.gif
                da1.Fill(ds,"Orders");
InBlock.gif                
//添加表Customer和表Orders之间的关系CustOrder;关联字段CustomerID
InBlock.gif
                ds.Relations.Add("CustOrder",ds.Tables["Customers"].Columns["CustomerID"],ds.Tables["Orders"].Columns["CustomerID"]);
InBlock.gif                
//当建立客户表Customer和订单表Orders之间的 DataRelation 后
InBlock.gif                
//可以使用 DataRow.GetChildRows 检索特定客户行的所有订单行。        
InBlock.gif
                foreach (DataRow custRow in ds.Tables["Customers"].Rows)
ExpandedSubBlockStart.gifContractedSubBlock.gif                
dot.gif{    
InBlock.gif                    
//显示主表Customer记录
InBlock.gif
                    stb.Append("<H1>Customer</H1>");
InBlock.gif                    stb.Append(
"<table>");
InBlock.gif                    stb.Append(
"<tr class='tr'>");
InBlock.gif                    stb.Append(
"<td>CustomerID</td>");
InBlock.gif                    stb.Append(
"<td>CompanyName</td>");
InBlock.gif                    stb.Append(
"<td>Address</td>");
InBlock.gif                    stb.Append(
"<td>City</td>");
InBlock.gif                    stb.Append(
"</tr>");
InBlock.gif                    stb.Append(
"<tr>");
InBlock.gif                    stb.Append(
"<td>"+custRow["CustomerID"].ToString()+"</td>");
InBlock.gif                    stb.Append(
"<td>"+custRow["CompanyName"].ToString()+"</td>");
InBlock.gif                    stb.Append(
"<td>"+custRow["Address"].ToString()+"</td>");
InBlock.gif                    stb.Append(
"<td>"+custRow["City"].ToString()+"</td>");
InBlock.gif                    stb.Append(
"</tr>");
InBlock.gif                    stb.Append(
"</table>");
InBlock.gif                    stb.Append(
"<H1>Order</H1>");
InBlock.gif                    stb.Append(
"<table>");
InBlock.gif                    stb.Append(
"<tr class='tr'>");
InBlock.gif                    stb.Append(
"<td>OrderID</td>");
InBlock.gif                    stb.Append(
"<td>OrderDate</td>");
InBlock.gif                    stb.Append(
"<td>ShippedDate</td>");
InBlock.gif                    stb.Append(
"</tr>");
InBlock.gif                    
//显示对应主表Customer下的Order记录.
InBlock.gif
                    foreach (DataRow orderRow in custRow.GetChildRows("CustOrder"))
ExpandedSubBlockStart.gifContractedSubBlock.gif                    
dot.gif{
InBlock.gif                        stb.Append(
"<tr>");
InBlock.gif                        stb.Append(
"<td>"+orderRow["OrderID"].ToString()+"</td>");
InBlock.gif                        stb.Append(
"<td>"+orderRow["OrderDate"].ToString()+"</td>");
InBlock.gif                        stb.Append(
"<td>"+orderRow["ShippedDate"].ToString()+"</td>");
InBlock.gif                        stb.Append(
"</tr>");
ExpandedSubBlockEnd.gif                    }

InBlock.gif                    stb.Append(
"</table>");
ExpandedSubBlockEnd.gif                }

InBlock.gif                
InBlock.gif                
this.DIV1.InnerHtml = stb.ToString();
ExpandedSubBlockEnd.gif            }

InBlock.gif            
catch(Exception ex)
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                
throw ex;
ExpandedSubBlockEnd.gif            }

InBlock.gif            
finally
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                con.Close();
ExpandedSubBlockEnd.gif            }

ExpandedSubBlockEnd.gif        }

ExpandedSubBlockEnd.gif    }

ExpandedBlockEnd.gif}

None.gif

运行结果如下图所示:
41.JPG

转载于:https://www.cnblogs.com/yknb/archive/2006/06/28/437961.html

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值