代码段——构建Datatable对象

            using System.Data;


            //先来建立ds数据库
            DataSet ds = new DataSet("ds");
            //再来建立tbClass和tbStudent两个数据表
            DataTable tbClass = new DataTable("tbClass");
            DataTable tbStudent = new DataTable("tbStudent");
            //把两个数据表tbClass和tbStudent加入数据库
            ds.Tables.Add(tbClass);
            ds.Tables.Add(tbStudent);

            //建立tbClass两列
            DataColumn ClassID = new DataColumn("ClassID", typeof(System.String));
            DataColumn ClassName = new DataColumn("ClassName", typeof(System.String));
            //设定ClassID列不允许为空
            ClassID.AllowDBNull = false;
            //把列加入tbClass表
            tbClass.Columns.Add(ClassID);
            tbClass.Columns.Add(ClassName);
            //设定tdClass表的主键
            tbClass.PrimaryKey = new DataColumn[] { ClassID };

            //建立tbStudent的三列
            DataColumn StudentID = new DataColumn("StudentID", typeof(System.String));
            DataColumn StudentName = new DataColumn("StudentName", typeof(System.String));
            DataColumn StudentClassID = new DataColumn("StudentClassID", typeof(System.String));
            //设定StudentID列不允许为空
            StudentID.AllowDBNull = false;
            //把列加入tbStudent表
            tbStudent.Columns.Add(StudentID);
            tbStudent.Columns.Add(StudentName);
            tbStudent.Columns.Add(StudentClassID);
            //设定tbStudent表的主键
            tbStudent.PrimaryKey = new DataColumn[] { StudentID };

            // 为两个表各加入5条记录
            for (int i = 1; i <= 5; i++)
            {
                //实例化tbClass表的行
                DataRow tbClassRow = tbClass.NewRow();
                //为行中每一列赋值
                tbClassRow["ClassID"] = Guid.NewGuid();
                tbClassRow["ClassName"] = string.Format("班级{0}", i);
                //把行加入tbClass表
                tbClass.Rows.Add(tbClassRow);
                //实例化tbStudent表的行
                DataRow tbStudentRow = tbStudent.NewRow();
                //为行中每一列赋值
                tbStudentRow["StudentID"] = Guid.NewGuid();
                tbStudentRow["StudentName"] = string.Format("学生{0}", i);
                tbStudentRow["StudentclassID"] = tbClassRow["ClassID"];
                //把行加入tbStudent表
                tbStudent.Rows.Add(tbStudentRow);
            }

            #region 快速给Datatable添加行数据
            tbClass.Rows.Add(Guid.NewGuid(), "一年级二班");
            tbClass.Rows.Add(Guid.NewGuid(), "二年级三班");
            tbClass.Rows.Add(Guid.NewGuid(), "三年级四班");

            tbStudent.Rows.Add(Guid.NewGuid(), "张三", tbClass.Rows[0].Field<string>("ClassID"));
            tbStudent.Rows.Add(Guid.NewGuid(), "李四", tbClass.Rows[1].Field<string>("ClassID"));
            tbStudent.Rows.Add(Guid.NewGuid(), "王五", tbClass.Rows[2].Field<string>("ClassID")); 
            #endregion


            foreach (DataRow row in tbClass.Rows)
            {
                Console.WriteLine("{0}-----{1}", row[0], row[1]);
            }

            foreach (DataRow row in tbStudent.Rows)
            {
                Console.WriteLine("{0}-----{1}-----{2}", row[0], row[1], row[2]);
            }
10dbd9d7-6f41-4253-a7bc-7df74f565e41-----班级1
df56ad88-9919-4877-ba64-07934f696d90-----班级2
85b02816-6386-4422-bdbb-d66081fe4e60-----班级3
dfacd2fc-202e-41a3-8ddd-14fee4b1c17e-----班级4
093165cf-656a-4d36-9f2c-c43bc038fe1e-----班级5
d50403e7-86bd-4419-9378-dd12cfb6bbd7-----一年级二班
2e39e703-bdb4-4965-9111-58ec99faf959-----二年级三班
821023eb-3a61-4c2f-bc12-8ccd3023ef71-----三年级四班


b5cc6bbc-7840-48e7-abdd-39c81dd92883-----学生1-----10dbd9d7-6f41-4253-a7bc-7df74f565e41
7bce80b0-0f1a-4959-836b-20d2ad9d29b7-----学生2-----df56ad88-9919-4877-ba64-07934f696d90
cb1a2864-867a-4427-9f6a-590bffc4afbe-----学生3-----85b02816-6386-4422-bdbb-d66081fe4e60
f0e5cbf3-6dc6-446b-8674-b48596baadb1-----学生4-----dfacd2fc-202e-41a3-8ddd-14fee4b1c17e
071357c9-9332-4943-8607-1f9d1b8de551-----学生5-----093165cf-656a-4d36-9f2c-c43bc038fe1e
41490a0c-61b2-4fe4-9218-2b69678c74a6-----张三-----10dbd9d7-6f41-4253-a7bc-7df74f565e41
dce159b3-2168-4e1a-aedd-bbc2d0a97abc-----李四-----df56ad88-9919-4877-ba64-07934f696d90
c9f44bf9-3b6a-422d-b059-5d173ec4712e-----王五-----85b02816-6386-4422-bdbb-d66081fe4e60
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章