首页 > 解决方案 > C# 在发布中的 ASP.NET 中创建 SQLite 数据库导致空数据库

问题描述

我有一个奇怪的问题,在我在网络服务器上创建和填充 SQLite 数据库后,下载后它完全是空的。这是我正在做的事情的片段。

if (!System.IO.File.Exists(sqlitePath))
    SQLiteConnection.CreateFile(sqlitePath);
else
{
    File.Delete(sqlitePath);
    SQLiteConnection.CreateFile(sqlitePath);
}

这是从文本文件加载的 SQL 语句:

CREATE TABLE "tblDriver" (
    `Driver_KeyID`  INTEGER NOT NULL,
    `FirstName` TEXT NOT NULL,
    `LastName`  TEXT NOT NULL
    PRIMARY KEY(`Driver_KeyID`)
)

并通过此方法执行

public void ExecuteSQLiteCommand(List<string> queries, string sqlitePath)
{
    using (var conn = new SQLiteConnection(string.Format(@"Data Source={0};Version=3;", sqlitePath)))
    {
        try
        {
            conn.Open();

            using (SQLiteTransaction transaction = conn.BeginTransaction())
            {
                foreach (var qry in queries)
                {
                    SQLiteCommand cmd = new SQLiteCommand(qry, conn);

                    cmd.ExecuteNonQuery();
                }

                transaction.Commit();
            }
        }
        catch (Exception ex)
        {
            Console.WriteLine(ex.ToString());
        }
    }
}

然后我用

string driverInsertSQL = "INSERT INTO `tblDriver` (`Driver_KeyID`, `FirstName`, `LastName`) VALUES ";

var drivervalues = (from d in drivers.AsEnumerable()
                    select string.Format("({0}, '{1}', '{2}')",
                        int.Parse(d["KeyID"].ToString()),
                        d["FirstName"].ToString(),
                        d["LastName"].ToString()
                    )
                );

foreach (var item in drivervalues)
{
    driverList.Add(string.Format("{0}{1}", driverInsertSQL, item));
}

ExecuteSQLiteCommand(driverList, sqlitePath);

然后我下载文件。当我在 DEBUG 中时,数据库中填充了我所有的数据,但是当我在 RELEASE 中时,甚至没有表。在具有完全相同代码的控制台应用程序中,它以 DEBUG 或 RELEASE 填充数据库。

看起来好像它没有在 RELEASE 中提交数据——即使在我添加了该SQLiteTransaction片段之后也是如此。

有任何想法吗?

标签: c#asp.netsqlite

解决方案


我发现了这个问题。 SQLite.Interop.dll/bin/ 目录中没有更新。即使通过 nuget 更新后,我仍然有旧版本。


推荐阅读