Creating Sqlite Embedded database from application

前端 未结 1 985
梦谈多话
梦谈多话 2020-12-14 12:40

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

相关标签:
1条回答
  • 2020-12-14 13:35

    Yes, this is possible:

    • 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;
    }
    
    0 讨论(0)
提交回复
热议问题