2012-08-31 44 views
6

我使用System.Data.SQLite.dll使使用SQLite內存數據庫中。程序結束後,我想將內存數據庫轉儲到.db3文件中供下次使用。我怎樣才能在C#中實現這一點?如何使用ADO.NET將SQLite內存數據庫轉儲到文件中?

+0

澄清,請... – Hassanation

+0

我想爲如下代碼: using (var conn = new SQLiteConnection("Data Source=:memory:") { conn.Open(); // do insert, update... // how to dump data into a file? conn.Close(); } xiagao1982

回答

5

據我所知有沒有內置的功能在System.Data.SQLite.dll做到這一點。但是,功能確實存在於與SQLite核心一起維護的sqlite3.exe客戶端中。

這是我如何與system.data.sqlite.dll做到這一點:

  1. 獲得SQL語句來創建新的數據庫結構。

    select sql from sqlite_master where name not like 'sqlite_%'; 
    
  2. 獲取所有用戶表的名稱。

    select name 
    from sqlite_master 
    where type='table' and name not like 'sqlite_%'; 
    
  3. 在一些新的SQLiteConnection中創建磁盤上的數據庫。

  4. 執行所有先前獲得的SQL語句在磁盤上的數據庫中創建數據庫結構。

  5. 關閉到磁盤上的數據庫中的單獨的連接。

  6. 安裝在磁盤上的數據庫的內存數據庫。

    attach 'ondisk.db3' as 'ondisk'; 
    
  7. 對於較早獲得的每個用戶臺,從內存中拷貝內容到磁盤上的數據庫中。

    insert into ondisk.TableX select * from main.TableX; 
    insert into ondisk.TableY select * from main.TableY; 
    
相關問題