Merge one datatable into another datatable in C#

The following code snippet show how we can merge records of one table to another in C# programming language.For this we use the Merge() method of the DataSet class.

        private void MergeDatatable()
        {
            DataTable dt1 = new DataTable();
            dt1.Columns.Add("id");
            dt1.Columns.Add("Name");
            dt1.Rows.Add();
            dt1.Rows[0][0] = "1";
            dt1.Rows[0][1] = "David";
            dt1.Rows.Add();
            dt1.Rows[1][0] = "2";
            dt1.Rows[1][1] = "Ram";
 
            DataTable dt2 = new DataTable();
            dt2.Columns.Add("id");
            dt2.Columns.Add("Name");
            dt2.Rows.Add();
            dt2.Rows[0][0] = "3";
            dt2.Rows[0][1] = "Ankur";
            dt2.Rows.Add();
            dt2.Rows[1][0] = "4";
            dt2.Rows[1][1] = "John";
            dt2.AcceptChanges();
            dt2.Merge(dt1, false, MissingSchemaAction.Add);
        }