使用SqlDataAdapter更新SQL Server数据库(Updating SQL Server database with SqlDataAdapter)

编程入门 行业动态 更新时间:2024-10-23 03:20:17
使用SqlDataAdapter更新SQL Server数据库(Updating SQL Server database with SqlDataAdapter)

我有一个带有新条目的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.

更多推荐

本文发布于:2023-08-01 20:11:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1363675.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:数据库   SQL   SqlDataAdapter   Server   database

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!