2012-11-19 77 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