VS. NET (C #) Database Interface C # DataSet object creation demonstration (code case), vs.net
Create a DataSet object when the page is loaded for the first time, create a local copy for the database Forum, and add the tbClass table and the tbBoard table to it.
protected void Page_Load(object sender, EventArgse) { if(IsPostBack) { this.CreateDataSet(); } }
Private void CreateDataSet () {Response. write ("START schema DataSet object ...... "); // 1 -- create database ds_Forum = new DataSet (" Forum "); // 2 -- create two data tables DataTable tbClass = new DataTable (" tbClass "); dataTable tbBoard = new DataTable ("tbBoard"); // 3 -- add two data tables to the database ds_Forum.Tables.Add (tbClass); ds_Forum.Tables.Add (tbBoard ); // 4 -- construct the tbClass structure of the data table # region tbClass table structure // 4.1 -- create two columns of the tbClass table DataColumn ClassID = new DataColumn ("Cl AssID ", typeof (System. string); DataColumn ClassName = new DataColumn ("ClassName", typeof (System. string); // 4.2 -- specifies that the ClassID Column cannot be empty. allowDBNull = false; ClassID. maxLength = 50; // 4.3 -- add the preceding two columns to the tbClass table. columns. add (ClassID); tbClass. columns. add (ClassName); // set the primary key tbClass of the tbClass table. primaryKey = new DataColumn [] {ClassID}; # endregion // 5 -- construct the tbBoard structure of the data table # region tbBoard table structure // 5.1 -- create a tbBo Three columns of DataColumn BoardID = new DataColumn ("BoardID", typeof (System. string); DataColumn BoardName = new DataColumn ("BoardName", typeof (System. string); DataColumn BoardClassID = new DataColumn ("BoardClassID", typeof (System. string); // 5.2 -- specifies that the BoardID Column cannot be empty. allowDBNull = false; // 5.3 -- add the column to the tbBoard of the BoardID table. columns. add (BoardID); tbBoard. columns. add (BoardName); tbBoard. columns. add (BoardClassID); // 5.4 -- set the primary key tbBoard of the BoardID table. primaryKey = new DataColumn [] {BoardID}; # endregion // 6 -- add data rows to the preceding two tables # Add 5 Records to each of the two tables for (int I = 0; I <5; I ++) {// 6.1 -- instantiate the row object DataRow tbClass_Row = tbClass OF THE tbClass table. newRow (); // 6.2 -- assign tbClass_Row ["ClassID"] = Guid to each column in the row. newGuid (); tbClass_Row ["ClassName"] = string. format ("Classification {0}", I); // 6.3 -- adds the row object to the tbClass table. rows. add (tbClass_Row); // instantiate Row D of the tbBoard table AtaRow tbBoard_Row = tbBoard. newRow (); // assign tbBoard_Row ["BoardID"] = Guid to each column in the row. newGuid (); tbBoard_Row ["BoardName"] = string. format ("Forum {0}", I); tbBoard_Row ["BoardClassID"] = tbClass_Row ["ClassID"]; // Add rows to the tbBoard table tbBoard. rows. add (tbBoard_Row);} # endregion Response. write ("schema DataSet object successful! ");}