2011-12-31 93 views
6

我在我的vb.net程序中使用System.data.sqlite.dll。而對於我的生活,我無法弄清楚激活WAL模式的代碼。System.data.sqlite - 激活WAL日誌模式

在創建數據庫或每個新的SQLiteConnection之後,是否立即激活此命令?

如果是的話就需要什麼樣的代碼中使用,現在使用類似即時通訊:

cnn As New SQLiteConnection(String.Format("Data Source={0}\{1};PRAGMA jounal_mode=WAL;", Application.StartupPath, DBName)) 

這是怎麼說PRAGMA命令應使用?

回答

8

您可以隨時使用SQLiteConnectionStringBuilder類爲你做的工作:

SQLiteConnectionStringBuilder connBuilder = new SQLiteConnectionStringBuilder(); 
    connBuilder.DataSource = filePath; 
    connBuilder.Version = 3; 
    //Set page size to NTFS cluster size = 4096 bytes 
    connBuilder.PageSize = 4096; 
    connBuilder.CacheSize = 10000; 
    connBuilder.JournalMode = SQLiteJournalModeEnum.Wal; 
    connBuilder.Pooling = true; 
    connBuilder.LegacyFormat = false; 
    connBuilder.DefaultTimeout = 500; 
    connBuilder.Password = "yourpass"; 


    using(SQLiteConnection conn = new SQLiteConnection(connBuilder.ToString())) 
    { 
    //Database stuff 
    } 
+0

請注意,您無需爲每個連接設置WAL,只需在創建數據庫時將其設置一次即可。 – UGEEN 2016-02-18 13:32:44

1

您需要執行該雜注作爲命令nonquery。

Using cmd As SQLiteCommand = cnn.CreateCommand() 
    cmd.CommandText = "PRAGMA journal_mode=WAL" 
    cmd.ExecuteNonQuery() 
End Using 

只要你保持連接打開,setting this once就足夠了。

+0

非常感謝,非常感謝。然而,我傾向於經常關閉連接,通常插入後,Update's Ect .. 這是否意味着我幾乎必須包括我的插入,更新,刪除,潛艇? – 2011-12-31 04:21:57

+1

不幸的是,是的。如果它是一個僅限本地的數據庫,那麼在整個應用程序的整個生命週期中,都不應該有任何缺點。我們始終使用具有本地數據庫的移動設備和平板電腦來執行此操作。 – 2011-12-31 04:47:42

+0

很好的例子,謝謝。 – kwoxer 2015-01-28 09:57:07

3

這是我的項目(app.config)中的樣本連接字符串:

<connectionStrings> 
    <add name="SQLiteDb" providerName="System.Data.SQLite" connectionString="Data Source=data.sqlite;Version=3;Pooling=True;Synchronous=Off;journal mode=Memory"/> 
    </connectionStrings> 

而不是journal mode=Memory你可以指定journal mode=WAL

如果您未在連接字符串中指定日記模式,則可以通過對數據庫執行PRAGMA jounal_mode=WAL查詢來手動進行切換。