如何使用其他源的数据更新 SQL Server 表 (DataTable)
我有一个从 .xls 表生成的 DataTable
。
我想将此 DataTable
存储到 SQL Server 数据库中的现有表中。
我使用 SqlBulkCopy
来存储具有唯一 PK 的行。
问题是,我还有其他行与 SQL Server 表具有相同的 PK,但包含与 SQL Server 表相比具有不同值的单元格。
简而言之:
假设在我的 DataTable
中我有一行如下所示:
id(PK) |名称 |编号
005 | ABC | 123
006 | LGE | 122
对于我的 SQL 服务器,我有这样的东西;
id(主键)|名称 |编号
004 |哥伦比亚广播公司 | 345
005 | 345 005克斯| 122
现在您会看到可以使用 SqlBulkCopy
将行 006 直接上传到 SQL Server。另一方面,无法使用它插入行 005,因为 SQL Server 表包含具有相同 PK 的行。
现在我尝试手动提取该行。将每个单元格提取到 ArrayList 中,然后生成 UPDATE Table 语句。然而,这种方法似乎不可行,因为我有很多行要处理。
我正在寻找更好的方法来实现这个目标。
任何帮助表示赞赏。
谢谢
I have a DataTable
which is generated from .xls table.
I would like to store this DataTable
into an existing table in SQL Server database.
I use SqlBulkCopy
to store rows which have unique PK.
Problem is, I also have other rows which have same PK as SQL Server table but contain cells with different value compared to SQL Server table.
In short:
Let's say in my DataTable
I have a row like this:
id(PK) | name | number
005 | abc | 123
006 | lge | 122
For my SQL server I have sth like this;
id(PK) | name | number
004 | cbs | 345
005 | lks | 122
Now you see the row 006 can be uploaded straight away into SQL Server using SqlBulkCopy
. On the other hand the row 005 can't be inserted using it since SQL server table contains row with identical PK.
Now I tried to manually extract the row. Extract each single cell into an ArrayList then generate an UPDATE Table statement afterwards. However this method seems to be unfeasible as I have so many rows to process.
I am looking for a better method to achieve this goal.
Any help is appreciated.
Thank's
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
使用以下代码:
用于从 DataTable 读取数据并准备 XML 数据的 C# 端代码:
数据库端存储过程(您将需要更新表名称):
Use the below code:
C# Side code for reading data from DataTable and preparing the XML data:
Database side Stored Procedure (you will need to update your table name):