【发布时间】:2013-03-30 00:05:38
【问题描述】:
在C#中,如何打开一个SQLite连接in WAL mode?
这是我在正常模式下的打开方式:
SQLiteConnection connection = new SQLiteConnection("Data Source=" + file);
connection.Open();
// (Perform my query)
【问题讨论】:
在C#中,如何打开一个SQLite连接in WAL mode?
这是我在正常模式下的打开方式:
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 SQLiteConnection("Data Source=" + file);
}
}
public class WALConnection : Connection
{
public override SQLiteConnection NewConnection(String file)
{
return new SQLiteConnection("Data Source=" + file + ";PRAGMA journal_mode=WAL;"
}
}
代码没有经过测试,但我希望你能明白,所以当你使用它时,你可以这样做。
SQLiteConnection conWal = new WALConnection(file);
conWAL.Open();
SQLiteConnection conNormal = new NormalConnection(file);
conNormal.Open();
【讨论】:
下面这行是我一直在寻找的,非常感谢 Turbot 的回答:
new SQLiteConnection("Data Source=" + file + ";PRAGMA journal_mode=WAL;")
【讨论】:
WAL模式的持久化
“与其他日志模式不同,PRAGMA journal_mode=WAL 是持久的。如果进程设置 WAL 模式,然后关闭并重新打开数据库,数据库将返回 WAL 模式。”
http://www.sqlite.org/wal.html
如果我理解正确,这意味着您可以为数据库设置一次 WAL 模式,无需在每个连接上都设置它。
您可以使用 SQLite 的命令行 shell 来执行此操作: http://www.sqlite.org/sqlite.html
【讨论】:
这是我不太完美的解决方案:
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)
如果你知道一些不那么冗长的东西,我会很高兴听到它!
【讨论】: