SQLIte无法打开数据库

Ram*_*ama 4 c# sqlite

我刚刚开始做示例应用程序,只调用我的sqlite数据库上的一些表,我已设法解决发生在我身上的其他问题,除了这个!虽然我搜索了关于连接字符串和权限问题的建议解决方案,并且所有似乎对我的许可似乎无效,我添加了所有用户完全控制并仍然相同的错误.下面是我试图执行的代码:

// 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;
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

注意:我使用以下内容:ADO.NET SQLite数据提供程序版本1.0.82.0 2012年9月3日使用SQLite 3.7.14最初由Robert Simpson编写发布到公共领域,使用风险自负!官方提供商网站:http://system.data.sqlite.org/

我真的很感谢你对此的帮助.

Bob*_*son 7

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

"找不到表"错误意味着它找到了数据库,但没有找到您要查找的表.另一方面,"无法打开数据库文件"意味着它甚至无法找到数据库,甚至没有找到一个表.当你得到"找不到桌子"时,你更接近它正常工作.

您应该更改路径以匹配磁盘上的文件,然后使用Firefox SQLite Manager之类的工具确认english_words数据库中是否存在该表.

如果没有,你应该使用该工具创建它,如果是,你应该在这里发布另一个关于"找不到表"错误的问题.

希望这会有所帮助.

  • 我面临同样的问题,但对我来说问题是connectionString的总长度超过128个字符,包括"Datasource = C:/ <path>/<databaseName> .db".减少长度,它应该工作. (2认同)

Pee*_*eet 6

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

SQLiteConnection connection = new SQLiteConnection(connectionString, true);
connection.Open();
Run Code Online (Sandbox Code Playgroud)