Skip to main content
 首页 » 编程设计

c#之一次在同一张表中执行多个插入操作

2024年02月23日18EasonJim

我会先说明我做了什么,然后我会指出问题出在哪里。

我的应用程序通过(文件 uploader )从经过身份验证的用户那里获取一个 XML 文件,然后我将存储在 XML 文件中的数据映射(我的意思是迁移)到我的数据库中的等效数据。 我通过 LINQ 从 XML 文件中获取数据。

  • 我的第一个问题

    中的每个元素 XML 文件有一个等效的实体 在我的数据库中。什么是最好的和 更高效的插入方式 一个以上的记录 具体表并保证如果 数据有问题,回滚整个操作? 有没有一些如何做的例子 这?你有什么建议 验证 XML 数据?

  • 我的第二个问题:

    在第一个问题中,我谈到了 (插入)操作。如果 用户更改 XML 中的一些数据 文件然后我想更新我的 包含新数据的数据库。我该怎么办 做这个?我应该比较每个 记录,或尝试插入,如果失败, 然后更新这条记录?

请您参考如下方法:

each element in the XML file has an equivalent entity in my database. What is the best and more performance way to insert more than one record in a specific table .and guarantee if there is some thing wrong in the data , rollback the whole operation .please if there is some sample to do this.and any suggestions concerning validating the XML data.

这里的简单答案是:使用事务。事务的要点是为您提供一种机制,您可以通过该机制执行多个命令,然后将它们作为单个工作单元提交或完全回滚,以便数据库处于一种状态,就好像您的操作从未发生过一样.

in the first one i talk about the the (INSERT) operation, if the user change some data in the XML file then i wanna to update my database with the new data.how to o this .should i compare each record . or try to insert if fail then update this record?.

如果可以轻松避免,尝试错误不同的重试模式并不是理想的模式。您的 SQL 应该使用旨在根据现有数据有条件地插入或更新的语句(例如 SQL Server MERGE 命令;我没有 Informix 经验,所以我不能说什么它支持或者如果 MERGE 是 ANSI SQL),或者您应该在 SQL 中自己执行此条件逻辑。