如果检查表是否存在以保证连接打开SQLite中 [英] Checking if Table Exists Keeping Connection Open in SQLite
问题描述
所以我有一个方法,这是为了检查是否存在表中定义如下数据库:
So I have a method which is supposed to check if a table exists in a database which is defined as follows:
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;
}
创建连接()
只是创建了一个连接字符串的新连接,所以我不认为这个问题是存在的。如果我有删除行 conn.GetSchema(表)...
,我能够删除数据库文件,但如果我添加该行早在我得到的当我尝试后删除以下异常的使用
:
CreateConnection()
just creates a new connection with a connection string so I don't think the issue is there. If I have remove the line conn.GetSchema("Tables")...
and I am able to delete the database file but if I add that line back in I get the following exception when I try to delete after the using
:
System.IO.IOException:进程 无法访问文件C:\ db.sqlite 因为它是正由另一个 流程。
System.IO.IOException: The process cannot access the file 'C:\db.sqlite' because it is being used by another process..
你的的DataRow
对象存到数据库的连接,或者没有人知道这个问题可能是什么?如果有更好的方法来检查,如果一个表中存在的SQLite我愿意接受这一点。
Do DataRow
objects keep a connection to the database or does anyone know what the issue could be? If there is a better way to check if a table exists in SQLite I am open to that as well.
谢谢!
推荐答案
好了,所以我已经想通了这个问题,所以我会张贴在这里万一有人遇到同样的问题。基本上我有连接池启用,因此连接是保持与数据库的开放式连接,这就是为什么我所看到的例外。刚过添加下面的使用
:
Ok so I've figured out the issue so I'll post it here in case anyone comes across the same problem. Basically I had connection pooling enabled so the connections were maintaining an open connection with the database and that was why i was seeing the exception. Just add the following after the using
:
SQLiteConnection.ClearAllPools();
这篇关于如果检查表是否存在以保证连接打开SQLite中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!