如何在WAL模式下打开SQLite连接
在C#中,如何在WAL模式下打开SQLite连接?
以下是我在正常模式下打开的方式:
SQLiteConnection connection = new SQLiteConnection("Data Source=" + file); connection.Open(); // (Perform my query)
如何在SQLiteConnection连接字符串中指定工厂方法?
例如
public static class Connection { public abstract SQLiteConnection NewConnection(String file); } public class NormalConnection : Connection { public override SQLiteConnection NewConnection(String file) { return new SQLLiteConneciton("Data Source=" + file); } } public class WALConnection : Connection { public override SQLiteConnection NewConnection(String file) { return new SQLLiteConnection("Data Source=" + file + ";PRAGMA journal_mode=WAL;" } }
代码没有经过测试,但我希望你能得到这个想法,所以当你使用它时,你可以这样做。
SQLLiteConnection conWal = new WALConnection(file); conWAL.Open(); SQLLiteConnection conNormal = new NormalConnection(file); conNormal.Open();
以下这一行是我所寻找的,非常感谢Turbot的回答包括:
new SQLiteConnection("Data Source=" + file + ";PRAGMA journal_mode=WAL;")
这是我不太完美的解决方案:
SQLiteConnection connection = new SQLiteConnection("Data Source=" + file); connection.Open(); using (var command = new SQLiteCommand(sqliteConnection)) { command.CommandText = "PRAGMA journal_mode=WAL"; command.ExecuteNonQuery(); } // (Perform my query)
如果你知道一些不那么冗长的东西,我会很高兴听到它!
WAL模式的持久性
“与其他日记模式不同,PRAGMA journal_mode = WAL是持久的。如果进程设置WAL模式,然后关闭并重新打开数据库,数据库将以WAL模式返回。”
http://www.sqlite.org/wal.html
如果我理解正确,这意味着你可以为数据库设置一次WAL模式,不需要在每个连接上设置它。
您可以使用SQLite的命令行shell来执行此操作: http : //www.sqlite.org/sqlite.html