2017-04-07 80 views
0

我遇到問題。我在iOS和Android應用程序中使用Xamarin。但是當我嘗試建立到文件夾路徑的連接時,它總是會引發異常。這是我的代碼: public class Queries { private string folder = DependencyService.Get()。GetAppDataFolder();Xamarin中的SQLite拋出連接異常

public bool createDataBase() 
    { 
     try 
     { 
      using (var connection = new SQLiteConnection(Path.Combine(folder, "Test.db"))) 
      { 
       connection.CreateTable<Posten>(); 
       return true; 
      } 
     } 
     catch (SQLiteException ex) 
     { 
      System.Diagnostics.Debug.WriteLine("SQLiteEx", ex.Message); 
      return false; 
     } 
    } 
} 

它在使用中引發異常。我用的是DependencyService得到我想要把我的數據庫路徑

private string folder = DependencyService.Get<IFileSystemService>().GetAppDataFolder(); 

在Android上側(我目前只是測試機器人)我已經實現了它這樣的:

public class FileSystemServiceAndroid : IFileSystemService 
{ 
    public string GetAppDataFolder() 
    { 
     return System.Environment.GetFolderPath(System.Environment.SpecialFolder.Personal); 
    } 
} 

這是異常說:

未處理的異常:

System.TypeInitializationException:本典型初始值>'SQLite.SQLiteConnection'引發異常。發生

我剛搬到哪裏我嘗試訪問CreateDatabase方法到我的工具欄被初始化代碼和我的應用程序的消息框:在 The Error

+1

什麼說異常? –

+0

何時發生異常?在構建或運行時? –

+0

僅在運行時將其部署到我的設備上 –

回答

0

在我的項目有SQLite.PCL Xamarin.Android,我使用以下來獲取數據庫文件夾和文件位置:

String dbFolder = Android.OS.Environment.ExternalStorageDirectory.AbsolutePath + "/project_name/"; 
String dbFileName = "project_name.db"; 
String dbPath = dbFolder + dbFileName; 
//To check if the database file exists: System.IO.File.Exists(dbPath); 
//To delete the database file: System.IO.File.Delete(dbPath); 
SQLiteConnection dbConn = new SQLiteConnection(dbPath); 
相關問題