微軟WP7本地數(shù)據(jù)庫之SQLite編程技巧
在上一篇《微軟WP7本機(jī)數(shù)據(jù)庫解決方案之SQLite》中我們簡要介紹了Sqlite Client for Windows Phone的主要功能及相關(guān)的輔助開發(fā)工具。特別注意的是,我們通過工具類DBHelper實(shí)現(xiàn)了SQLite數(shù)據(jù)庫與WP7系統(tǒng)本地存儲功能的關(guān)聯(lián)。在本篇文章中,我們將著手構(gòu)建一個簡單的Windows Phone 7客戶端應(yīng)用程序,并將具體探討Sqlite Client for Windows Phone在WP7開發(fā)中的基本應(yīng)用思路和相關(guān)編程技巧。
本系列文章中的案例程序調(diào)試環(huán)境:
- Windows 7;
- .NET 4.0;
- Visual Studio 2010;
- Windows Phone Developer Tools RTW;
- Sqlite Client for Windows Phone (http://sqlitewindowsphone.codeplex.com/);
- (推薦) sqlite-manager (http://code.google.com/p/sqlite-manager/);
- (推薦) MVVM Light Toolkit (http://mvvmlight.codeplex.com/).
一、構(gòu)建簡易數(shù)據(jù)驅(qū)動型WP7客戶信息編輯器
啟動Visual Studio 2010中創(chuàng)建一個簡單的Windows Phone 7應(yīng)用程序,并命名為WP7SQLiteClient。然后,打開解決方案,并添加一個到程序集Community.CsharpSqlite.WP.dll的引用(在上文中剛剛構(gòu)建的Sqlite Client for Windows Phone源碼工程的bin目錄下)。
1.運(yùn)行時的截圖預(yù)覽
為了能夠更好地了解后文的解釋,讓我們首先看一下示例程序的運(yùn)行時快照,如圖1所示。
▲圖1. 客戶編輯器的初始畫面
注意到,在最初的客戶編輯器截圖中只提供了三個使用Sqlite Manager工具創(chuàng)建的記錄。當(dāng)您點(diǎn)擊按鈕“Add”后將有五個客戶被添加到客戶表Customer中。在此示例中,我沒有立即刷新屏幕。但是,如果您按下硬件后退按鈕(我們已經(jīng)使用了仿真器),然后再次導(dǎo)航回到這個屏幕,你便會看到新追加的5條記錄,如圖2所示。
▲圖2. 五個客戶記錄添加到表Customer中
現(xiàn)在,按下圖中的按鈕“Del Last 1”,則數(shù)據(jù)庫database1.sqlite中Customer表格中的最后一個記錄將被刪除。圖3給出了相關(guān)截圖。
▲圖3. 最后一條記錄從表Customer中刪除
接下來,讓我們看看后臺代碼中的具體編程技術(shù)。
#p#
2.后臺代碼編程
首先,為了實(shí)現(xiàn)數(shù)據(jù)庫的全局訪問的方便(例如從每一個子頁面中訪問數(shù)據(jù)庫),我們最好在App類中定義一個公共屬性db。
清單2:在App類中定義一個公共屬性db方便數(shù)據(jù)庫訪問
- public partial class App : Application
- {
- private DBHelper _db;
- public DBHelper db
- {
- get
- {
- Assembly assem = Assembly.GetExecutingAssembly();
- if (_db == null)
- _db = new DBHelper(assem.FullName.Substring(0, assem.FullName.IndexOf(',')), "database1.sqlite");
- return _db;
- }
- }
- // ...其他省略
注意到,公共屬性db關(guān)聯(lián)到上文中的工具類DBHelper。另外,請注意我們是如何使用程序集相關(guān)操作并結(jié)合上文中SQLite數(shù)據(jù)庫文件的Build Action屬性來實(shí)現(xiàn)訪問這個數(shù)據(jù)庫文件的。
上述短短的代碼足矣。接下來,讓我們在示例頁面TestDataEditor.xaml相應(yīng)的后臺代碼文件TestDataEditor.xaml.cs中實(shí)現(xiàn)一些基本的初始化操作。
清單3:
- //其他省略...
- using WP7SQLiteClient.Dal;
- using System.Collections.ObjectModel;//ObservableCollection
- using System.ComponentModel;
- using SQLiteClient;
- using Community.CsharpSqlite;
- using System.Collections;
- namespace WP7SQLiteClient
- {
- public partial class TestDataEditor : PhoneApplicationPage
- {
- ObservableCollection _customerEntries = null;
- public TestDataEditor()
- {
- InitializeComponent();
- //retrieve dat
- string strSelect = "SELECT ID,Name,Email,Desc FROM Customer ORDER BY ID ASC";
- _customerEntries = (Application.Current as App).db.SelectObservableCollection(strSelect);
- foreach (Customer data in _customerEntries)
- {
- TextBlockID.Text += data.ID + Environment.NewLine;
- TextBlockName.Text +=data.Name + Environment.NewLine;
- TextBlockEmail.Text +=data.Email + Environment.NewLine;
- TextBlockDesc.Text +=data.Desc + Environment.NewLine;
- }
- }
- //其他省略...
在上面的代碼中,我們首先定義了一個ObservableCollection 類型的變量_customerEntries。然后,在類構(gòu)造器中我們建立了一個標(biāo)準(zhǔn)的SQL的SELECT命令的字符串。接下來,通過調(diào)用定義于全局App類中DBHelper類相關(guān)實(shí)例中的方法SelectObservableCollection,實(shí)現(xiàn)把所有客戶的數(shù)據(jù)提取到變量_customerEntries中。最后,通過迭代一個結(jié)構(gòu)集合,成功地實(shí)現(xiàn)了在屏幕上顯示所有客戶數(shù)據(jù)之目的。
接下來,再看看具體的相關(guān)編碼吧。
1. 添加記錄
現(xiàn)在,讓我們看看是如何把五個樣本客戶數(shù)據(jù)添加到客戶表中的。
清單4:
- private void btnAdd_Click(object sender, RoutedEventArgs e)
- {
- DateTime start = DateTime.Now;
- int rec;
- Random rnd = new Random();
- string strInsert = " Insert into Customer (Name,Email,Desc) values (@Name,@Email,@Desc)";
- for (int i = 0; i < 5; i++)
- {
- Customer tst = new Customer
- {
- Name = "Name " + i,
- Email = Name + "@" + "aaa.com",
- Desc = "Desc for " + i
- };
- rec = (Application.Current as App).db.Insert < Customer>(tst,strInsert);
- }
- System.Diagnostics.Debug.WriteLine("\nInserted 5 " + " rows\r\nGenerated in " + (DateTime.Now - start).TotalSeconds);
- }
2. 刪除記錄
刪除操作更簡單,如清單5所示。
清單5:
- private void btnDel_Click(object sender, RoutedEventArgs e)
- {
- DateTime start = DateTime.Now;
- string strDel = " Delete from Customer where ID="+ "(SELECT COUNT(*) FROM Customer)" ;
- (Application.Current as App).db.Delete(strDel);
- }
在上面的代碼,客戶表中的最后一個記錄將被刪除。顯然,要編寫復(fù)雜和全功能的SQL語句,你應(yīng)該先熟悉SQLite數(shù)據(jù)庫相關(guān)的語法。事實(shí)上,這并不是一件難事,因?yàn)橛嘘P(guān)于SQLite的教程在網(wǎng)站可以輕松搞到(例如這里http://www.sqlite.org/lang.html)。
二、總結(jié)
在本系列文章中,我向您介紹了一款強(qiáng)大且易于使用的支持Windows Phone 7開發(fā)的開源嵌入數(shù)據(jù)庫系統(tǒng)-Sqlite Client for Windows Phone。正如您所看到的,在文中,我給出的僅僅是有關(guān)Sqlite Client for Windows Phone的入門性使用方法。但是,也看到了基于傳統(tǒng)型SQL數(shù)據(jù)庫操作知識,你可以輕松掌握Sqlite Client for Windows Phone的使用。盡管如此,有關(guān)這款軟件的真正實(shí)用性的技巧性的東西,還有待讀者您的進(jìn)一步挖掘。
【編輯推薦】
- 微軟WP7本地數(shù)據(jù)庫之Sterling編程技巧
- WP7開發(fā)中的數(shù)據(jù)庫系統(tǒng)選擇
- 微軟WP7本機(jī)數(shù)據(jù)庫解決方案之SQLite
- 為您介紹幾款開源的數(shù)據(jù)挖掘工具
- 告訴你如何解決MySQL server has gone away問題