I have a slight problem that I'm trying to solve. I am using SQLite database and i created my database Schedule.db automatically when application starts for the first time(if .db does not already exists).
我有一个小问题,我正在努力解决。我正在使用SQLite数据库,并且我第一次启动应用程序时自动创建了我的数据库Schedule.db(如果.db尚不存在)。
On a button_click I want to delete it so I can create new one when I start my application again.
在button_click上我想删除它,这样当我再次启动我的应用程序时我可以创建一个新的。
The problem is, every time I try to delete it I get an error:
问题是,每次我尝试删除它时都会出错:
"Additional information: The process cannot access the file '/filePath.../Scheduler.db' because it is being used by another process."
“附加信息:进程无法访问文件'/filePath.../Scheduler.db',因为它正由另一个进程使用。”
I understand that I can't delete it because my application is already using it but is there any solution to my current problem?
我知道我无法删除它,因为我的应用程序已经在使用它但是我的当前问题是否有任何解决方案?
string databasePath = AppDomain.CurrentDomain.BaseDirectory + "Scheduler.db";
if (MessageBox.Show("Do you want to delete database: [Scheduler.db]?", "Question", MessageBoxButton.YesNo, MessageBoxImage.Warning, MessageBoxResult.Yes) == MessageBoxResult.Yes)
{
if (File.Exists(databasePath))
{
SQLiteConnection connectionSqlLIte = new SQLiteConnection(@"Data Source=Scheduler.db;Version=3;");
connectionSqlLIte. Close();
File.Delete(databasePath);
MessageBox.Show("Database deleted: [Scheduler] ");
Application.Current.Shutdown();
}
else
{
MessageBox.Show("There is no database: [Scheduler]!");
}
}
1 个解决方案
#1
0
I'm flying a bit blind here, without your exact code but this is worth trying:
我在这里飞得有点盲,没有你的确切代码,但这值得尝试:
...
connectionSqLite.Close();
connectionSqLite.Dispose();
connectionSqLite = null; // Bad coding, see if you can delete this line
...
Now it is possible that the database is free to be deleted (and recreated)
现在可以*删除(并重新创建)数据库
You should not have to set connectionSqLite = null
, this is overkill and usually unnecessary and also kinda incorrect, but try it out anyway. See here
你不应该设置connectionSqLite = null,这是过度的,通常是不必要的,也有点不正确,但无论如何都要尝试。看这里
(But - remember that after you have called Dispose
on the connectionSqLite
object - it is essentially empty (null
). So you must set it to a new instance of your object before you can use it again)
(但是 - 请记住,在对connectionSqLite对象调用Dispose之后 - 它基本上是空的(null)。所以你必须先将它设置为对象的新实例才能再次使用它)
#1
0
I'm flying a bit blind here, without your exact code but this is worth trying:
我在这里飞得有点盲,没有你的确切代码,但这值得尝试:
...
connectionSqLite.Close();
connectionSqLite.Dispose();
connectionSqLite = null; // Bad coding, see if you can delete this line
...
Now it is possible that the database is free to be deleted (and recreated)
现在可以*删除(并重新创建)数据库
You should not have to set connectionSqLite = null
, this is overkill and usually unnecessary and also kinda incorrect, but try it out anyway. See here
你不应该设置connectionSqLite = null,这是过度的,通常是不必要的,也有点不正确,但无论如何都要尝试。看这里
(But - remember that after you have called Dispose
on the connectionSqLite
object - it is essentially empty (null
). So you must set it to a new instance of your object before you can use it again)
(但是 - 请记住,在对connectionSqLite对象调用Dispose之后 - 它基本上是空的(null)。所以你必须先将它设置为对象的新实例才能再次使用它)