Adding a DataTable to a DataSet in C#

In the C#, DataSet object is the set the more than one DataTables. You can add the multiple dataTables into the single DataSet object. You can set constraint information for a DataTable by using the PrimaryKey and Unique properties.

The following example creates a DataSet, adds the two new DataTables object to the DataSet, and then adds two DataColumn objects to the each table.

DataSet dsSchool = new DataSet("School");
// create a datatable foe class X
DataTable dtClass1 = dsSchool.Tables.Add("ClassX");
DataColumn Id1 = dtClass1.Columns.Add("StudentID", typeof(Int32));
dtClass1.Columns.Add("StudentName", typeof(string));
dtClass1.PrimaryKey = new DataColumn[] { Id };
// create another datatable foe class XI
DataTable dtClass2 = dsSchool.Tables.Add("ClassX1");
DataColumn Id2 = dtClass2.Columns.Add("StudentID", typeof(Int32));
dtClass2.Columns.Add("StudentName", typeof(string));

Two or more tables or relations with the same name, but different casing, can exist in a DataSet. This means you can add two tables with same name with different casing to single dataset like as:

DataTable dtClass1 = dsSchool.Tables.Add(“ClassX”);
DataTable dtClass1 = dsSchool.Tables.Add(“classX”);