检查数据库是否存在SQLite(UWP)

时间:2016-12-07 15:27:53

标签: c# visual-studio sqlite uwp

我正在编写UWP应用程序。我有PCL和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();
}

我需要检查我有数据库oк否,我该怎么做?

1 个答案:

答案 0 :(得分: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