我正在開發一個C#應用程序,其後端爲sqlite。在我的應用程序中,我有一個用於清理數據庫的按鈕(刪除.db文件並使用一些初始數據重新創建它)。有時當我嘗試刪除數據庫時,它說它不能被刪除,因爲它正在被另一個進程使用。在刪除它之前,我正在使用緊密連接,處理和清除池功能。即使這樣它也會拋出同樣的異常。這裏是我的代碼:刪除sqlite數據庫文件
string targetDataBaseFilePath = Path.Combine(dataDirectoryPath, "local.db");
ThingzDatabase.Create(targetDataBaseFilePath, "touchdb");
在創建函數我定義了下面的代碼,從那裏我得到的錯誤:
if (File.Exists(DatabaseFileName))
{
try
{
ThingzDatabase.localdb.conn.Close();
ThingzDatabase.localdb.conn.Dispose();
SQLiteConnection.ClearAllPools();
}
catch (Exception)
{
}
File.Delete(DatabaseFileName); //error throws from here.
}
我怎樣才能防止被拋出的錯誤?
我會很樂意回顯! – 2011-12-16 06:21:03