首页 > 代码库 > 用自己设计的表(包括主键,外键约束)填充DataSet类

用自己设计的表(包括主键,外键约束)填充DataSet类

1.创建一个内存表,定义主键约束等

2.声明基类数组并赋值,再把数组当做行添加到Rows中

using System;using System.Collections.Generic;using System.Linq;using System.Text;using System.Threading.Tasks;using System.Data.SqlClient;using System.Data;namespace ConsoleApplication2{    class Program    {        static void Main(string[] args)        {            DataSet ds = new DataSet();            ds.Tables.Add(createbuildingtable());            ds.Tables.Add(createroomtable());            //定义外键约束            DataColumn child = ds.Tables["room"].Columns["buildingid"];            DataColumn parent=ds.Tables["building"].Columns["buildingid"];            ForeignKeyConstraint fc = new ForeignKeyConstraint("fc", parent, child);//主(building)表从(room)表之间建立外键关联对象fc,约束名为fc            ds.Tables["room"].Constraints.Add(fc);//必须添加到子表中的约束集合(Constraints)中            ////申明基类数组,存放表中的数据            object[] dt1=new object[2];            object[] dt2 = new object[3];            //为基类数组赋值            dt1[0] = 1;            dt1[1] = "如家";            dt2[0] = 206;            dt2[1] = "翰墨轩";            dt2[2] = 1;            ////将数据加到表的行中去            ds.Tables["building"].Rows.Add(dt1);            ds.Tables["room"].Rows.Add(dt2);            //打印两个表中的数据            foreach(DataRow x in ds.Tables["building"].Rows)                foreach (DataColumn y in ds.Tables["building"].Columns)                {                    Console.WriteLine("{0}:{1}", y.ColumnName, x[y]);                }            foreach (DataRow x in ds.Tables["room"].Rows)                foreach (DataColumn y in ds.Tables["room"].Columns)                {                    Console.WriteLine("{0}:{1}", y.ColumnName, x[y]);                }            ds.Relations.Add("rooms", ds.Tables["building"].Columns["buildingid"], ds.Tables["room"].Columns["buildingid"]);//Relations:用于将表链接起来并允许从父表浏览到子表的关系的集合            Console.WriteLine("________________________");            foreach (DataRow building in ds.Tables["building"].Rows)            {                DataRow[] children = building.GetChildRows("rooms");//使用 关系集合 的指定 关系名称RelationName(rooms),取得 DataRow 的子資料列                int count = children.Length;                Console.WriteLine("building{0} contains {1} room{2}", building["name"], count, count > 1 ? "s" : "");                foreach (DataRow row in children)                    Console.WriteLine("room:{0}", row["name"]);            }        }        private static DataTable createroomtable()        {            DataTable room = new DataTable("room");//申请一个内存表            //为内存表添加列            room.Columns.Add(new DataColumn("roomid", typeof(int)));            room.Columns.Add(new DataColumn("name", typeof(string)));            room.Columns.Add(new DataColumn("buildingid", typeof(int)));            //设置主键约束的另一种方法,使用UniqueConstraint            //room.Constraints.Add(new UniqueConstraint(room.Columns["roomid"]));//room.Constraints:由该表维护的约束的集合            DataColumn[] a = new DataColumn[1];//定义datacolumn对象的数组            a[0] = room.Columns["roomid"];            room.PrimaryKey = a;//定义主键 PrimaryKey:获取或设置充当数据表主键的列的数组,他的类型为datacolumn对象的数组            return room;        }        private static DataTable createbuildingtable()        {            DataTable building = new DataTable("building");            building.Columns.Add(new DataColumn("buildingid", typeof(int)));            building.Columns.Add(new DataColumn("name", typeof(string)));            DataColumn[] a = new DataColumn[1];//定义datacolumn对象的数组            a[0] = building.Columns["buildingid"];            building.PrimaryKey =a;//定义主键 PrimaryKey:获取或设置充当数据表主键的列的数组,他的类型为datacolumn对象的数组            return building;        }    }}