2015-10-26 19 views
0

我想爲我的ASP.Net網站使用SQLite。 該代碼在Windows應用程序中正常工作,但不在WebAPP中。我想我必須更改一些文件權限才能讓網站設置數據庫。但是如何?如何配置權限:用於ASP.Net網站的SQLite?

我總是在open()聲明中得到一個錯誤。說cannot open file

繼代碼,這是在WindowsAPP做工精細:

Dim connDB as SQLiteConnection = New SQLiteConnection("PlayerDB.sqlite") 
Dim conn = New SQLiteConnection("Data Source=PlayerDB.sqlite;Version=3") 
conn.open() ---> ERROR 
... 

我希望有人能幫助我快速。

謝謝。

回答

0

有兩個原因讓你可以得到這個錯誤,所以你必須確保在運行你的代碼之前,這兩個都需要照顧。

  • You need to specify the absolute path to the database in your connection string and not a relative path that you are currently using in your code因爲SQLite數據庫只是一個文件。爲此,您需要在ASP.Net應用程序中爲sqlite數據庫創建一個特殊文件夾。假設您已經在根文件夾下創建了一個名爲sqlite的文件夾。那麼你的代碼應該如下。 Server.MapPath是ASP.Net框架中的一種實用方法,可將根相對路徑轉換爲類似於c:\abc\wwq\myfile.db的絕對路徑。

    Dim connDB as SQLiteConnection = New SQLiteConnection(string.Format("Data 
         Source={0};Version=3;",Server.MapPath("~/sqlite/PlayerDB.sqlite"))); 
    
    conn.open() ---> NO ERROR should happen now 
    
  • You should also make sure that the ASP.Net application identity is allowed read/write access to the sqlite的folder created in above step你的web應用程序的根目錄下,因爲數據將被讀取以及寫入到該文件夾​​下的數據庫文件。

    sqlite文件夾只需點擊右鍵,選擇屬性然後安全選項卡,如下面的屏幕截圖。在大多數本地主機的情況下,ASP.Net標識是IIS_IUSRS,在下面突出顯示。確保此用戶完全控制sqlite文件夾。

enter image description here