C# SQLIt 无法打开数据库

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/13459742/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-10 08:39:02  来源:igfitidea点击:

SQLIte unable to open database

c#sqlite

提问by Rama

I just started working on a sample application that only calls some tables on my SQLite database, and I have managed to solve other issues that have happened except this one.

我刚刚开始开发一个示例应用程序,它只调用我的 SQLite 数据库上的一些表,并且我已经设法解决了除这个问题之外发生的其他问题。

Although I have searched for this, none of the suggested solutions for the connectionstring, permission issues and etc seem to be valid and working for me. For the permissions, I added the Everyoneuser with full control, and I still get the same error.

虽然我已经搜索过这个,但对于连接字符串、权限问题等的建议解决方案似乎都没有对我有效并且对我有用。对于权限,我添加了Everyone具有完全控制权的用户,但仍然出现相同的错误。

Below is the code that I am trying to execute:

下面是我试图执行的代码:

// calling function
void getRecords2()
    {
        MySqlLite.DataClass ss = new MySqlLite.DataClass();
        DataTable dt = ss.selectQuery("select * from english_words"); 
    }

// the SQLite class that execute the code
using System.Data;
using System.Data.SQLite;

namespace MySqlLite
{
    class DataClass
    {
        private SQLiteConnection sqlite;

        public DataClass()
        {            
            //This part killed me in the beginning.  I was specifying "DataSource"
            //instead of "Data Source"
            sqlite = new SQLiteConnection(@"Data Source=C:\testwork\db\MrPick.sqlite3.db;Version=3;FailIfMissing=True");

        }

        public DataTable selectQuery(string query)
        {
            SQLiteDataAdapter ad;
            DataTable dt = new DataTable();

            try
            {
                SQLiteCommand cmd;
                sqlite.Open();  //Initiate connection to the db
                cmd = sqlite.CreateCommand();
                cmd.CommandText = query;  //set the passed query
                ad = new SQLiteDataAdapter(cmd);
                ad.Fill(dt); //fill the datasource

                cmd.Dispose();
                sqlite.Dispose();  

            }
            catch (SQLiteException ex)
            {
                //Add your exception code here.
            }
            sqlite.Close();
            return dt;
        }
    }
}

Note: I used the following assembly:

注意:我使用了以下程序集:

ADO.NET SQLite Data Provider
Version 1.0.82.0 September 3, 2012
Using SQLite 3.7.14
Originally written by Robert Simpson
Released to the public domain, use at your own risk!
Official provider website: http://system.data.sqlite.org/

I really appreciate your help on this.

我真的很感谢你在这方面的帮助。

采纳答案by Bobson

Per your comment, you're getting a "Unable to open database file" error because you're pointing the code at a file that doesn't exist.

根据您的评论,您收到“无法打开数据库文件”错误,因为您将代码指向不存在的文件。

A "Table not found" error means it found the database, but not the table you were looking for. On the other hand, "Unable to open database file" means that it couldn't even find the database, and didn't even bother looking for a table. You're much closer to it working correctly when you're getting "Table not found".

“找不到表”错误意味着它找到了数据库,但没有找到您要查找的表。另一方面,“无法打开数据库文件”意味着它甚至找不到数据库,甚至没有费心寻找表。当您收到“找不到表”时,您就离它正常工作更近了。

You should change the path back to match the file on disk, then use a tool like the Firefox SQLite Managerto confirm that the english_wordstable does exist in your database.

您应该将路径改回以匹配磁盘上的文件,然后使用诸如Firefox SQLite Manager 之类的工具来确认该english_words表确实存在于您的数据库中。

If it doesn't, you should create it with that tool, and if it does, you should post another question here about the "Table not found" error.

如果没有,您应该使用该工具创建它,如果有,您应该在此处发布另一个关于“找不到表”错误的问题。

Hopefully that helps.

希望这有帮助。

回答by Peet

When I ran into this error I had to set parseViaFrameworkin the SQLiteConnectionconstructor to true.

当我遇到这个错误时,我不得不parseViaFrameworkSQLiteConnection构造函数中设置为true.

SQLiteConnection connection = new SQLiteConnection(connectionString, true);
connection.Open();