oleg-shilo / wixsharp

Framework for building a complete MSI or WiX source code by using script files written with C# syntax.
MIT License
1.12k stars 176 forks source link

Unable to create a sqlite database. #1669

Open swapnilpachkude opened 1 week ago

swapnilpachkude commented 1 week ago

I am able to create a .db file but while creating a table using SqliteConnection throws an exception that could not find SQLite.Interop.dll file

using System; using System.Data.SQLite; using System.Windows.Forms; using WixSharp;

namespace WixSharpTestApp { public class Program { private static readonly string installDir = @"%ProgramFiles%\WixSharpTest";

    static void Main()
    {
        var project = new ManagedProject("WixSharpApp",
            new Dir(@"%ProgramFiles%\WixSharpApp",
                new File("Program.cs"),
                new File(@"Files\System.Data.SQLite.dll"),
                new File(@"Files\SQLite.Interop.dll")
              ));
        project.DefaultRefAssemblies.Add(@"Files\System.Data.SQLite.dll");
        project.AfterInstall += Project_AfterInstall;

        Compiler.BuildMsi(project);
    }

    private static void Project_AfterInstall(SetupEventArgs e)
    {
        if (e.IsInstalling)
        {
            string dbPath = @"D:\WixSharpApp\mydatabase.db";

            try
            {
                System.IO.Directory.CreateDirectory(System.IO.Path.GetDirectoryName(dbPath));
                CreateDatabase(dbPath);
                MessageBox.Show("Database file created successfully.", "Success", MessageBoxButtons.OK, MessageBoxIcon.Information);

                CreateDbData(dbPath);
                MessageBox.Show("Table created successfully.", "Success", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }
            catch (Exception ex)
            {
                MessageBox.Show($"Error in AfterInstall: {ex.Message}", "Installation Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }
    }

    private static void CreateDatabase(string dbPath)
    {
        if (!System.IO.File.Exists(dbPath))
        {
            SQLiteConnection.CreateFile(dbPath);
            MessageBox.Show("Database created at: " + dbPath, "Debug", MessageBoxButtons.OK, MessageBoxIcon.Information);
        }
        else
        {
            MessageBox.Show("Database already exists.", "Debug", MessageBoxButtons.OK, MessageBoxIcon.Information);
        }
    }

    private static void CreateDbData(string dbPath)
    {
        try
        {

            using (SQLiteConnection connection = new SQLiteConnection($"Data Source={dbPath};Version=3;Read Only=False;"))
            {
                connection.Open();
                MessageBox.Show("Connection opened successfully.", "Debug", MessageBoxButtons.OK, MessageBoxIcon.Information);

                // Set journal mode to WAL for better concurrency
                using (var pragmaCommand = new SQLiteCommand("PRAGMA journal_mode=WAL;", connection))
                {
                    pragmaCommand.ExecuteNonQuery();
                }

                string sql = "CREATE TABLE IF NOT EXISTS tbl_mails (MailId TEXT NOT NULL, EmailAddress TEXT NOT NULL);";
                using (var command = new SQLiteCommand(sql, connection))
                {
                    command.ExecuteNonQuery();
                    MessageBox.Show("Table creation executed.", "Debug", MessageBoxButtons.OK, MessageBoxIcon.Information);
                }
            }
        }
        catch (Exception ex)
        {
            MessageBox.Show($"Error creating table: {ex.Message}", "Database Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
        }

    }

}

}

Torchok19081986 commented 1 week ago

Hiho, AFAIK defaultrefassemblies is list of assemblies for later use. What happens if you add your interop.dll as well to project?

project.DefaultRefAssemblies.Add(@"Files\SQLite.Interop.dll");
oleg-shilo commented 5 days ago

Yep, this is most likely what is missing