Click here to Skip to main content
15,846,038 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Please help me guys, what i really want is to copy data(1 or all data) in datatable1 to datatable2. Im stuck of my code, dunno if it is correct
dt1 = ds1.Tables(0)
        Dim dt2 As DataTable = ds2.Tables(0)


        da1 = New MySqlDataAdapter("select s_inventoryTab_name, s_inventoryTab_stockonhand,s_inventoryTab_jan,s_inventoryTab_feb from s_medicinetab where s_inventoryTab_year = '" & 2016 & "'", con)
        da1.Fill(ds1)


        For i As Integer = 0 To dt2.Rows.Count - 1
            dt1.ImportRow(dt2.Rows(i))

        Next

I dont know if my code is correct.If there are some method, please guide me. Thanks guys

What I have tried:

It seems this code is correct but i dont understand
DataTable dtTarget = new DataTable();
    dtTarget = dtSource.Clone();
    DataRow[] rowsToCopy;
    rowsToCopy = dtSource.Select("key='" + matchString + "'");
    foreach (DataRow temp in rowsToCopy)
    {
        dtTarget.ImportRow(temp);
    }
Posted
Updated 28-Oct-16 6:18am
Comments
[no name] 28-Oct-16 11:00am    
If you don't know if it's correct or not, how would you expect us to know?
Member 12821687 28-Oct-16 21:26pm    
I am really sorry sir, my problem is where should i put the INSERT sql on the first example.

1 solution

DataTable.Clone only copies the structure/scheme - and does NOT copy the data. Your 2nd code block is illustrating that (it clones the table and then copies the data one row at a time).


If you want both the schema AND the data, use the DataTable.Copy method.
 
Share this answer
 
Comments
Member 12821687 28-Oct-16 21:24pm    
so if i use DataTable.Copy, there's no need to use INSERT sql or something to save the data to datatable2? uhmm its a little bit confuse

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900