检查表是否存在在SQLite中保持连接打开

所以我有一个方法可以检查数据库中是否存在一个表,其定义如下:

internal override bool TableExists(string tableName) { bool tableExists = false; // Check the Tables schema and see if the table exists using (SQLiteConnection conn = (SQLiteConnection) CreateConnection()) { conn.Open(); DataRow[] rows = conn.GetSchema("Tables").Select(string.Format("Table_Name = '{0}'", tableName)); tableExists = (rows.Length > 0); } // Actually called elsewhere in the code, just here for testing. File.Delete(DatabaseEnvironmentInfo.GetPrimaryDataFile(DatabaseName)); return tableExists; } 

CreateConnection()只是用连接字符串创建一个新连接,所以我认为问题不在那里。 如果我删除了行conn.GetSchema("Tables")...并且我能够删除数据库文件但是如果我添加该行,我在using后尝试删除时会得到以下exception:

System.IO.IOException:进程无法访问文件’C:\ db.sqlite’,因为它正由另一个进程使用。

DataRow对象是否与数据库保持连接,或者是否有人知道问题可能是什么? 如果有更好的方法来检查SQLite中是否存在表,我也对此持开放态度。

谢谢!

好的,所以我已经找到了问题所以我会在这里发布,万一有人遇到同样的问题。 基本上我启用了连接池,因此连接保持与数据库的开放连接,这就是我看到exception的原因。 using后添加以下内容:

 SQLiteConnection.ClearAllPools(); 

如果在使用结束时添加conn.Close(),是否可以删除数据库?

我没有使用连接池有类似的问题。 我发现问题是由TableAdapters中丢失的SQLiteCommand处理引起的。

你可以在这里找到更多细节。