locked
C# Datatable - Merge multiple table into single Datatable RRS feed

  • Question

  • User-1024101449 posted

    Hi,

    I am using multiple mdb and i want to read all the table values and those rows write into excel file.

    For example.

    I have three mdb.

    every mdb will have one table and every table will have one record.

    i want to merge all three records and write into excel file in particular range.

    i have written some code. but, it will return only one record. i.e last record which is executed in last in the loop.

    see my below code.

    foreach (String file1 in openFileDialog1.FileNames)

    {

       DataSet ds_combined = new DataSet();

               DataTable res = new DataTable();

       ds = mdbDataset.ReadMdbData(file1, false);

               res = frmMain.MergeMultiplemdb(ds);

               ds_combined.Merge(ds); // Nothing will display

    }

     

    private static DataTable MergeMultiplemdb(DataSet ds1)

    {

                DataTable retval = new DataTable();

     

                string tablename1 = "EMP_TABLE";

                DataTable DTRes1 = ds1.Tables[tablename1];

               

     

                var results = (from myRow in DTRes1.AsEnumerable()

                              

                               select myRow).CopyToDataTable();

     

                DataTable dtTemp = (DataTable)results;

     

                retval = dtTemp;

     

              

                return retval;

     

    }

    what is the problem..

    Tuesday, November 28, 2017 12:29 PM

Answers

  • User1400794712 posted

    Hi gani7787,

    It seems there is something with the logic of your code. You can use breakpoint to debug your project step by step. As I can see in your code, you want to merge all records into ds_combine. Please define ds_combine outside the loop, else ds_combine will be redefined each time. So, it will only return one record.

    DataSet ds_combined = new DataSet();
    foreach (String file1 in openFileDialog1.FileNames)
    {  
        DataTable res = new DataTable();
        ds = mdbDataset.ReadMdbData(file1, false);
        res = frmMain.MergeMultiplemdb(ds);
        ds_combined.Merge(ds);
    }

    Best Regards,
    Daisy

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Wednesday, November 29, 2017 9:46 AM