2016-09-22 33 views
0

已经为PCL的Xamarin.Forms安装了NUGet软件包。我有4个Droid,iOS,Win 8.1和WinPhone 8.1项目。试图连接我的数据库,但遇到麻烦:我的Win和WinPhone项目不会看到它或返回错误的路径。跟随官方Xamarin.Forms论坛。WinPhone上SQLiteDB(或复制)的错误路径。 Xamarin.Forms PCL

接口:

public interface ISQLite 
{ 
    string GetDatabasePath(string filename); 
} 

WInPhone类:

using System.IO; 
using Windows.Storage; 
using Baumizer.WinPhone; 
using Xamarin.Forms; 

[assembly: Dependency(typeof(SQLite_WinPhone))] 
namespace Baumizer.WinPhone 
{ 
    public class SQLite_WinPhone:ISQLite 
{ 
    public SQLite_WinPhone() { } 
    public string GetDatabasePath(string filename) 
    { 
     return Path.Combine(ApplicationData.Current.LocalFolder.Path, filename); 
    } 
}} 

此类选择信息:

public class DataBase 
{ 
    SQLiteConnection connection; 

    public DataBase() 
    { 
     connection= new SQLiteConnection(DependencyService.Get<ISQLite>().GetDatabasePath("Database.db")); 
    } 

    public IEnumerable<Group> GetGroups() 
    { 
     return connection.Query<Group>("SELECT * FROM [Scedule] WHERE [facultyName] =" + "'" + Data.CurrentFaculty + "'"); 
    } 
} 

它运作良好,在Android上。在WinPhone上我得到了SQLite的异常 - 没有这样的表格:Scedule。我打开模拟器的本地目录,其中db是 - 0Kb。 我把db分配到资产并设置BuildInAction内容。什么问题?需要帮助

+0

你是否在某处创建表?或者它是一个现有的数据库? –

+0

是现有的数据库 –

回答

0

在论坛上找到这段代码,在WinPhone App.cs推杆到OnLaunched(...)

if(await ApplicationData.Current.LocalFolder.GetItemAsync(Data.DataBaseName) == null) 
     { 
      StorageFile databaseFile = await Package.Current.InstalledLocation.GetFileAsync($"Assets\\{Data.DataBaseName}"); 
      await databaseFile.CopyAsync(ApplicationData.Current.LocalFolder); 
     } 

它复制DB如果不是存在,但它是。可能是我需要删除DB 0Kb,但我不知道如何做到这一点。

这是工作,OnLaunched():

try 
     { 
      await ApplicationData.Current.LocalFolder.GetItemAsync("Scedule.db"); 
     } 
     catch (System.IO.FileNotFoundException) 
     { 
      StorageFile databaseFile = 
        await Package.Current.InstalledLocation.GetFileAsync($"Assets\\{"Scedule.db"}"); 
      await databaseFile.CopyAsync(ApplicationData.Current.LocalFolder); 
     } 

DB是uncorrectly复制。可能还有其他方法。