0

I have three DataTables with data:

table1 has columns AGE, FIRST_NAME, LAST_NAME, FAVORITE_COLOR, PHONE
table2 has columns AGE, FIRST_NAME, LAST_NAME, PHONE
table3 has columns AGE, LAST_NAME, FIRST_NAME, FAVORITE_COLOR, PHONE

I also have a fourth table which I need to fill with all data from the three previous tables.

table4 has columns AGE, LAST_NAME, FIRST_NAME, PHONE

Any ideas how this can be done?

4
  • 4
    why are you creating a table for table4 instead of a view? Commented Jul 9, 2010 at 12:06
  • Table4 seems to be the same as Table3 less the FAVORITE_COLOR field. Commented Jul 9, 2010 at 12:11
  • table1, table2 and table3 are all read from three different CSV files which. I need to push the data from the CSV files into a database, but they have different columns and column ordering. Commented Jul 9, 2010 at 12:24
  • ImportRow works even the column order are different. ImportRow matches the column Commented Jul 9, 2010 at 12:40

4 Answers 4

1

It sounds like you are tackling the problem the wrong way; a clue is the way that you are duplicating data and seeking to merge tables together.

This kind of thing would usually be achieved at the data access layer (DAL), i.e. with a better database query.

Perhaps if you supply some more context to the problem you are trying to achieve, we will be able to analyse and see if we can come up with a better solution. Sorry if that sounds patronising, it's not - as you know there are infinite ways of solving a problem in software!

Hope that helps!

Sign up to request clarification or add additional context in comments.

Comments

1

Try .ImportRow :

var dtA = new DataTable
{
    Columns =
    {            
        { "Age", typeof(int) },
        { "Middlename", typeof(string) },
        { "Firstname", typeof(string) }
    }
};

dtA.Rows.Add(1, "Yeah", "John");
dtA.Rows.Add(2, "Yo", "Paul");

var dtB = new DataTable
{
    Columns =
    {
        { "Age", typeof(int) },
        { "Firstname", typeof(string) }
    }
};

dtB.Rows.Add(3, "George");
dtB.Rows.Add(4, "Ringo");



foreach (DataRow r in dtA.Rows)
    dtB.ImportRow(r);


foreach (DataRow r in dtB.Rows)
{
    MessageBox.Show(string.Format("{0} {1}", r["Age"], r["Firstname"]));
}

Comments

0

DataTable has a method called "Merge", so:

table4.Merge(table1, true, MissingSchemaAction.Ignore) // ignores any columns that are not in table4's schema
table4.Merge(table2, true, MissingSchemaAction.Ignore) 
table4.Merge(table3, true, MissingSchemaAction.Ignore) 

Assuming you have given table4 the columns you want first, of course, using

table4.Columns.Add

Comments

0

you can try something like this:

DataSet dataset; //dataset with all datatables (table1, table2, table3)
DataTable table4; //datatable with the result "union"
foreach (DataTable dt in dataset.Tables)
{
    foreach (DataRow dr in dt.Rows)
    {
        DataRow nr = table4.NewRow();
        foreach (DataColumn dc in table4.Columns)
        {
            try
            {
                nr[dc.ColumnName] = dr[dc.ColumnName];
            }
            catch
            {
                nr[dc.ColumnName] = "COLUMN NOT FOUND";
            }
        }
        table4.Rows.Add(nr);
    }
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.