使用 LINQ to SQL 进行批量插入允许出现一些失败

发布于 2024-10-10 07:50:59 字数 734 浏览 6 评论 0原文

因此,我们有一个通过 WCF 服务获取更新列表的服务,然后将其写入数据库。其中一些可能会失败(每个查询 1000 次更新/插入),如果失败,则要求写入其余的插入/更新,并记录失败以便稍后处理...

问题是如何这应该在 LINQ to SQL 中完成吗?目前,我正在执行如下操作:

        TestDataContext dc = new TestDataContext();
        Random r = new Random();
        for (int i = 0; i < 300; i++)
        {
            Table table = new Table { randomNumber = r.Next(150) };
            dc.Tables.InsertOnSubmit(table);
        }
        try
        {
            dc.SubmitChanges();

        }
        catch (Exception ex)
        {
            Console.WriteLine(ex.Message);
        }

在本示例中,Table.randomNumber 是主键。在写入之前数据集中应该有重复项,但是当我调用 dc.SubmitChanges() 时,它会抛出异常...而不是在每次迭代后调用 dc.SubmitChanges() ,是否有更好的方法来执行此操作?

So, we have a service that takes list of updates via a WCF service, which should then be written to the DB. some of these could potentially fail (1000 updates/inserts per query) and if they do, the requirement is for the rest of the inserts/updates to be written, and the fails to be logged for processing later...

the question is how should this be done in LINQ to SQL. at the moment, i am doing something like follows:

        TestDataContext dc = new TestDataContext();
        Random r = new Random();
        for (int i = 0; i < 300; i++)
        {
            Table table = new Table { randomNumber = r.Next(150) };
            dc.Tables.InsertOnSubmit(table);
        }
        try
        {
            dc.SubmitChanges();

        }
        catch (Exception ex)
        {
            Console.WriteLine(ex.Message);
        }

in this example, Table.randomNumber is primary key. there should be duplicates in the dataset before getting written, but when i call dc.SubmitChanges() it throws an exception... instead of calling dc.SubmitChanges() after each iteration, is there a better way of doing this?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

↙温凉少女 2024-10-17 07:50:59

我最终按照编写的方式提交了每一行,并发现了任何错误。这样,我可以捕获失败,但正确插入其余部分。它不像我想象的那么慢,但并不理想......请注意,因为这是一个“离线”完成的过程(后端处理),它可以满足我们的需要。

I ended up submitting each row as they were written, and catching any errors. this way, i can catch the fails, but insert the rest correctly. Its not as slow as i though it would be, but not ideal... mind you, since this is a process that is done "offline" (backend processing) it works for what we need.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文