2012-11-19 281 views
0

我刚开始做样本应用程序,只调用我的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; 
     } 
    } 
} 

注:我用下面的assemply: SQLite的ADO.NET数据提供 版本1.0.82.0 2012年9月3日 使用SQLite 3.7.14 最初写作者:Robert Simpson 发布到公共领域,使用须自负风险! 官方供应商网站:http://system.data.sqlite.org/

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

+3

好的,有什么例外,你在哪里得到它? – cdhowie

+0

你确定,是你的db 3的版本吗? –

+0

异常是:System.Data.SQLite.SQLiteException(0x80004005):无法打开数据库文件 – Rama

回答

3

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

“表未找到”错误意味着它找到了数据库,但找不到您正在查找的表。另一方面,“无法打开数据库文件”意味着它甚至无法找到数据库,甚至不打算寻找表格。当你得到“表未找到”时,你更接近它正常工作。

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

如果没有,您应该使用该工具创建它,如果有,请在此处发布有关“表未找到”错误的其他问题。

希望有帮助。

+0

SQLite中存在一些行为,如果连接字符串上指定的数据库不存在,它将创建新的数据库,因此您将始终拥有“表未发现错误“错误的连接字符串中。 – Rama

+0

@Nasser - 是的,但是你将'FailIfMissing'设置为true,所以如果找不到它,它就不会创建它。 – Bobson

+2

我面临同样的问题,但对我来说,问题是connectionString的总长度超过128个字符,其中包括“Datasource = C:/ /.db”。减少长度,它应该工作。 –

3

当我遇到这个错误时,我不得不在SQLiteConnection构造函数中设置parseViaFrameworktrue

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

https://stackoverflow.com/questions/10875612/sqlite-c-unable-to-open-the-database-file – kikea