DataSet中表间的关系

在包含多个 DataTable 对象的 DataSet 中,可以使用 DataRelation 对象来使一个表与另一个表相关,在多个表之间导航,以及从相关表中返回子行或父行。
下面是一个主表和子表关系的示例:
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
// ***********************************************************
// *公司:浙江航大科技开发有限公司
// *作者: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
{
    
/**//// 
    
/// Demo 的摘要说明。
    
/// 

    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 窗体设计器生成的代码#region Web 窗体设计器生成的代码
        
override protected void OnInit(EventArgs e)
        
{
            
//
            
// CODEGEN: 该调用是 ASP.NET Web 窗体设计器所必需的。
            
//
            InitializeComponent();
            
base.OnInit(e);
        }

        
        
/**//// 
        
/// 设计器支持所需的方法 - 不要使用代码编辑器修改
        
/// 此方法的内容。
        
/// 

        private void InitializeComponent()
        
{    
            
this.Load += new System.EventHandler(this.Page_Load);

        }

        
#endregion


        
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("

Customer

");
                    stb.Append(
"");
                    stb.Append(
"");
                    stb.Append(
"");
                    stb.Append(
"");
                    stb.Append(
"");
                    stb.Append(
"");
                    stb.Append(
"");
                    stb.Append(
"");
                    stb.Append(
"");
                    stb.Append(
"");
                    stb.Append(
"");
                    stb.Append(
"");
                    stb.Append(
"");
                    stb.Append(
"
CustomerIDCompanyNameAddressCity
"+custRow["CustomerID"].ToString()+""+custRow["CompanyName"].ToString()+""+custRow["Address"].ToString()+""+custRow["City"].ToString()+"
");
                    stb.Append(
"

Order

");
                    stb.Append(
"");
                    stb.Append(
"");
                    stb.Append(
"");
                    stb.Append(
"");
                    stb.Append(
"");
                    stb.Append(
"");
                    
//显示对应主表Customer下的Order记录.
                    foreach (DataRow orderRow in custRow.GetChildRows("CustOrder"))
                    
{
                        stb.Append(
"
");
                        stb.Append(
"");
                        stb.Append(
"");
                        stb.Append(
"");
                        stb.Append(
"");
                    }

                    stb.Append(
"
OrderIDOrderDateShippedDate
"+orderRow["OrderID"].ToString()+""+orderRow["OrderDate"].ToString()+""+orderRow["ShippedDate"].ToString()+"
");
                }

                
                
this.DIV1.InnerHtml = stb.ToString();
            }

            
catch(Exception ex)
            
{
                
throw ex;
            }

            
finally
            
{
                con.Close();
            }

        }

    }

}


运行结果如下图所示:
DataSet中表间的关系_第1张图片

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

你可能感兴趣的:(DataSet中表间的关系)