自拍偷在线精品自拍偷,亚洲欧美中文日韩v在线观看不卡

微軟WP7本地數(shù)據(jù)庫之SQLite編程技巧

運(yùn)維 數(shù)據(jù)庫運(yùn)維 其他數(shù)據(jù)庫
在本篇文章中,我們將著手構(gòu)建一個簡單的Windows Phone 7客戶端應(yīng)用程序,并將具體探討Sqlite Client for Windows Phone在WP7開發(fā)中的基本應(yīng)用思路和相關(guān)編程技巧。

在上一篇《微軟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)境:

  1. Windows 7;
  2. .NET 4.0;
  3. Visual Studio 2010;
  4. Windows Phone Developer Tools RTW;
  5. Sqlite Client for Windows Phone (http://sqlitewindowsphone.codeplex.com/);
  6. (推薦) sqlite-manager (http://code.google.com/p/sqlite-manager/);
  7. (推薦) 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ù)庫訪問

  1. public partial class App : Application  
  2. {  
  3.    private DBHelper _db;  
  4.    public DBHelper db  
  5.    {  
  6.       get 
  7.       {  
  8.          Assembly assem = Assembly.GetExecutingAssembly();  
  9.          if (_db == null)  
  10.             _db = new DBHelper(assem.FullName.Substring(0, assem.FullName.IndexOf(',')), "database1.sqlite");  
  11.          return _db;  
  12.       }  
  13.    }  
  14. // ...其他省略 

注意到,公共屬性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:

  1. //其他省略...  
  2. using WP7SQLiteClient.Dal;  
  3. using System.Collections.ObjectModel;//ObservableCollection  
  4. using System.ComponentModel;  
  5. using SQLiteClient;  
  6. using Community.CsharpSqlite;  
  7. using System.Collections;  
  8. namespace WP7SQLiteClient  
  9. {  
  10.    public partial class TestDataEditor : PhoneApplicationPage  
  11.    {  
  12.       ObservableCollection _customerEntries = null;  
  13.       public TestDataEditor()  
  14.       {  
  15.          InitializeComponent();  
  16.          //retrieve dat  
  17.          string strSelect = "SELECT ID,Name,Email,Desc FROM Customer ORDER BY ID ASC";  
  18.          _customerEntries = (Application.Current as App).db.SelectObservableCollection(strSelect);  
  19.          foreach (Customer data in _customerEntries)  
  20.          {  
  21.             TextBlockID.Text += data.ID + Environment.NewLine;  
  22.             TextBlockName.Text +=data.Name + Environment.NewLine;  
  23.             TextBlockEmail.Text +=data.Email + Environment.NewLine;  
  24.             TextBlockDesc.Text +=data.Desc + Environment.NewLine;  
  25.          }  
  26.       }  
  27. //其他省略... 

在上面的代碼中,我們首先定義了一個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:

  1. private void btnAdd_Click(object sender, RoutedEventArgs e)  
  2. {  
  3.    DateTime start = DateTime.Now;  
  4.    int rec;  
  5.    Random rnd = new Random();  
  6.    string strInsert = " Insert into Customer (Name,Email,Desc) values (@Name,@Email,@Desc)";  
  7.    for (int i = 0; i < 5; i++)  
  8.    {  
  9.       Customer tst = new Customer  
  10.       {  
  11.          Name = "Name " + i,  
  12.          Email = Name + "@" + "aaa.com",  
  13.          Desc = "Desc for " + i  
  14.       };  
  15.       rec = (Application.Current as App).db.Insert < Customer>(tst,strInsert);  
  16.    }  
  17.    System.Diagnostics.Debug.WriteLine("\nInserted 5 " + " rows\r\nGenerated in " + (DateTime.Now - start).TotalSeconds);  

2. 刪除記錄

刪除操作更簡單,如清單5所示。

清單5:

  1. private void btnDel_Click(object sender, RoutedEventArgs e)  
  2. {  
  3.    DateTime start = DateTime.Now;  
  4.    string strDel = " Delete from Customer where ID=""(SELECT COUNT(*) FROM Customer)" ;  
  5.    (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)一步挖掘。


 

【編輯推薦】

  1. 微軟WP7本地數(shù)據(jù)庫之Sterling編程技巧
  2. WP7開發(fā)中的數(shù)據(jù)庫系統(tǒng)選擇
  3. 微軟WP7本機(jī)數(shù)據(jù)庫解決方案之SQLite
  4. 為您介紹幾款開源的數(shù)據(jù)挖掘工具
  5. 告訴你如何解決MySQL server has gone away問題

 

責(zé)任編輯:艾婧 來源: it168
相關(guān)推薦

2011-05-12 10:25:14

WP7數(shù)據(jù)庫Sterling

2011-05-18 09:30:16

SQLite

2011-02-28 10:42:14

Windows Pho微軟

2011-05-12 13:03:36

WP7數(shù)據(jù)庫選擇

2011-03-29 13:03:59

IronRubyWindows Pho.NET

2010-09-03 08:57:26

本地數(shù)據(jù)庫

2011-04-27 09:58:56

Windows PhoLBS微軟

2012-03-04 20:55:33

WP7

2011-08-19 09:09:01

AndroidWP7Windows Pho

2011-05-10 08:53:46

iOSWindows Pho開發(fā)者

2011-06-10 09:03:36

AndroidWindows Pho開發(fā)者

2012-07-06 09:26:13

Windows PhoWindows Pho

2011-06-15 10:18:12

Windows PhoPerst

2012-01-01 19:33:19

2013-06-17 14:10:08

WP7開發(fā)Windows Pho豆瓣電臺

2011-09-22 14:20:10

雷軍小米WP7

2013-06-17 13:47:41

WP7開發(fā)Windows Pho文本框水印控件

2011-07-28 09:26:18

MangoWindows Pho富士通

2011-03-08 10:26:45

Windows Pho諾基亞Qt

2011-02-15 09:50:02

Windows PhoKinectMWC
點(diǎn)贊
收藏

51CTO技術(shù)棧公眾號