uwp数据库操作
在絕大多數(shù)應(yīng)用中,免不了要做的一項(xiàng)就是設(shè)置這樣的本地?cái)?shù)據(jù)存儲(chǔ)。簡(jiǎn)單的數(shù)據(jù)存儲(chǔ)我們可以使用 LocalSettings 或者 IsolatedStorageFile(獨(dú)立存儲(chǔ))等等的方式來進(jìn)行本地?cái)?shù)據(jù)存儲(chǔ)。但是,如果數(shù)據(jù)比較復(fù)雜,或者是存在關(guān)聯(lián)關(guān)系的情況下,這種簡(jiǎn)單的鍵值存儲(chǔ)方式是不夠用的。這時(shí)候就需要用到數(shù)據(jù)庫來進(jìn)行存儲(chǔ)。說到數(shù)據(jù)庫,小型、輕量基于文件的 SQLite 就很適合在這種場(chǎng)合使用。
一、安裝 SQLite for Universal App Platform VSIX 擴(kuò)展
打開菜單欄的工具-擴(kuò)展與更新,選擇左側(cè)的聯(lián)機(jī)選項(xiàng)卡,在右上角搜索框輸入 SQLite。
安裝上面這個(gè) SQLite for Universal App Platform 擴(kuò)展。等待安裝完成后,重新啟動(dòng) Visual Studio。
二、在項(xiàng)目中添加引用
1、添加對(duì) SQLite 的引用
新建一個(gè)項(xiàng)目(當(dāng)然在現(xiàn)有項(xiàng)目添加也可以,這里只是演示)。等待新建完成后,添加引用。
按照?qǐng)D片中的步驟,找到 SQLite for Universal App Platform,并勾選,然后按右下角的確定按鈕。
2、添加 SQLite.Net 的引用
或者可以直接在程序包管理器控制臺(tái)鍵入:Install-Package SQLite.Net-PCL 來進(jìn)行安裝。
3、確保引用無誤
確保項(xiàng)目是把這兩個(gè)包都引用上
三、開始編碼
1、編寫用于測(cè)試的 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]// 自動(dòng)增長。public int Id{get;set;}[MaxLength(5)]// 對(duì)應(yīng)到數(shù)據(jù)庫 varchar 的大小。public string Name{get;set;}} }2、編寫測(cè)試頁面的前臺(tái) 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="添加進(jìn)數(shù)據(jù)庫"Click="BtnAdd_Click"></Button><TextBlock Text="查詢"Margin="0,50,0,0"></TextBlock><Button Content="查詢所有"Click="BtnGetAll_Click"></Button></StackPanel></Grid> </Page>3、編寫測(cè)試頁面的后臺(tái) 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;//“空白頁”項(xiàng)模板在 http://go.microsoft.com/fwlink/?LinkId=402352&clcid=0x409 上有介紹namespace sqlite2 {/// <summary>/// 可用于自身或?qū)Ш街?Frame 內(nèi)部的空白頁。/// </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 對(duì)象。var addPerson = new Person() { Name = name };// 受影響行數(shù)。var count = conn.Insert(addPerson);string msg = $"新增的 Person 對(duì)象的 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($"數(shù)據(jù)庫中總共 {dbPerson.Count()} 個(gè) Person 對(duì)象。");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>/// 數(shù)據(jù)庫文件所在路徑,這里使用 LocalFolder,數(shù)據(jù)庫文件名叫 test.db。/// </summary>public readonly static string DbPath = Path.Combine(ApplicationData.Current.LocalFolder.Path, "test.db");public static SQLiteConnection GetDbConnection(){// 連接數(shù)據(jù)庫,如果數(shù)據(jù)庫文件不存在則創(chuàng)建一個(gè)空數(shù)據(jù)庫。var conn = new SQLiteConnection(new SQLitePlatformWinRT(), DbPath);// 創(chuàng)建 Person 模型對(duì)應(yīng)的表,如果已存在,則忽略該操作。conn.CreateTable<Person>();return conn;}} }
總結(jié)
- 上一篇: android中跨进程通讯的4种方式
- 下一篇: Node.js与Express4安装与配