2014-09-20 81 views
1

我試着通過IsolatedStorage訪問我的新SQLite數據庫,但是當我做的:如何通過獨立存儲訪問SQLite數據庫?

new SQLiteConnection(_dbpath); 

的數據庫中找不到。
這裏是我的代碼來創建文件:

IsolatedStorageFile isoStore = IsolatedStorageFile.GetUserStoreForApplication(); 
if (!isoStore.FileExists("TestDB.sqlite")) 
{ 
    isoStore.CreateFile("TestDB.sqlite"); 
} 
_dbpath = System.IO.Path.Combine(Windows.Storage.ApplicationData.Current.LocalFolder.Path, "SportInDB.sqlite"); 

我錯過了什麼,同時創造了新的SQLiteConnection?

回答

0

最簡單的例子呢?它創建一個數據庫,其表格與class Question匹配。


// namespaces 
using SQLite; 
using System.IO; 
using System.IO.IsolatedStorage; 
using System.Threading.Tasks; 

// define a class like table to create 
public class Question 
{ 
    [SQLite.PrimaryKey, SQLite.AutoIncrement] 
    public int Id { get; set; } 
    public int Status { get; set; } 
} 

public partial class MainPage : PhoneApplicationPage 
{ 
    string dbPath = ""; 

    // Constructor 
    public MainPage() 
    { 
     InitializeComponent(); 

     // combine the local folder with the file name of the database 
     dbPath = Path.Combine(Windows.Storage.ApplicationData.Current.LocalFolder.Path, "db.sqlite");       

     CreateDBTable();    
    } 

    // from the MSDN article for getting SQLite to work 
    private async Task<bool> FileExists(string fileName) 
    { 
     var result = false; 
     try 
     { 
      var store = await Windows.Storage.ApplicationData.Current.LocalFolder.GetFileAsync(fileName); 
      result = true; 
     } 
     catch 
     { 
     } 
     return result; 
    } 

    // if the file doesn't exist, create it with the db.CreateTable 
    public void CreateDBTable() 
    { 
     if (!FileExists("db.sqlite").Result) 
     { 
      using (var db = new SQLiteConnection(dbPath)) 
      { 
       db.CreateTable<Question>(); 
      } 
     } 
    } 
} 
+0

我的Silverlight應用程序工作,我沒有Windows.Storage.ApplicationData.Current.LocalFolder.Path – MatDev8 2014-09-20 17:15:26

+0

當我做新的連接路徑沒有找到,但getfileasync返回良好值.. – MatDev8 2014-09-20 17:42:37

+0

如果您在8.0 WP Silverlight或8.1 WP Silverlight中沒有'Windows.Storage.ApplicationData.Current.LocalFolder.Path',那麼您遇到了很大的問題。你需要先解決這個問題。從模板創建一個空白的銀色燈光應用程序,你可以在那裏訪問它,或者在文本下面是否有紅線? – 2014-09-20 20:04:31

相關問題