【问题标题】:Check if database is exists SQLite (UWP)检查数据库是否存在 SQLite (UWP)
【发布时间】:2017-04-22 14:58:04
【问题描述】:

我正在编写 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();
}

我需要检查我有没有数据库,我该怎么做?

【问题讨论】:

  • 像以前一样创建路径变量并使用 if(File.Exists(path)) { // File is there; }
  • 感谢@Kevin 的帮助

标签: c# visual-studio sqlite uwp


【解决方案1】:
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

【讨论】:

    猜你喜欢
    • 2014-04-03
    • 2017-10-21
    • 1970-01-01
    • 2015-12-20
    • 1970-01-01
    • 2013-10-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多