sqlite异常:链接close()和dispose()之后任然不能释放与db文
c#使用sqlite1.0.97.0版本, string dbFile = @"G:test.db"; string connenctStr = string.Format(@"Data Source={0};Pooling=true;FailIfMissing=false",dbFile); SQLiteConnection m_Connection = new SQLiteConnection(connenctStr); m_Connection.Open(); using (var com = m_Connection.CreateCommand()) { com.CommandText = @"select 1"; com.ExecuteNonQuery(); } m_Connection.Close(); m_Connection.Dispose(); try { File.Delete(dbFile); } catch (Exception e) { throw e; } 执行代码,在删除文件时提示: An unhandled exception of type 'System.IO.IOException' occurred in TestSqlite.exe 异常。 原因是sqlite在执行 参考连接 stackoverflow 问答,http://stackoverflow.com/questions/8511901/system-data-sqlite-close-not-releasing-database-file,但是即使按
照answer中给出的答案加上GC.Collect();仍然报同样异常。 还需要加上GC.WaitForPendingFinalizers()这句。参考上述博客中11楼的回答。 代码修改后如下
string dbFile = @"G:test.db"; string connenctStr = string.Format(@"Data Source={0};Pooling=true;FailIfMissing=false",dbFile); SQLiteConnection m_Connection = new SQLiteConnection(connenctStr); m_Connection.Open(); using (var com = m_Connection.CreateCommand()) { com.CommandText = @"select 1"; com.ExecuteNonQuery(); } m_Connection.Close(); m_Connection.Dispose(); GC.Collect(); GC.WaitForPendingFinalizers(); try { File.Delete(dbFile); } catch (Exception e) { throw e; }运行正常 (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |