2014-01-31 72 views
0

我一直在使用本教程为基础 http://code.msdn.microsoft.com/wpapps/Using-Sqlite-with-WP8-52c3c671 和它在App.xaml中创建与此使用.sqlite3文件创建在Windows Phone数据库8

数据库在Windows Phone尝试的SQLite
string dbPath = Path.Combine(
    Windows.Storage.ApplicationData.Current.LocalFolder.Path, 
    "db.sqlite"); 
if (!FileExists("db.sqlite").Result) 
{ 
    using (var db = new SQLiteConnection(dbPath)) 
    { 
     db.CreateTable<Person>(); 
    } 
} 

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; 
} 

我有一个database.sqlite3文件与我创建的数据库,并添加到我的项目在资产文件夹。 我如何使用该文件在我的Windows Phone应用程序上创建数据库?

回答

0

在你的解决方案中添加database.sqlite3。并确保database.sqlite3构建操作属性设置为“内容”。以下代码可以帮助您在wp8应用程序中使用创建的数据库文件。

string dbPath = GetDbPath(dbFileName);

编辑

private async string GetDbPath(string fileName) 
    { 
    if (await DoesFileExistAsync(fileName)) 
     { 
     // file exists; 
     string DBPath = Path.Combine(Windows.Storage.ApplicationData.Current.LocalFolder.Path, 
     fileName); 
    } 
    else 
     { 
     // file does not exist 
     StorageFile databaseFile = await Package.Current.InstalledLocation.GetFileAsync(fileName); 
     await databaseFile.CopyAsync(ApplicationData.Current.LocalFolder); 
string DBPath=Path.Combine(Windows.Storage.ApplicationData.Current.LocalFolder.Path, fileName); 
     } 
} 

private async Task<bool> DoesFileExistAsync(string fileName) 
    { 
    try 
     { 
     await ApplicationData.Current.LocalFolder.GetFileAsync(fileName); 
     return true; 
     } 
    catch 
    { 
     return false; 
    } 
    } 
+0

其抱怨GetDbPath的返回类型应该是任务和方法应该被标记为异步 我做错了什么或者是正确的? – Ric

+0

我可以在这个PLZ上多一点帮助吗? :) – Ric

+0

@Ric请检查我的答案编辑 – Jaihind

相关问题