C#语言中的组件Datarelation使用实例代码详解

在C#语言中的组件包括有DataSet(DataTable、DataRow、DataColumn、DataRelation、Constraints、DataView)。
Datarelation是基于公共键建立父(主)表和子(详细资料)表之间的关系。
Datarelation的作用在于可以使与正在使用的记录相关的记录可用(如使用父记录时提供子记录,如使用子记录则提供父记录);然后还可以强制约束的引用完整性(如删除父记录时同时也删除相关的子记录)。

eg:
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 DataRelation : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        //先来建立ds数据库
        DataSet ds = new DataSet("ds");
        //再来建立tbClass和tbBoard两个数据表
        DataTable tbClass = new DataTable("tbClass");
        DataTable tbBoard = new DataTable("tbBoard");
        //把两个数据表tbClass和tbBoard加入数据库
        ds.Tables.Add(tbClass);
        ds.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}", i);
            //把行加入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);

        }

        //构建父子关系
        ds.Relations.Add("RelationBetweenClassAndBoard", ClassID, BoardClassID);
        //从关系获取父表
        DataTable dtParent = ds.Relations["RelationBetweenClassAndBoard"].ParentTable;
        //从关系获取子表
        DataTable dtChild = ds.Relations["RelationBetweenClassAndBoard"].ChildTable;
        //构建输出字符串
        System.Text.StringBuilder htmlStr = new System.Text.StringBuilder();
        //表开始
        htmlStr.Append("<table border='1' cellpadding='5' cellSpacing='0' style='font-size:9pt;font:宋体'>");
        //遍历父表中所有行
        for (int i = 0; i < dtParent.Rows.Count; i++)
        {
            //父表数据行开始
            htmlStr.Append("<tr style='background-color=#f0f0f0'>");
            //遍历父表行中列
            for (int j = 0; j < dtParent.Columns.Count; j++)
            {
                if (!dtParent.Rows[i].IsNull(j))
                    htmlStr.Append(string.Format("<td>{0}</td>", dtParent.Rows[i][j]));
            }
            //父表数据结束
            htmlStr.Append("</tr>");
            //遍历表中所有行
            for (int j = 0; j < dtParent.Rows[i].GetChildRows("RelationBetweenClassAndBoard").Length; j++)
            {
                //子表数据行开始
                htmlStr.Append("<tr>");
                //遍历子表行中列
                for (int k = 0; k < dtParent.Columns.Count; k++)
                {
                    if (!dtParent.Rows[i].GetChildRows("RelationBetweenClassAndBoard")[j].IsNull(k))
                        htmlStr.Append(string.Format("<td>{0}</td>", dtParent.Rows[i].GetChildRows("RelationBetweenClassAndBoard")[j][k]));
                }
                //子表数据行结束
                htmlStr.Append("</tr>");
            }
             
        }
        //表结束
        htmlStr.Append("</table><br>");
        Response.Write(htmlStr);
    }

}

上述代码运行后的结果图如下:

长沙做网站

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值