从创建应用程序sqlite的嵌入式数据库 [英] Creating Sqlite Embedded database from application

查看:286
本文介绍了从创建应用程序sqlite的嵌入式数据库的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有使用SQLite存储数据的WinForms应用程序。相反出货空白数据库,我可以使用脚本来创建表的用户第一次使用该应用程序?你可以指向一个C#示例

I have a winforms app that uses sqlite to store data. Instead of shipping a blank database, can I use scripts to create the tables the first time the user uses the app? Can you point to a C# example?

更​​新:我想避免运送一个空数据库。因此,如果用户安装应用程序1用户只,只有他的个人资料得到副本。所有用户的个人资料获取数据库如果安装是针对所有用户

Update: I want to avoid shipping a blank database. So if a user install the app for 1 user only, only his profile gets a copy. All users profile gets the database if the install is for all users.

推荐答案

是的,这是可能的:


  • 当应用程序第一次运行,检查数据库文件是否存在。

  • 如果没有,打开它SQLite的选项 FailIfMissing =假。这将创建一个新的文件。

  • 然后,使用SQL命令像 CREATE TABLE ... 来创建模式结构。

  • When the application first runs, check if the database file exists.
  • If it doesn’t, open it with the Sqlite option FailIfMissing=False. This will create a new file.
  • Then, use SQL commands like CREATE TABLE ... to create the schema structure.

第二步,我使用的代码看起来是这样的:

For the second step, I use code that looks something like this:

public DbConnection CreateConnectionForSchemaCreation(string fileName)
{
    var conn = new SQLiteConnection();
    conn.ConnectionString = new DbConnectionStringBuilder()
    {
        {"Data Source", fileName},
        {"Version", "3"},
        {"FailIfMissing", "False"},
    }.ConnectionString;
    conn.Open();
    return conn;
}

这篇关于从创建应用程序sqlite的嵌入式数据库的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆