简体   繁体   English

使用SqlDataAdapter更新SQL Server数据库

[英]Updating SQL Server database with SqlDataAdapter

I have a DataTable with new entries, and I want to add these to an existing database. 我有一个带有新条目的DataTable ,我想将它们添加到现有数据库中。

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. 因此,我创建了一个SqlDataAdapter并用它填充第二个表,然后合并两个表并更新数据库,但是什么也没有发生,返回值为0,数据库保持不变。

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);

也可以尝试使用CategoryAdapterBackup.Flush()DBManager.Flush() ,将SELECT*FROM替换为SELECT * FROM (带空格)

I'm having a problem discerning your intentions with this code. 我在用此代码识别您的意图时遇到了问题。 Why not simply use sqlbulkcopy on SubTable? 为什么不直接在SubTable上使用sqlbulkcopy? That would append the contents of SubTable to the destination SQL table. 这会将SubTable的内容附加到目标SQL表中。 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. 如果您担心重复,则将SubTable表的内容插入数据库中的Temp中,并可能使用Join将行插入到目标表中,而目标表中不存在该行。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM