How to Dyanamic Dataset in C#

The DataSet object serves as a container that represents a complete set of data. It includes related tables, constraints, and relationships among the tables. The DataSet consists of a DataTableCollection, which holds zero or more DataTable objects.

In ADO.NET, you have the ability to create DataTable objects and add them to an existing DataSet. The DataTable class is defined within the System.Data namespace and represents a single table of memory-resident data. Each DataTable can contain columns, rows, and data.

DataTable

To enforce constraints on a DataTable, you can utilize properties such as PrimaryKey and Unique. The PrimaryKey property allows you to specify one or more columns as the primary key for the table, ensuring uniqueness and efficient searching. The Unique property, on the other hand, allows you to specify columns that must have unique values within the DataTable.

Full Source C#
using System; using System.Data; using System.Data.SqlClient; using System.Windows.Forms; namespace WindowsApplication1 { public partial class Form1 : Form { public Form1() { InitializeComponent(); } private void button1_Click(object sender, EventArgs e) { DataSet ds = new DataSet(); DataTable dt ; DataRow dr ; DataColumn idCoulumn ; DataColumn nameCoulumn ; int i = 0; dt = new DataTable(); idCoulumn = new DataColumn("ID", Type.GetType("System.Int32")); nameCoulumn = new DataColumn("Name", Type.GetType("System.String")); dt.Columns.Add(idCoulumn); dt.Columns.Add(nameCoulumn); dr = dt.NewRow(); dr["ID"] = 1; dr["Name"] = "Name1"; dt.Rows.Add(dr); dr = dt.NewRow(); dr["ID"] = 2; dr["Name"] = "Name2"; dt.Rows.Add(dr); ds.Tables.Add(dt); for (i = 0; i <= ds.Tables[0].Rows.Count - 1; i++) { MessageBox.Show(ds.Tables[0].Rows[i].ItemArray[0] + " -- " + ds.Tables[0].Rows[i].ItemArray[1]); } } } }

Conclusion

The DataSet object in ADO.NET represents a complete set of data, including related tables, constraints, and relationships. The DataTableCollection within the DataSet holds the DataTable objects. You can create DataTable objects and add them to the DataSet. Each DataTable can have constraint information set using properties like PrimaryKey and Unique, ensuring data integrity within the table.