日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 运维知识 > 数据库 >内容正文

数据库

uwp数据库操作

發布時間:2025/6/15 数据库 20 豆豆
生活随笔 收集整理的這篇文章主要介紹了 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;}} }


總結

以上是生活随笔為你收集整理的uwp数据库操作的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。