Thread Safe way to access SQL CE Database

落爺英雄遲暮 提交于 2019-12-24 09:27:18

问题


Background:

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.

Here's the issue:

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.


回答1:


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
}

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.



来源:https://stackoverflow.com/questions/9195913/thread-safe-way-to-access-sql-ce-database

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!