如何在 UWP 项目中使用预先设计好的 SQLite?
How to use pre-designed SQLite in UWP project?
我有一个来自另一个项目的 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.
一个是Application install directory
。正如@Henk Holterman 所说,您可以通过右键单击一个文件夹并将 select 添加-> 现有项目将您的数据库文件添加到项目中,将现有的数据库文件导入到您的项目中。注意文件的Build action
属性需要设置为content
。详情请看下图。
假设你已经有一个数据库文件 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
,可以 read/write。您可以将安装目录下的数据库文件复制到应用程序数据目录下使用。以下代码示例用于连接本地文件夹中的数据库文件。
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 应用程序中使用它。但是我不知道如何将这个数据库导入到项目中并且!
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.
一个是Application install directory
。正如@Henk Holterman 所说,您可以通过右键单击一个文件夹并将 select 添加-> 现有项目将您的数据库文件添加到项目中,将现有的数据库文件导入到您的项目中。注意文件的Build action
属性需要设置为content
。详情请看下图。
假设你已经有一个数据库文件 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
,可以 read/write。您可以将安装目录下的数据库文件复制到应用程序数据目录下使用。以下代码示例用于连接本地文件夹中的数据库文件。
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>();
}