2016-12-07 62 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 
相关问题