2014-03-01 12 views
0

我的項目應該顯示來自sqlite3數據庫的數據。數據庫名稱是db.sqlite3。 錯誤是:無法打開數據庫文件無法在asp.net中打開Sqlite數據庫

這裏是我的代碼:

public partial class OverviewAdmin : System.Web.UI.Page 
    { 

     private SQLiteConnection sql_con; 
     private SQLiteCommand sql_cmd; 
     private SQLiteDataAdapter DB; 
     private DataSet DS = new DataSet(); 
     private DataTable DT = new DataTable(); 


     protected void Page_Load(object sender, EventArgs e) 
     { 
      LoadData(); 
     } 

     private void SetConnection() 
     { 
      sql_con = new SQLiteConnection 
       ("Data Source=db.sqlite3;"); 
     } 

     private void LoadData() 
     { 
      SetConnection(); 
      sql_con.Open(); 
      sql_cmd = sql_con.CreateCommand(); 
      string CommandText = "select * from scotty_fahrt"; 
      DB = new SQLiteDataAdapter(CommandText, sql_con); 
      DS.Reset(); 
      DB.Fill(DS); 
      DT = DS.Tables[0]; 
      GVOutput.DataSource = DT; 
      sql_con.Close(); 
     } 

數據庫文件名的也許它因爲。

回答

1

把你的數據庫文件在您的網站的APP_DATA子文件夾,更改設置順便說連接

private void SetConnection() 
    { 
     sql_con = new SQLiteConnection 
      ("Data Source=|DataDirectory|\\db.sqlite3;"); 
    } 

的方法,我會刪除全局變量的連接,改變你的SetConnection返回SQLiteConnection。這將允許應用使用語句正確處理像連接一樣的一次性也在例外的情況下

private SQLite GetConnection() 
    { 
     return new SQLiteConnection("Data Source=|DataDirectory|\\db.sqlite3;"); 
    } 

    private void LoadData() 
    { 
     using(SQLiteConnection cn = GetConnection()) 
     { 
      cn.Open(); 
      string CommandText = "select * from scotty_fahrt"; 
      DB = new SQLiteDataAdapter(CommandText, sql_con); 
      DS.Reset(); 
      DB.Fill(DS); 
      DT = DS.Tables[0]; 
      GVOutput.DataSource = DT; 
     } 
    } 
+0

我已經git另一個問題。用sqlite getconnection的方法。我必須添加參考sqlite,但它不起作用。你現在爲什麼? – schocka94

+0

您是否在項目參考中添加了對組件的引用?你是否已經將正確的using語句添加到包含對SQLite類的引用的文件的開頭? – Steve

+0

是的,我addaed SQLite(也安裝它),然後我寫了添加.SQLite;使用聲明 – schocka94