如何在WAL模式下打开SQLite连接

问题描述:

在C#中,如何打开SQLite连接in WAL mode如何在WAL模式下打开SQLite连接

这是我在正常模式下如何打开:

SQLiteConnection connection = new SQLiteConnection("Data Source=" + file); 
connection.Open(); 
// (Perform my query) 

如何在SQLiteConnection连接字符串中指定工厂方法?

为e.g

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(); 
+0

+1你的代码的最后一行是我一直在寻找解决方案,非常感谢!工厂方法可能很有趣,即使我不需要它。 – 2013-04-08 03:51:12

+1

您的方法是一个有趣的组合研究案例研究,给出了SQLite连接字符串允许的参数数量:) – Mark 2014-05-30 16:49:36

这是我不那么完美的解决方案:

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) 

如果你知道那么详细的东西,我会很高兴听到关于它!

下面这行就是我一直在寻找,非常感谢大菱鲆,其答案包括它:

new SQLiteConnection("Data Source=" + file + ";PRAGMA journal_mode=WAL;") 
+0

那么,为什么你不给他答案呢? – Mawg 2015-02-19 16:02:41

+0

@Mawg:你说得对。我已经做了 :-) – 2015-02-20 01:25:05

WAL模式的持久性

“不像其他的日志模式,PRAGMA journal_mode = WAL是永久的,如果一个进程设置了WAL模式,然后关闭并重新打开数据库,数据库将以WAL模式返回。“

http://www.sqlite.org/wal.html

如果我理解正确的话,这意味着你可以设置WAL模式数据库一次,就没有必要设置它的每一个连接上。

您可以使用命令行shell做SQLite的: http://www.sqlite.org/sqlite.html