sqlite - 如何在 UWP 项目中使用预先设计好的 SQLite?

标签 sqlite uwp windows-10-universal

我有一个来自另一个项目的 SQLite 数据库,我想在 UWP 应用程序中使用它。但是我不知道如何将这个数据库导入到项目中并且!

最佳答案

I can create a new Database and use it but I don't know how to copy database file from project. I use from SQLite.Net-PCL nuget package.

关于如何访问现有文件,有两个所有应用程序都可以访问的位置。详情请引用file-access-permissions .

一个是应用程序安装目录。正如@Henk Holterman 所说,您可以通过右键单击一个文件夹并选择 Add->Existing item 将您的数据库文件添加到项目中,从而将现有的数据库文件导入到您的项目中。注意文件的 Build action 属性需要设置为 content。详情请看下图。 Sun.db in Assets folder

假设您已经将一个数据库文件 Sun.db 添加到 Assets 文件夹中,现在您可以通过以下代码连接到它(使用 SQLite.Net-PCL Nuget 包)。

  path = Path.Combine(Windows.ApplicationModel.Package.Current.InstalledLocation.Path, @"Assets\Sun.db");
  using (SQLite.Net.SQLiteConnection conn = new SQLite.Net.SQLiteConnection(new SQLite.Net.Platform.WinRT.SQLitePlatformWinRT(), path))
  {
  }

但是这个文件夹是只读的。另一个位置是可以读/写的Application data locations。您可以将安装目录下的数据库文件复制到应用程序数据目录下使用。以下代码示例用于连接本地文件夹中的数据库文件。

  StorageFile file;
  try
  {
      file = await Windows.Storage.ApplicationData.Current.LocalFolder.GetFileAsync("Sun.db");
  }
  catch
  {
      StorageFile Importedfile = await StorageFile.GetFileFromApplicationUriAsync(new Uri("ms-appx:///Assets/Sun.db"));
      file = await Importedfile.CopyAsync(Windows.Storage.ApplicationData.Current.LocalFolder);
  }
  path = file.Path;

  using (SQLite.Net.SQLiteConnection conn = new SQLite.Net.SQLiteConnection(new SQLite.Net.Platform.WinRT.SQLitePlatformWinRT(), path))
  {
      conn.CreateTable<User>();
  }

关于sqlite - 如何在 UWP 项目中使用预先设计好的 SQLite?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38036834/

相关文章:

c# - UWP 中的 httpclient 问题

listview - 如何在 ListView 中拉伸(stretch) ListView 项

c# - 在样式文件中使用 setter 覆盖控制资源

ruby-on-rails - 我的 Rails 控制台不断崩溃

sqlite,当表达到最大记录数时触发

c# - x :Bind to DependencyProperty not working (classic Binding works fine)

c# - UWP 应用 : Process Memory Usage vs Heap Usage

android - 在 sqlite (Android) 中锁定数据库或表

android - PRIMARY KEY 必须是调用更新时的唯一异常

visual-studio-2015 - 从 Xamarin Forms PCL 中删除 ios、windows8 和 wp8 - nuget 3.0 选择加入错误?