简体   繁体   English

在.exe C#应用程序中嵌入.sqlite数据库

[英]Embed .sqlite database in .exe c# application

I want to embed the SQLite database into my C# windows form app (.exe) so I can give a very non-tech savvy client just the .exe file and nothing else. 我想将SQLite数据库嵌入到我的C#Windows窗体应用程序(.exe)中,这样我就可以给一个非技术精通的客户端,仅提供.exe文件,而别无其他。 The database is read-write and I need to access its path for the DataSource like so: 该数据库是可读写的,我需要像下面这样访问DataSource的路径:

 SQLconnect = new SQLiteConnection("DataSource=" + path + ";Version=3;Compress=True;");

I also tried saving the database to a folder on Desktop but SQLiteConnection.CreateFile(path) seems to fail because if I put the .exe file outside the bin/debug folder, .exe does not open at all (but it creates the necessary folder on Desktop). 我还尝试将数据库保存到Desktop上的文件夹中,但是SQLiteConnection.CreateFile(path)似乎失败,因为如果将.exe文件放在bin / debug文件夹之外,.exe根本不会打开(但它将创建必要的文件夹)在桌面上)。

But as long as it is in the bin/debug, it creates the database file and creates the tables and .exe works perfectly. 但是只要它在bin / debug中,它就会创建数据库文件并创建表,.exe可以完美运行。 There are no errors raised at any point in the application. 在应用程序中的任何时候都不会引发任何错误。

To include the SQLite dlls you will have to modify the property of the reference and mark it as Embedded Resource . 要包括SQLite dll,您将必须修改引用的属性并将其标记为Embedded Resource You will probably have to use ILMerge See this . 您可能必须使用ILMerge看到此内容

You cannot embed the database. 您不能嵌入数据库。 So, you have the following two choices : 因此,您有以下两种选择:

  1. You can create In-Memory/Temporary database . 您可以创建内存/临时数据库

    var connection = new SQLiteConnection("Data Source=:memory:"); var connection = new SQLiteConnection(“ Data Source =:memory:”);

  2. Create the database on the go. 随时随地创建数据库。 On your application startup, you can create and seed the database. 在应用程序启动时,您可以创建数据库并为其设定种子。 See this . 看到这个

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

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