克隆数据或复制数据 [英] Cloning data or copy data
本文介绍了克隆数据或复制数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
请帮助我们,我真正想要的是将datatable1中的数据(1或所有数据)复制到datatable2。我卡住我的代码,如果它是正确的dunno
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);
}
推荐答案
DataTable.Clone
仅复制结构/方案 - 并不复制数据。你的第二个代码块说明了(它克隆了表格,然后一次复制一行数据)。
如果你想要架构和数据,使用DataTable.Copy
方法。
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 theDataTable.Copy
method.
这篇关于克隆数据或复制数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文