【问题标题】:C# deploy with SQL Lite database to work on other PCC# 使用 SQLite 数据库部署以在其他 PC 上工作
【发布时间】:2018-04-25 13:33:53
【问题描述】:

我有一个 WPF 项目,我在其中通过 NuGet 添加了 SQL Lite。

我的主要目标是编写一个函数,如果数据库不存在,则创建一个数据库,然后向其中写入记录。

它在我的 PC 上运行良好,但在我部署应用程序并在其他 PC 上运行后,它立即崩溃。 (我将 *.db 文件添加到解决方案中,但它也没有帮助)

(我的问题是它错过了另一台 PC 上 SQL Lite 中的 *.dll 文件)

我应该怎么做才能让它在其他电脑上也能正常工作?

到目前为止我的代码:

string relativePath = @"getstarted.db";
SQLiteConnection conn = new SQLiteConnection("Data Source=" + relativePath);
conn.Open();
using (SQLiteCommand mCmd = new SQLiteCommand("CREATE TABLE IF NOT EXISTS [Test Table] (id INTEGER PRIMARY KEY AUTOINCREMENT, 'username' TEXT, 'password' TEXT);", conn))
{
   mCmd.ExecuteNonQuery();
}
string Query = "insert into [Test Table](username, password) values('" + "testuser" + "','" + "testpassword" + "')";
SQLiteCommand insercommand = new SQLiteCommand(Query, conn);
insercommand.CommandType = CommandType.Text;
insercommand.ExecuteNonQuery();

【问题讨论】:

  • “(我的问题是它错过了另一台 PC 上 SQL Lite 的 *.dll 文件)” - 如果您没有传输这些文件,您可能是对的。只需将它们与您的 exe 一起复制即可。
  • 我做了,但也没有用..
  • 您要在哪里创建数据库?您不能只写入程序文件中的文件夹。使用应用数据。
  • 你能更具体地说明我应该在 appdata 中做什么/更改吗?
  • 您在打开数据库文件之前是否必须创建它:'SQLiteConnection.CreateFile("getstarted.db");'

标签: c# wpf sqlite deployment


【解决方案1】:

[已解决]

基本上我改变了 3 件事。

  1. 我在解决方案中添加了 x86 和 x64 SQLiteInterop.Dll-s。

  2. 我把connectionString改成了

    字符串 mDbPath = System.AppDomain.CurrentDomain.BaseDirectory + "getstarted.db";

  3. 我把构造函数改成

    SQLiteConnection conn = new SQLiteConnection("Data Source=" + mDbPath,true);

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2010-10-06
    • 2020-01-28
    • 1970-01-01
    • 2019-12-25
    • 2015-09-25
    • 1970-01-01
    相关资源
    最近更新 更多