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
            {
                
            }
        }

    }
}

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM