2013-04-08 220 views

回答

10

如何在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

1

這是我不那麼完美的解決方案:

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) 

如果你知道那麼詳細的東西,我會很高興聽到關於它!

6

下面這行就是我一直在尋找,非常感謝大菱鮃,其答案包括它:

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

1

WAL模式的持久性

「不像其他的日誌模式,PRAGMA journal_mode = WAL是永久的,如果一個進程設置了WAL模式,然後關閉並重新打開數據庫,數據庫將以WAL模式返回。「

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

如果我理解正確的話,這意味着你可以設置WAL模式數據庫一次,就沒有必要設置它的每一個連接上。

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