uwp数据库操作
在絕大多數應用中,免不了要做的一項就是設置這樣的本地數據存儲。簡單的數據存儲我們可以使用 LocalSettings 或者 IsolatedStorageFile(獨立存儲)等等的方式來進行本地數據存儲。但是,如果數據比較復雜,或者是存在關聯關系的情況下,這種簡單的鍵值存儲方式是不夠用的。這時候就需要用到數據庫來進行存儲。說到數據庫,小型、輕量基于文件的 SQLite 就很適合在這種場合使用。
一、安裝 SQLite for Universal App Platform VSIX 擴展
打開菜單欄的工具-擴展與更新,選擇左側的聯機選項卡,在右上角搜索框輸入 SQLite。
安裝上面這個 SQLite for Universal App Platform 擴展。等待安裝完成后,重新啟動 Visual Studio。
二、在項目中添加引用
1、添加對 SQLite 的引用
新建一個項目(當然在現有項目添加也可以,這里只是演示)。等待新建完成后,添加引用。
按照圖片中的步驟,找到 SQLite for Universal App Platform,并勾選,然后按右下角的確定按鈕。
2、添加 SQLite.Net 的引用
或者可以直接在程序包管理器控制臺鍵入:Install-Package SQLite.Net-PCL 來進行安裝。
3、確保引用無誤
確保項目是把這兩個包都引用上
三、開始編碼
1、編寫用于測試的 Person 模型類
using SQLite.Net.Attributes; using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks;namespace sqlite2 {class Person{[PrimaryKey]// 主鍵。[AutoIncrement]// 自動增長。public int Id{get;set;}[MaxLength(5)]// 對應到數據庫 varchar 的大小。public string Name{get;set;}} }2、編寫測試頁面的前臺 Xaml 代碼
<Pagex:Class="sqlite2.MainPage"xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"xmlns:local="using:sqlite2"xmlns:d="http://schemas.microsoft.com/expression/blend/2008"xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"mc:Ignorable="d"><Grid Background="{ThemeResource ApplicationPageBackgroundThemeBrush}"><StackPanel Margin="100"><TextBlock Text="添加"></TextBlock><TextBox Header="名字"x:Name="txtAddName"></TextBox><Button Content="添加進數據庫"Click="BtnAdd_Click"></Button><TextBlock Text="查詢"Margin="0,50,0,0"></TextBlock><Button Content="查詢所有"Click="BtnGetAll_Click"></Button></StackPanel></Grid> </Page>3、編寫測試頁面的后臺 cs 代碼
using System; using System.Collections.Generic; using System.IO; using System.Linq; using System.Runtime.InteropServices.WindowsRuntime; using System.Text; using Windows.Foundation; using Windows.Foundation.Collections; using Windows.UI.Popups; using Windows.UI.Xaml; using Windows.UI.Xaml.Controls; using Windows.UI.Xaml.Controls.Primitives; using Windows.UI.Xaml.Data; using Windows.UI.Xaml.Input; using Windows.UI.Xaml.Media; using Windows.UI.Xaml.Navigation;//“空白頁”項模板在 http://go.microsoft.com/fwlink/?LinkId=402352&clcid=0x409 上有介紹namespace sqlite2 {/// <summary>/// 可用于自身或導航至 Frame 內部的空白頁。/// </summary>public sealed partial class MainPage : Page{public MainPage(){this.InitializeComponent();}private async void BtnAdd_Click(object sender, RoutedEventArgs e){string name = txtAddName.Text;using (var conn = AppDatabase.GetDbConnection()){// 需要添加的 Person 對象。var addPerson = new Person() { Name = name };// 受影響行數。var count = conn.Insert(addPerson);string msg = $"新增的 Person 對象的 Id 為 {addPerson.Id},Name 為 {addPerson.Name}";await new MessageDialog(msg).ShowAsync();}}private async void BtnGetAll_Click(object sender, RoutedEventArgs e){using (var conn = AppDatabase.GetDbConnection()){StringBuilder msg = new StringBuilder();var dbPerson = conn.Table<Person>();msg.AppendLine($"數據庫中總共 {dbPerson.Count()} 個 Person 對象。");foreach (var person in dbPerson){msg.AppendLine($"Id:{person.Id};Name:{person.Name}");}await new MessageDialog(msg.ToString()).ShowAsync();}}} }4、編寫 AppDatabase 類
using SQLite.Net; using SQLite.Net.Platform.WinRT; using System; using System.Collections.Generic; using System.IO; using System.Linq; using System.Text; using System.Threading.Tasks; using Windows.Storage;namespace sqlite2 {public static class AppDatabase{/// <summary>/// 數據庫文件所在路徑,這里使用 LocalFolder,數據庫文件名叫 test.db。/// </summary>public readonly static string DbPath = Path.Combine(ApplicationData.Current.LocalFolder.Path, "test.db");public static SQLiteConnection GetDbConnection(){// 連接數據庫,如果數據庫文件不存在則創建一個空數據庫。var conn = new SQLiteConnection(new SQLitePlatformWinRT(), DbPath);// 創建 Person 模型對應的表,如果已存在,則忽略該操作。conn.CreateTable<Person>();return conn;}} }
總結
- 上一篇: android中跨进程通讯的4种方式
- 下一篇: Node.js与Express4安装与配