2014-09-20 47 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

相关问题