访问 SQL CE 数据库的线程安全方式 [英] Thread Safe way to access SQL CE Database

查看:36
本文介绍了访问 SQL CE 数据库的线程安全方式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

背景:

在我的 Windows 手机项目中.我使用本地 SQL CE 数据库来存储日期.使用 LINQ to SQL 以编程方式创建数据库表.

In my windows phone project. I use local SQL CE database to store date. The create the database tables using LINQ to SQL programmatically.

这个数据库是通过几个线程中的各种数据上下文实例访问的.

This database is accessed through various datacontext instances in couple of threads.

问题来了:

我为我的应用程序的用户提供了一个选项来清除所有数据并退出.当用户选择它时,我使用 datacontext 的 DeleteDatabase 方法删除数据库.但是,我总是收到数据库正在被另一个进程使用的错误,因此无法删除.

I give an option to the user of my app to erase all the data and logout. When the user selects this, I delete the database using the datacontext's DeleteDatabase method. But, I always receive the error that the database is being used by another process, hence cannot delete.

任何朝正确方向的推动都会让我开心.

Any nudges in the right direction will make my day.

推荐答案

安迪如上所述(老实说他们打败了我)当另一个进程仍然打开连接时访问数据库的问题.基本上它可以通过将您针对数据库运行的任何代码放在 using 块中来轻松修复,如下所示:

Andy as mentioned above (to be honest they beat me) the problem was accessing the database when another process still had the connection opened. Basically it can be easily fixed by placing any code you run against the database inside of a using block like so:

using (MyDataContext db = new MyDataContext("isostore:/MyData.sdf"))
{
    //Run database logic here
}

我想从某种意义上说,这对错误来说是完全合理的.在 WP7 中打开文件也会发生同样的事情.

I guess in a sense it makes perfect sense for the error. Same thing happens with open files in WP7.

有关更多信息和示例,请查看此 页面.

For more information and an example check out this page.

这篇关于访问 SQL CE 数据库的线程安全方式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆