【问题标题】:Error trying to query SQLite3 database in C#尝试在 C# 中查询 SQLite3 数据库时出错
【发布时间】:2012-04-01 10:08:04
【问题描述】:

我正在尝试使用以下代码以编程方式访问 SQLite 3 数据库:

private void button1_Click(object sender, EventArgs e)
{
    try
    {

        string connectionPath = @"Data Source=C:\temp\sms.db";

        SQLiteConnection connection = new SQLiteConnection(connectionPath);

        SQLiteCommand command = connection.CreateCommand();
        connection.Open();
        string query = "SELECT * FROM Message";
        command.CommandText = query;
        command.ExecuteNonQuery();

        SQLiteDataAdapter dataAdaptor = new SQLiteDataAdapter(command);
        DataSet dataset = new DataSet();
        dataAdaptor.Fill(dataset, "messages");
        dataGridView1.DataSource = dataset.Tables["messages"];
        connection.Close();

    }

    catch
    {
        MessageBox.Show("error", "error", MessageBoxButtons.OK);
    }
}

但是,每当我按下分配此代码的按钮时,它都不会捕捉到 Try Catch 并显示我的消息框,它会冻结几秒钟并调试到 Program.cs 文件如下所示的行

    static void Main()
    {
        Application.EnableVisualStyles();
        Application.SetCompatibleTextRenderingDefault(false);
        Application.Run(new frmMainWindow()); **<----This Line**
    }
}

然后在该行向我显示此错误

无法加载文件或程序集'System.Data.SQLite,版本=1.0.76.0,文化=中性, PublicKeyToken=db937bc2d44ff139' 或其依赖项之一。
试图加载格式不正确的程序。

如何克服此错误以便查询数据库?

编辑

我的开发操作系统是 Win 7 x64 Prof
我已经安装了:
64 位 Windows (.NET Framework 4.0) 的设置
32 位 Windows (.NET Framework 4.0) 的设置
从这里system.data.sqlite.org/index.html/doc/trunk/www/downloads.wiki

【问题讨论】:

  • 听起来像是 32 位/64 位不匹配。您的开发操作系统是 64 位的吗?如果是这样:您是否安装了两个版本的 SQLite?棘手的部分是:由于 Visual Studio 是 32 位的,因此在 VS 中运行时需要 32 位版本。如果你在 VS 之外运行,你会默认获得 64 位版本(除非你明确地只为“x86”编译)

标签: c# visual-studio-2010 sqlite


【解决方案1】:

您使用的是什么 dll?如果您在 x64 系统中,则必须使用 SQLite 的 x64 dll

【讨论】:

    【解决方案2】:

    原来 Visual Studio 出于某种原因错误地引用了不同版本的 System.Data.SQLite.dll

    我删除了旧的引用,并将新的 System.Data.SQLite.dll 文件复制到该位置:

    C:\Program Files (x86)\Reference Assemblies\Microsoft\Framework.NETFramework\v4.0\Profile\Client

    然后再次使用 Visual Studio 添加对它的引用,它现在可以工作了

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2013-04-19
      • 1970-01-01
      • 2021-03-29
      • 1970-01-01
      • 2015-07-08
      • 2022-01-11
      相关资源
      最近更新 更多