2016-12-07 63 views
0

我正在編寫UWP應用程序。我有PCL和UWP項目。檢查數據庫是否存在SQLite(UWP)

這樣

我創建數據庫:

public class CreatingBD 
{ 
    private string path = Path.Combine(Windows.Storage.ApplicationData.Current.LocalFolder.Path, "db.sqlite"); 
    public void Create() 
    { 
     SQLite.Net.SQLiteConnection conn = 
      new SQLite.Net.SQLiteConnection(new SQLite.Net.Platform.WinRT.SQLitePlatformWinRT(), path); 
    } 

這之後我把這種方法,當我啓動應用程序。像這樣:

public StartScreen() 
{ 
    this.InitializeComponent(); 
    CreatingBD appdatabase = new CreatingBD(); 
    appdatabase.Create(); 
} 

我需要檢查我有數據庫沒有,我怎麼能做到這一點?

+0

創建路徑變量和以前一樣使用if(File.Exists(路徑)){//文件是存在的; } – Kevin

+0

感謝@凱文的幫助 – Eugene

回答

0
public class SQLiteDatabase 
{  

    private static string dbPath = string.Empty; 
    private static string DBPath(string fileName) 
    { 
     if (string.IsNullOrEmpty(dbPath)) 
     { 
      dbPath = Path.Combine(Windows.Storage.ApplicationData.Current.LocalFolder.Path, fileName); 
     } 
     return dbPath; 
    } 
    /// <summary> 
    /// Get fileName From LocalFolder. Please Create first a dataBase. 
    /// </summary> 
    /// <param name="fileName"></param> 
    /// <returns></returns> 
    public static SQLiteConnection DbConnection(string fileName) 
    { 
     return new SQLiteConnection(new SQLitePlatformWinRT(), DBPath(fileName)); 
    } 


    //Installed Location. 
    private static string dbPathStatic = string.Empty; 
    private static string DBPathStatic(string path) 
    { 
     if (string.IsNullOrEmpty(dbPathStatic)) 
     {        
      dbPathStatic = Path.Combine(Package.Current.InstalledLocation.Path, path); 
     } 
     return dbPathStatic; 
    } 
    /// <summary> 
    /// Get fileName Path from Installed location. Note Add @ in path e.g(@"Data\Data.dta or (@"Shared\Data\Data.dta) if in Class Library), Make Data.dta Build to Content in Properties. 
    /// </summary> 
    /// <param name="fileName"></param> 
    /// <returns></returns> 
    public static SQLiteConnection DbConnectionPackage(string path) 
    { 
     return new SQLiteConnection(new SQLitePlatformWinRT(), DBPathStatic(path)); 
    } 


    //Roaming Location 
    private static string dbPathRoaming = string.Empty; 
    private static string DBPathRoaming(string fileName) 
    { 
     if (string.IsNullOrEmpty(dbPathRoaming)) 
     { 
      dbPathRoaming = Path.Combine(Windows.Storage.ApplicationData.Current.RoamingFolder.Path, fileName); 
     } 
     return dbPathRoaming; 
    } 
    /// <summary> 
    /// Get fileName Path. Please Create first a dataBase. 
    /// </summary> 
    /// <param name="fileName"></param> 
    /// <returns></returns> 
    public static SQLiteConnection DbConnectionRoaming(string fileName) 
    { 
     return new SQLiteConnection(new SQLitePlatformWinRT(), DBPathRoaming(fileName)); 
    } 

    //CustomFolder Location 
    private static string dbPathCustomFolder = string.Empty; 
    private static string DBPathCustomFolder(string fileName, StorageFolder CustomFolder) 
    { 
     if (string.IsNullOrEmpty(dbPathCustomFolder)) 
     { 
      dbPathCustomFolder = Path.Combine(CustomFolder.Path, fileName); 
     } 
     return dbPathCustomFolder; 
    } 
    /// <summary> 
    /// Get fileName Path. Please Create first a dataBase. 
    /// </summary> 
    /// <param name="fileName"></param> 
    /// <returns></returns> 
    public static SQLiteConnection DbConnectionCustomFolder(string fileName, StorageFolder knownFolder) 
    { 
     return new SQLiteConnection(new SQLitePlatformWinRT(), DBPathCustomFolder(fileName, knownFolder)); 
    } 

檢查,如果文件中存在

#region CheckFileExist 
    public static async Task<bool> IsFileExistAsync(string fileName, StorageFolder folder) 
    { 
     try 
     { 
      var item = await folder.TryGetItemAsync(fileName); 
      return item != null; 
     } 
     catch 
     { 
      return false; 
     } 
    } 
    #endregion 
相關問題