如何在UWP项目中使用预先设计的SQLite?
我有一个来自另一个项目的SQLite数据库,我想在UWP应用程序中使用它.但我不知道如何将这个数据库导入到项目中!
有关如何访问现有文件,所有应用程序都可以访问两个位置.详情请参考file-access-permissions. 一个是Application安装目录.正如@Henk Holterman所说,您可以通过右键单击一个文件夹并选择Add-> Existing item将您现有的数据库文件导入项目,将数据库文件添加到项目中.注意文件的Build action属性需要设置为content.详情请见下图. 假设您已经将数据库文件Sun.db添加到Assets文件夹中,现在您可以通过以下代码连接到它(使用SQLite.Net-PCL Nuget包). path = Path.Combine(Windows.ApplicationModel.Package.Current.InstalledLocation.Path,@"AssetsSun.db");
using (SQLite.Net.SQLiteConnection conn = new SQLite.Net.SQLiteConnection(new SQLite.Net.Platform.WinRT.SQLitePlatformWinRT(),path))
{
}
但是这个文件夹是只读的.另一个位置是可以读/写的应用程序数据位置.您可以将数据库文件从安装目录复制到应用程序数据目录以供使用.以下代码示例用于连接本地文件夹中的数据库文件. 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>();
} (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |
