简体   繁体   English

如何使用ADO.NET将SQLite内存数据库转储到文件中?

[英]How to dump SQLite in-memory database into file with ADO.NET?

I am using System.Data.SQLite.dll to make use of the SQLite in-memory database. 我正在使用System.Data.SQLite.dll来使用SQLite内存数据库。 After the program finishes, I would like to dump the in-memory database into a .db3 file for next use. 程序完成后,我想将内存数据库转储到.db3文件中以供下次使用。 How can I achieve this in C#? 我怎样才能在C#中实现这一目标?

To the best of my knowledge there is not built-in functionality to accomplish this in System.Data.SQLite.dll. 据我所知,在System.Data.SQLite.dll中没有内置功能来完成此任务。 The functionality does however exist in the sqlite3.exe client maintained along with the SQLite core. 但是,与SQLite核心一起维护的sqlite3.exe客户端中存在该功能。

This is how I would do it with system.data.sqlite.dll: 这是我用system.data.sqlite.dll做的方法:

  1. Obtain SQL statements to create new database structure. 获取SQL语句以创建新的数据库结构。

     select sql from sqlite_master where name not like 'sqlite_%'; 
  2. Obtain names of all user tables. 获取所有用户表的名称。

     select name from sqlite_master where type='table' and name not like 'sqlite_%'; 
  3. Create the on-disk database in some new SQLiteConnection. 在一些新的SQLiteConnection中创建磁盘数据库。

  4. Execute all previously obtained SQL statements to create the database structure in the on-disk database. 执行所有以前获取的SQL语句,以在磁盘数据库中创建数据库结构。

  5. Close the separate connection to the on-disk database. 关闭与磁盘数据库的单独连接。

  6. Attach the on-disk database to the in-memory database. 将磁盘数据库附加到内存数据库。

     attach 'ondisk.db3' as 'ondisk'; 
  7. For each user table obtained earlier, copy the content from the in-memory to the on-disk database. 对于先前获取的每个用户表,将内容从内存中复制到磁盘上数据库。

     insert into ondisk.TableX select * from main.TableX; insert into ondisk.TableY select * from main.TableY; 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM