我有一个带有新条目的DataTable ,我想将它们添加到现有数据库中。
所以我创建了一个SqlDataAdapter并用它填充了第二个表,然后我合并了两个表,并更新了数据库,但没有任何反应,返回值为0,数据库保持不变。
这是我的代码:
DBManager.DBDestinationConnect(textBox10.Text, textBox9.Text, textBox8.Text, textBox7.Text, ref destinationConnection); //DBManager is a Class, to connect with the database CategoryAdapterBackup = new SqlDataAdapter("SELECT*FROM " + tablename, destinationConnection); CategoryTableBackup = new DataTable(); CategoryAdapterBackup.Fill(CategoryTableBackup); CategoryTableBackup.Merge(SubTable); //SubTable is the DataTable with the new entries CategoryAdapterBackup.Update(CategoryTableBackup);I have a DataTable with new entries, and I want to add these to an existing database.
So I create a SqlDataAdapter and fill a second table with it, then I merge the two tables, and update the database, but nothing happens, the return value is 0, the database stays the same.
Here's my code:
DBManager.DBDestinationConnect(textBox10.Text, textBox9.Text, textBox8.Text, textBox7.Text, ref destinationConnection); //DBManager is a Class, to connect with the database CategoryAdapterBackup = new SqlDataAdapter("SELECT*FROM " + tablename, destinationConnection); CategoryTableBackup = new DataTable(); CategoryAdapterBackup.Fill(CategoryTableBackup); CategoryTableBackup.Merge(SubTable); //SubTable is the DataTable with the new entries CategoryAdapterBackup.Update(CategoryTableBackup);最满意答案
我有一个问题,用这段代码辨别你的意图。 为什么不简单地在SubTable上使用sqlbulkcopy? 这会将SubTable的内容追加到目标SQL表中。 如果您害怕重复,则将SubTable Table的内容插入到DB中的Temp,并且可以使用Join将行插入到目标表中不存在的行的目标表中。
I'm having a problem discerning your intentions with this code. Why not simply use sqlbulkcopy on SubTable? That would append the contents of SubTable to the destination SQL table. If you are afraid of duplicates, then insert the contents of SubTable Table to a Temp in the DB and maybe use a Join to insert rows into the destination table where not exists row in destination table.
更多推荐
发布评论