日韩无码专区无码一级三级片|91人人爱网站中日韩无码电影|厨房大战丰满熟妇|AV高清无码在线免费观看|另类AV日韩少妇熟女|中文日本大黄一级黄色片|色情在线视频免费|亚洲成人特黄a片|黄片wwwav色图欧美|欧亚乱色一区二区三区

RELATEED CONSULTING
相關(guān)咨詢
選擇下列產(chǎn)品馬上在線溝通
服務(wù)時(shí)間:8:30-17:00
你可能遇到了下面的問題
關(guān)閉右側(cè)工具欄

新聞中心

這里有您想知道的互聯(lián)網(wǎng)營(yíng)銷解決方案
WindowsPhone7Perst嵌入式數(shù)據(jù)庫(kù)的學(xué)習(xí)

WP7只有本地存儲(chǔ),自身是不帶數(shù)據(jù)庫(kù)存儲(chǔ)的,所以想要在WP7上使用數(shù)據(jù)庫(kù)只能通過使用第三方的嵌入式數(shù)據(jù)庫(kù)。Perst 是一個(gè)簡(jiǎn)單,快速,便捷,面向?qū)ο筮m合Java與.NET的數(shù)據(jù)庫(kù),它在嵌入式數(shù)據(jù)庫(kù)領(lǐng)域是鼎鼎有名的,并且其代碼是開源的,我們可以在它的官方網(wǎng)站上下載該數(shù)據(jù)庫(kù)的所有的代碼。

官方網(wǎng)站www.mcobject.com/perst_eval

下面是Perst數(shù)據(jù)庫(kù)在Windows Phone 7上使用的一些基本的語法的簡(jiǎn)單總結(jié):

1、創(chuàng)建數(shù)據(jù)庫(kù)

 
 
 
  1. Storage storage = StorageFactory.Instance.CreateStorage(); //創(chuàng)建Perst存儲(chǔ)Storage實(shí)例  
  2. storage.Open( "PerstDemoDB.dbs"); // 打開Storage  
  3. Database DB = new Database(storage); //使用上面初始化的Storage實(shí)例創(chuàng)建數(shù)據(jù)庫(kù) 

2、創(chuàng)建數(shù)據(jù)庫(kù)面向?qū)ο蟮念?作用相當(dāng)于關(guān)系數(shù)據(jù)庫(kù)的表)

以下是代碼片段:

 
 
 
  1. //創(chuàng)建一個(gè)數(shù)據(jù)庫(kù)存儲(chǔ)的面向?qū)ο蟮念惖幕?nbsp;要繼承Perst.Persistent基類  
  2. public class User: Perst.Persistent  
  3. {  
  4.     //定義字段  
  5.     //Perst使用反射來獲取對(duì)象的值 需要在字段的前面加上[FullTextIndexable]標(biāo)示  
  6.     [FullTextIndexable]  
  7.     public long id;  
  8.     [FullTextIndexable]  
  9.     public string name;  
  10.     ……  
  11.      public long Id  
  12.     {  
  13.         get { return id; }  
  14.         set { id= value; }  
  15.     }  
  16.     ……  
  17.      public User(long Id, string name)  
  18.     {  
  19.         id = id;  
  20.         name = name;  
  21.     }  
  22.     public override void OnLoad()  
  23.     {  
  24.         base.OnLoad();  
  25.     }  
  26.     //獲取數(shù)據(jù)庫(kù)對(duì)象 一般會(huì)將數(shù)據(jù)庫(kù)定義在App里面  
  27.     protected static Database DB  
  28.     {  
  29.         get { return ((App)Application.Current).DB; }  
  30.     }  
  31.     public override void Deallocate()  
  32.     {  
  33.         DB.DeleteRecord(this);//刪除記錄  
  34.     }  
  35.     public void Save()  
  36.     {  
  37.         Store();//保存 相當(dāng)于保存表  
  38.          DB.UpdateFullTextIndex(this);  
  39.     }  

3、添加記錄

以下是代碼片段:

 
 
 
  1. User user= new User(1, "名字");  
  2. DB.AddRecord(user);  
  3. DB.Storage.Commit(); 

4、修改記錄

以下是代碼片段:

 
 
 
  1. user.Id=2 
  2. user.Save(); 

5、刪除記錄

以下是代碼片段:

 
 
 
  1. user.Deallocate();  
  2. DB.Storage.Commit(); 

6、查詢數(shù)據(jù)庫(kù)

根據(jù)唯一的oid查詢記錄 oid是Perst數(shù)據(jù)庫(kù)為每一個(gè)類的對(duì)象分配的一個(gè)唯一的值

 
 
 
  1. User user= DB.Select("oid = " + this.NavigationContext.QueryString["oid"]).FirstOrDefault(); 

模糊查詢

以下是代碼片段:

 
 
 
  1. // 查詢所有包含了tbSearch.Text.ToLower()的結(jié)果FullTextSearchResult  
  2. FullTextSearchResult prefixes = DB.SearchPrefix(tbSearch.Text.ToLower(), 1000, 4000, false);  
  3. ObservableCollection searchUsers = new ObservableCollection();  
  4. List arrayRes = new List();  
  5. if (prefixes != null) arrayRes.AddRange(prefixes.Hits);  
  6. foreach (var hit in arrayRes)  
  7. {  
  8.     if (hit.Document is User)//如果是聯(lián)系人類型 FullTextSearchHit.Document 查詢匹配的文件  
  9.     {  
  10.         if (!searchcontacts.Contains((User)hit.Document))  
  11.         searchcontacts.Add((User)hit.Document);  
  12.     }  

7、刪除存儲(chǔ)的類的所有對(duì)象

以下是代碼片段:

 
 
 
  1. DB.DropTable(typeof(User));  
  2. DB.Storage.Commit();//完成 

8、刪除數(shù)據(jù)庫(kù)

以下是代碼片段:

 
 
 
  1. var storage = ((App)App.Current).DB.Storage;//獲取在App上定義的數(shù)據(jù)庫(kù)存儲(chǔ)  
  2. storage.Close();//關(guān)閉它  
  3. using (var store = IsolatedStorageFile.GetUserStoreForApplication())//獲取當(dāng)前應(yīng)用程序使用的本地存儲(chǔ)文件  
  4. {  
  5.     if (store.FileExists("PerstDemoDB.dbs"))//找到數(shù)據(jù)庫(kù)的存儲(chǔ)文件 perst數(shù)據(jù)庫(kù)文件是在本地存儲(chǔ)中的  
  6.     {  
  7.         store.DeleteFile("PerstDemoDB.dbs");//刪除這個(gè)數(shù)據(jù)庫(kù)的本地存儲(chǔ)文件  
  8.     }  
  9. }  

【編輯推薦】

  1. 微軟WP7本地?cái)?shù)據(jù)庫(kù)之Sterling編程技巧
  2. WP7開發(fā)中的數(shù)據(jù)庫(kù)系統(tǒng)選擇
  3. 微軟WP7本機(jī)數(shù)據(jù)庫(kù)解決方案之SQLite
  4. 微軟WP7本地?cái)?shù)據(jù)庫(kù)之SQLite編程技巧
  5. Perst嵌入式數(shù)據(jù)庫(kù)介紹

網(wǎng)頁標(biāo)題:WindowsPhone7Perst嵌入式數(shù)據(jù)庫(kù)的學(xué)習(xí)
本文來源:http://www.5511xx.com/article/cdichjg.html