hex*_*xOr 8 sqlite uwp windows-10-universal
我有一个来自另一个项目的SQLite数据库,我想在UWP应用程序中使用它.但我不知道如何将这个数据库导入到项目中!
我可以创建一个新的数据库并使用它,但我不知道如何从项目中复制数据库文件.我使用的是SQLite.Net-PCL nuget包.
有关如何访问现有文件,所有应用程序都可以访问两个位置.详细信息请参考文件访问权限.
一个是Application install directory.正如@Henk Holterman所说,您可以通过右键单击一个文件夹并选择Add-> Existing item将您现有的数据库文件导入项目,将数据库文件添加到项目中.注意文件的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))
{
}
Run Code Online (Sandbox Code Playgroud)
但是这个文件夹是只读的.另一个位置是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>();
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
1245 次 |
| 最近记录: |