如何在 UWP 项目中使用预先设计的 SQLite? [英] How to use pre-designed SQLite in UWP project?
问题描述
我有一个来自另一个项目的 SQLite 数据库,我想在 UWP 应用程序中使用它.但是我不知道如何将这个数据库导入到项目中!
我可以创建一个新的数据库并使用它,但我不知道如何从项目中复制数据库文件.我使用 SQLite.Net-PCL nuget 包.
对于如何访问现有文件,所有应用程序都可以访问两个位置.详情请参考
假设您已经在 Assets 文件夹中添加了一个数据库文件 Sun.db
,现在您可以通过以下代码连接到它(使用 SQLite.Net-PCL Nuget 包).
path = Path.Combine(Windows.ApplicationModel.Package.Current.InstalledLocation.Path, @"Assets\Sun.db");使用 (SQLite.Net.SQLiteConnection conn = new SQLite.Net.SQLiteConnection(new SQLite.Net.Platform.WinRT.SQLitePlatformWinRT(), path)){}
但是这个文件夹是只读的.另一个位置是可以读/写的应用程序数据位置
.您可以将数据库文件从安装目录复制到应用程序数据目录中使用.以下代码示例用于连接本地文件夹中的数据库文件.
StorageFile 文件;试试{file = await Windows.Storage.ApplicationData.Current.LocalFolder.GetFileAsync("Sun.db");}抓住{StorageFile Importedfile = await StorageFile.GetFileFromApplicationUriAsync(new Uri("ms-appx:///Assets/Sun.db"));file = await Importedfile.CopyAsync(Windows.Storage.ApplicationData.Current.LocalFolder);}路径 = 文件.路径;使用 (SQLite.Net.SQLiteConnection conn = new SQLite.Net.SQLiteConnection(new SQLite.Net.Platform.WinRT.SQLitePlatformWinRT(), path)){conn.CreateTable<用户>();}
I have a SQLite database from another project and I want to use it in a UWP application. But I don't know how to import this database to the project and!
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.
For how to access an exist file, there are two locations that all apps can access. Details please reference the file-access-permissions.
One is Application install directory
. As @Henk Holterman said, you can import your existed database file into your project by right-click one folder and select Add->Existing item to add your database file to the project. Pay attention the file's Build action
property need to be set to content
. Details please see the following picture.
Suppose you already had a database file Sun.db
added to the Assets folder, and now you can connect to it by the following code( use the SQLite.Net-PCL Nuget Package).
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))
{
}
But this folder is read only. Another location is Application data locations
which can read/write. You can copy the database file from install directory to the application data directory for using. The following code example is for connecting a database file that in the local folder.
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>();
}
这篇关于如何在 UWP 项目中使用预先设计的 SQLite?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!