Create DataRelation ship between two tables

 There is a simple demo for DataRelation class of DataSet.

while u can use DataTable.Select(conditions) or DataView.Filter to filete the data what you want .however,maybe the method is more complex with this one.

 


        datatalbe performance test#region datatalbe performance test

        /**//// <summary>
        /// Test table relation.
        /// </summary>
        public void Relation()
        ...{
            DataTable dtParent = this.CreateDataTable();
            dtParent.TableName = "Parent";

            .net framework 2.0#region .net framework 2.0
            DataSet ds = new DataSet();
            ds.Tables.Add(dtParent);
            DataRelation relations = ds.Relations.Add("Relation", ds.Tables["Parent"].Columns["index"], ds.Tables["Parent"].Columns["parent_index"], false);

            foreach (DataRow row in ds.Tables["Parent"].Rows)
            #endregion

            .net framework 1.1#region .net framework 1.1
            //DataRelation relations = new DataRelation("Relation", dtParent.Columns["index"], dtParent.Columns["parent_index"], false);
            //foreach (DataRow row in dtParent.Rows)
            #endregion
            ...{
                if (0 != string.Compare(row["parent_index"].ToString(), "0", true,System.Globalization.CultureInfo.CurrentCulture))
                ...{
                    continue;
                }

                this.DebugPrint(string.Format("{0}", row["description"].ToString()));

                this.PrintChilds(row, relations);

                this.DebugPrint("--------------------------------------------------");

                
            }
        }

        /**//// <summary>
        /// print the child 
        /// </summary>
        private void PrintChilds(DataRow rowParent, DataRelation relations)
        ...{
            DataRow[] rowChilds = rowParent.GetChildRows(relations);

            if (null == rowChilds || 0 == rowChilds.Length)
            ...{
                return;
            }

            string spaces = string.Empty;
            foreach (DataRow row in rowChilds)
            ...{
                spaces = spaces.PadLeft(int.Parse(row["layer"].ToString()), ' ');
                this.DebugPrint(string.Format("{0}|-- {1}", spaces, row["description"].ToString()));

                this.PrintChilds(row, relations);                
            }
        }

        /**//// <summary>
        /// Create the datatable for test.
        /// </summary>
        /// <returns></returns>
        private DataTable CreateDataTable()
        ...{
            DataTable dt = new DataTable("Test");

            // add columns
            dt.Columns.Add(new DataColumn("index", typeof(Int32)));
            dt.Columns.Add(new DataColumn("parent_index", typeof(Int32)));
            dt.Columns.Add(new DataColumn("description", typeof(string)));
            dt.Columns.Add(new DataColumn("layer", typeof(string)));

            // add rows
            dt.Rows.Add(new object[] ...{ 1000, 0,     "first 1" , 0});
            dt.Rows.Add(new object[] ...{ 1001, 1000, "secord 2", 1 });
            dt.Rows.Add(new object[] ...{ 2000, 0, "third 3", 0 });
            dt.Rows.Add(new object[] ...{ 2001, 1000, "forth 4", 2 });
            dt.Rows.Add(new object[] ...{ 3000, 2000, "fifth 5", 1 });
            dt.Rows.Add(new object[] ...{ 3001, 3000, "six 6", 2 });
            dt.Rows.Add(new object[] ...{ 2222, 0, "seven 7", 0 });


            dt.Rows.Add(new object[] ...{ 100, 2222, "first 11", 1 });
            dt.Rows.Add(new object[] ...{ 101, 1000, "secord 21", 1 });
            dt.Rows.Add(new object[] ...{ 200, 222, "third 31", 3 });
            dt.Rows.Add(new object[] ...{ 201, 1001, "forth 41", 2 });
            dt.Rows.Add(new object[] ...{ 300, 2000, "fifth 51", 1 });
            dt.Rows.Add(new object[] ...{ 301, 3000, "six 61", 2 });
            dt.Rows.Add(new object[] ...{ 222, 1000, "seven 71", 3 });

            // accept changes
            dt.AcceptChanges();

            //DataSet ds = new DataSet("Test");
            //ds.Tables.Add(dt);

            return dt;
        }

        private void DebugPrint(string msg)
        ...{
            //System.Diagnostics.Debug.WriteLine(msg);
            Console.WriteLine(msg);
        }
        #endregion

 

 

时间: 2024-08-24 08:11:11

Create DataRelation ship between two tables的相关文章

13.1.17 CREATE TABLE Syntax

13.1.17 CREATE TABLE Syntax   13.1.17.1 CREATE TABLE ... LIKE Syntax 13.1.17.2 CREATE TABLE ... SELECT Syntax 13.1.17.3 Using FOREIGN KEY Constraints 13.1.17.4 Silent Column Specification Changes CREATE [TEMPORARY] TABLE [IF NOT EXISTS] tbl_name (cre

Display hierarchical data with TreeView

treeview Display hierarchicaldata with TreeViewTeemu Keiski   Very oftenapplications have need to manage hierarchical data and indeed such thathierarchy is unlimited. This has been a bit problem in databases, althoughsolved using self-referencing tab

SQL Server 2005中的DDL触发器的实现

server|触发器     SQL SERVER 2005中,新增加了许多新的特性,其中的DDL触发器是个不错的选择,根据资料初步学习如下,现整理之:    在sql server 2000中,只能为针对表发出的 DML 语句(INSERT.UPDATE 和 DELETE)定义 AFTER 触发器.SQL Server 2005 可以就整个服务器或数据库的某个范围为 DDL 事件定义触发器.可以为单个 DDL 语句(例如,CREATE_TABLE)或者为一组语句(例如,DDL_DATABASE

运用 ADO.NET 对象优化数据查询代码

ado|对象|数据|优化    毫无疑问,ADO.NET 向人们提供了一种功能强大.仿真数据库的对象模型,它可以将数据记录保存到内存中.尤其是ADO.net 的 DataSet 类,它不但在功能上相当于数据库表的集中存储器(central repository),而且支持表间的各种约束和逻辑关系.进一步说来,DataSet 对象其实是一种离线数据容器.     乍一看,只要把 DataSet 类的所有特性联合起来,就能消除 SQL 查询命令中的复杂子句,比如那些泛滥成灾且层层嵌套的 INNER

使用 ADO.NET 访问 Oracle 9i 存储过程

ado|oracle|存储过程|访问 摘要:使用 Microsoft .NET 框架 1.1 中的 Microsoft .NET Oracle 提供程序访问 Oracle 存储过程和函数. 本文讨论了如何使用 ADO.NET 访问 Oracle 存储过程(称为 SQL 编程块)和函数(返回单个值的编程块). 您可以使用以下托管数据提供程序连接到 Oracle 数据库:Microsoft .NET Oracle 提供程序.OLE DB .NET 提供程序.ODBC .NET 数据提供程序以及 Or

Oracle常用数据字典

oracle|数据  作者:佚名 来源:InterNet 加入时间:2003-7-20 以下表格中收集了Oracle数据字典中几乎所有的视图或别名,至于每个视图或别名的字段请用'Describe'语句来查询. 命名说明: Oracle数据字典中,对象名称多数以"USER.","ALL.","DBA."为前缀"USER."视图中记录通常记录执行查询的帐户所拥有的对象的信息,"ALL."视图中记录包括"

ADO.NET深入研究(2)[特别推荐]

ado 导 读:.NET 在数据存取方面做了很大的调整.在.NET 框架下,数据存取是由ADO.NET来完成的,这是一个ADO的改进和完善版本.它最显著的变化是其完全基于XML.而对于从事ADO开发的人员来说,Recordset对象的消失也令他们感到惊奇. -------------------------------------------------------------------------------- 翻译整理:.net技术网(www.51dotnet.com)slash 原文出处

SQL Server 2005中DDL触发器的实现

SQL SERVER 2005中,新增加了许多新的特性,其中的DDL触发器是个不错的选择,根据资料初步学习如下,现整理之. 在sql server 2000中,只能为针对表发出的 DML 语句(INSERT.UPDATE 和 DELETE)定义 AFTER 触发器.SQL Server 2005 可以就整个服务器或数据库的某个范围为 DDL 事件定义触发器. 可以为单个 DDL 语句(例如,CREATE_TABLE)或者为一组语句(例如,DDL_DATABASE_LEVEL_EVENTS)定义

小议ADO.NET对象模型

了解ADO.NET模型对数据应用程序开发很重要,下面是我今天学习总结的ADO.NET新手入门级的知识,希 望对新手有用,也期待以这块破砖头引来高手们的玉哦! DataSet(数据集).它表示整个数据库的一个子集,无需跟数据库有直接连接,缓存在机器中, DataSet需要周期性地跟父数据库进行连接,以对数据库和DataSet相互更新. DataSet不仅仅从一个表中获得几行,而且要获得一组表,它们包含有原数据库关系和约束的必要原数 据. DataSet由DataTable对象和DataRelati