DataSet常用简单方法

Clear移除表中所有行来清除任何数据的DataSet

Clone赋值DataSet的结构但不复制数据

Copy赋值DataSet的结构和数据

Dispose释放DataSet对象

Equals确定两个DataSet对象是否相同

Merge指定的DataSet DataTableDataRow对象的数组合到当前的DataSetDataTable

ResetDataSet重置为初始状态

 

  class Program
    {
        public void outvalue(DataSet ds)
        {
            foreach (DataTable outdtb in ds.Tables)
            {
                Console.WriteLine(outdtb.TableName);
                foreach (DataRow outdrw in outdtb.Rows)
                {
                    foreach (DataColumn outdcl in outdtb.Columns)
                    {
                        Console.Write(outdrw[outdcl]+"\t");
                        
                    }
                    Console.WriteLine();
                }
                
            }
        
        
        }




        static void Main(string[] args)
        {
            try
            {
                DataSet ds = new DataSet("csharp001");
                DataTable dtb = new DataTable("mytable001");
                ds.Tables.Add(dtb);

                DataColumn mycl0 = new DataColumn("id", Type.GetType("System.Int32"));
                DataColumn mycl1 = new DataColumn("name",Type.GetType("System.String"));
                DataColumn mycl2 = new DataColumn("deparment", Type.GetType("System.String"));
                dtb.Columns.Add(mycl0);
                dtb.Columns.Add(mycl1);
                dtb.Columns.Add(mycl2);

                string[,] mystr = { { "张三", "销售部" }, { "李四", "人事部" }, { "王五", "网络部" }, { "赵六", "开发部" }, { "刘奇", "研发部" }, { "周八", "市场部" } };
                for (int i = 0; i < mystr.Length/2; i++)
                {
                    DataRow drw = dtb.NewRow();
                    dtb.Rows.Add(drw);
                    drw[0] = i;
                    drw[1]=mystr[i,0];
                    drw[2]=mystr[i,1];
                }
                Program p = new Program();
                p.outvalue(ds);

                DataTable mydt002 = dtb.Clone();          //mydt001de 的表克隆给mydt002

                Console.WriteLine("\t","克隆后的表");

                DataRow row01 = dtb.NewRow();
                mydt002.Rows.Add(new object[]{6,"杜九","开发部"});

                ds.Merge(mydt002);  //将mydt表 合并到dataset数据集中

                p.outvalue(ds);


                Console.ReadKey();    
                 
            }
                
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message.ToString());

            }
            finally
            {
                
            }
        }

    }
}

 

posted @ 2018-03-26 08:28  WhiteSpace  阅读(2318)  评论(0编辑  收藏  举报