14.14DataSet数据集
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Data; namespace _14._13数据集DataSet { class Program { static void Main(string[] args) { //创建DataSet对象 DataSet myds = new DataSet("csharp51zxw");//定义数据集 DataTable mydt = new DataTable("mytable001");//定义数据表 myds.Tables.Add(mydt);//数据集加载数据表 DataColumn myDataColumn1 = new DataColumn("id", Type.GetType("System.Int32"));//定义字段 DataColumn myDataColumn2 = new DataColumn("name", Type.GetType("System.String")); DataColumn myDataColumn3 = new DataColumn("department", Type.GetType("System.String")); mydt.Columns.Add(myDataColumn1);//数据表加载字段 mydt.Columns.Add(myDataColumn2); mydt.Columns.Add(myDataColumn3); string[,] mystr= { { "张三","开发部"}, { "李四", "销售部" }, { "王五", "技术支持部" }, { "赵六", "人力资源部" } };//定义二元字符串 for(int i = 0; i < mystr.Length / 2; i++) { DataRow myRow = mydt.NewRow(); myRow[0] = i+1; myRow["name"] = mystr[i, 0]; myRow[2] = mystr[i, 1]; mydt.Rows.Add(myRow); } myds.AcceptChanges();//数据集接收数据 Console.WriteLine("更改前的数据"); outValues(myds); DataTable mydt002 = mydt.Clone(); DataRow mydr = mydt002.NewRow(); mydt002.Rows.Add(new object[] { 6, "张三五", "支持部" }); mydt002.Rows.Add(new object[] { 7, "周全", "业务部" }); myds.Merge(mydt002);//将mydt002及其架构合并到myds中 Console.WriteLine("第二次更改后"); outValues(myds); DataSet myds001 = myds.Copy(); DataSet myds002 = myds.Clone(); Console.WriteLine("copy的结果"); outValues(myds001); Console.WriteLine("克隆的结果"); outValues(myds002); Console.ReadKey(); } public static void outValues(DataSet ds)//实现遍历DataSet { foreach(DataTable outdt in ds.Tables) { Console.WriteLine("表名称:"+outdt.TableName); foreach(DataRow outdr in outdt.Rows) { foreach(DataColumn outDataColume in outdt.Columns) Console.Write(outdr[outDataColume]+"\t"); Console.WriteLine(); } } } } }