使用 LinqToDatasets 缓慢更新/插入 SQL Server CE

发布于 2024-08-24 10:33:12 字数 918 浏览 2 评论 0原文

我有一个移动应用程序,它使用 LinqToDatasets 更新/插入 SQL Server CE 3.5 文件。

我的代码如下所示:

// All the MyClass Updates
MyTableAdapter myTableAdapter = new MyTableAdapter();

foreach (MyClassToInsert myClass in updates.MyClassChanges)
{
    // Update the row if it is already there
    int result = myTableAdapter.Update(myClass.FirstColumn, 
                                       myClass.SecondColumn, 
                                       myClass.FirstColumn);
    // If the row was not there then insert it.
    if (result == 0)
    {
        myTableAdapter.Insert(myClass.FirstColumn, myClass.SecondColumn);
    }
}

该代码用于保持手持数据库与服务器数据库同步。问题是,如果是完整更新(例如第一次),则会有很多更新(大约 125 个)。这使得这段代码(以及更多类似的循环需要很长时间(我有三个这样的循环,每个循环花费超过 30 秒)。

是否有更快或更好的方法来执行这样的更新/插入?

(我确实看到了这个 < a href="http://sqlcebulkcopy.codeplex.com/" rel="nofollow noreferrer">Codeplex 项目,但我不知道如何使其同时适用于更新和插入。)

I have a mobile app that is using LinqToDatasets to update/insert into a SQL Server CE 3.5 File.

My Code looks like this:

// All the MyClass Updates
MyTableAdapter myTableAdapter = new MyTableAdapter();

foreach (MyClassToInsert myClass in updates.MyClassChanges)
{
    // Update the row if it is already there
    int result = myTableAdapter.Update(myClass.FirstColumn, 
                                       myClass.SecondColumn, 
                                       myClass.FirstColumn);
    // If the row was not there then insert it.
    if (result == 0)
    {
        myTableAdapter.Insert(myClass.FirstColumn, myClass.SecondColumn);
    }
}

This code is used to keep the hand held database in sync with the server database. Problem is if it is a full update (first time for example) there are a lot of updates (about 125). That makes this code (and more loops like it take a very long time (I have three such loops that take over 30 seconds each).

Is there a faster or better way to do updates/inserts like this?

(I did see this Codeplex Project, but I could not see how to make it work with both updates and inserts.)

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

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

发布评论

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

评论(1

怂人 2024-08-31 10:33:12

您应该始终使用 SqlCeResultSet 在移动设备上进行数据访问,以获得最大性能和内存使用率。您必须标识要插入的数据,然后使用类似 SqlCeBulkCopy 示例的代码,并通过使用 SqlCeResultSet 的 Seek 和 Update 方法来使用类似的代码。

You should always use SqlCeResultSet for data access on mobile devices for maximum performance and memory usage. You must identify the data to be inserted and then use code like the SqlCeBulkCopy sample, and use similar code by using the Seek and Update methods of the SqlCeResultSet.

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