2016-01-12 418 views
1

我使用Xamarin Forms開發應用程序並使用SQlite存儲用戶詳細信息。剛開始使用Windows(Windows 10)。 SQLite是否支持UWP,我提到了一些網站,並說它支持。但是當我嘗試時,連接始終爲空。SQLite支持通用Windows應用程序

我使用的代碼:

public SQLite.Net.SQLiteConnection GetConnection() 
{ 
    var sqliteFilename = "Sample.db3"; 
    string path = Path.Combin(ApplicationData.Current.LocalFolder.Path, sqliteFilename); 
    if (!File.Exists(path)) 
    { 
    File.Create(path + sqliteFilename); 
    } 
    var plat = new SQLite.Net.Platform.WinRT.SQLitePlatformWinRT(); 
    var conn = new SQLite.Net.SQLiteConnection(plat,path); 

    return conn; 
    } 
} 

任何幫助或建議將不勝感激。

注:我已經安裝了SQLite.Net-PCL並加入參考SQLite的通用應用平臺

+0

您是否按照Windows的這些說明進行操作? https://developer.xamarin.com/guides/xamarin-forms/working-with/databases/#Adding_SQLite_to_WinPhone – Jason

+0

是的,我確實按照說明 – Sudha

+0

你也見過http://ef.readthedocs.org/en/latest/工具入門/ uwp.html? –

回答

0

你App.cs裏面我有一個叫靜態變量:

public static LocalDatabase Database { get; private set; } 

public App() 
{ 
    Database = new LocalDatabase();... 
} 

然後你可以在你的控制器等中的任何地方訪問你的數據庫類:App.Database

僅供參考LocalDatabase類將包含:

public class LocalDatabase 
{ 
    static readonly object locker = new object(); 

    static SQLiteConnection database; 

    string DatabasePath { 
     get { 
      const string sqliteFilename = "LocalDatabaseSQLite.db3"; 
#if __IOS__ 
      string documentsPath = Environment.GetFolderPath (Environment.SpecialFolder.Personal); // Documents folder 
      string libraryPath = Path.Combine (documentsPath, "..", "Library"); // Library folder 
      var path = Path.Combine (libraryPath, sqliteFilename); 
#else 
#if __ANDROID__ 
      string documentsPath = Environment.GetFolderPath(Environment.SpecialFolder.Personal); // Documents folder 
      var path = Path.Combine(documentsPath, sqliteFilename); 
#else 
      // WinPhone 
      var path = Path.Combine(Windows.Storage.ApplicationData.Current.LocalFolder.Path, sqliteFilename); ; 
#endif 
#endif 
      return path; 
     } 
    } 

    public LocalDatabase() 
    { 
     database = new SQLiteConnection (DatabasePath); 

     database.CreateTable<UserSQLModel>(); 

     //All your create tables... 
    } 
} 
相關問題