C# 如何在WAL模式下打开SQLite连接
在C#中,如何打开SQLite连接 以下是我在正常模式下打开的方式:C# 如何在WAL模式下打开SQLite连接,c#,sqlite,wal,C#,Sqlite,Wal,在C#中,如何打开SQLite连接 以下是我在正常模式下打开的方式: SQLiteConnection connection = new SQLiteConnection("Data Source=" + file); connection.Open(); // (Perform my query) 以下是我不太完美的解决方案: SQLiteConnection connection = new SQLiteConnection("Data Source=" + file); connecti
SQLiteConnection connection = new SQLiteConnection("Data Source=" + file);
connection.Open();
// (Perform my query)
以下是我不太完美的解决方案:
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)
如果你知道一些不太详细的事情,我很高兴听到 在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模式 如果我理解正确,这意味着您可以为数据库设置一次WAL模式,无需在每个连接上都设置它 可以使用SQLite的命令行shell执行此操作:
+1您代码的最后一行就是我一直在寻找的解决方案,非常感谢!工厂方法可能很有趣,尽管在我的案例中我不需要它。考虑到SQLite连接字符串中允许的参数数量,您的方法是组合学中一个有趣的案例研究:)此解决方案不适用于我。我将DB置于journal_mode=WAL的唯一方法是在建立连接后发出单独的命令。我相信这是唯一正确的答案。上面的答案在连接字符串中设置PRAGMA对我不起作用。