Perst是一个简单、快速、便捷的面向Java和.Net的数据库。它可以直接将.net对象存储,在Silverlight不需要web service的方式进行读写,而是直接读写。
本文将编写一个实例进行perst的操作,下载一个PerstSilverlight.dll,然后引用它。
首先:在App.xaml.cs中Application_Startup()中建立Perst数据库的实例,在Application.Exit()中关闭Perst数据库。
private void Application_Startup(object sender, StartupEventArgs e) { //应用程序开启时打开数据库 Storage instanse = StorageFactory.Instance.CreateStorage(); //初始化数据库文件为1M instanse.SetProperty("perst.file.extension.quantum", 1024 * 1024); //数据库大小不够时,每次增加512K instanse.SetProperty("perst.extension.quantum", 512 * 1024); instanse.Open("SLDemo.dbs"); db = new Database(instanse); this.RootVisual = new MainPage(); } private void Application_Exit(object sender, EventArgs e) { //应用程序关闭时关闭数据库连接 db.Storage.Close(); }
其次:建立一个实体类继承于Persistent,定义为索引的字段必须设置为Public。
/// <summary> /// 城市类 /// </summary> public class City : Persistent { //定义为索引 [FullTextIndexable] public string _CityName; /// <summary> /// 城市名称 /// </summary> public string CityName { get { return _CityName; } set { _CityName = value; } } [FullTextIndexable] public string _CityNum; /// <summary> /// 城市电话区号 /// </summary> public string CityNum { get { return _CityNum; } set { _CityNum = value; } } public override void OnLoad() { base.OnLoad(); } //删除 public override void Deallocate() { base.Deallocate(); } }
再次:实现前台界面的事件,并实现它。
public partial class MainPage : UserControl { public MainPage() { InitializeComponent(); } private void GetNum_1(object sender, RoutedEventArgs e) { BindCity(); } /// <summary> /// 绑定所有的城市,并且按照CityNum排序 /// </summary> private void BindCity() { List<City> list = App.db.Select<City>("order by CityNum").ToList(); this.dgrid.ItemsSource = list; } /// <summary> /// 查找到021号码的区号对象并且删除 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void DeleteNum_2(object sender, RoutedEventArgs e) { City city = App.db.Select<City>("CityNum='021'").FirstOrDefault(); city.Deallocate(); App.db.Storage.Commit(); BindCity(); } /// <summary> /// 查找到020区号的对象进行修改 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void EditNum_4(object sender, RoutedEventArgs e) { City city = App.db.Select<City>("CityNum='020'").FirstOrDefault(); city.CityName = "上海市"; city.Store(); App.db.UpdateFullTextIndex(city); BindCity(); } /// <summary> /// 添加数据 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void AddNum_3(object sender, RoutedEventArgs e) { App.db.AddRecord(new City() { CityName = "广州", CityNum = "021" }); App.db.AddRecord(new City() { CityName = "北京", CityNum = "010" }); App.db.AddRecord(new City() { CityName = "上海", CityNum = "020" }); App.db.AddRecord(new City() { CityName = "成都", CityNum = "028" }); App.db.Storage.Commit(); } }
最后我们来看前台界面Xaml如下:
<Grid x:Name="LayoutRoot" Background="White"> <sdk:DataGrid Name="dgrid" HorizontalAlignment="Left" Height="181" Margin="10,80,0,0" VerticalAlignment="Top" Width="338"/> <Button Content="增加数据" HorizontalAlignment="Left" Margin="42,10,0,0" VerticalAlignment="Top" Width="75" Click="AddNum_3"/> <Button Content="获取值" HorizontalAlignment="Left" Margin="160,10,0,0" VerticalAlignment="Top" Width="75" Click="GetNum_1"/> <Button Content="修改值" HorizontalAlignment="Left" Margin="42,48,0,0" VerticalAlignment="Top" Width="75" Click="EditNum_4"/> <Button Content="删除值" HorizontalAlignment="Left" Margin="160,48,0,0" VerticalAlignment="Top" Width="75" Click="DeleteNum_2"/> </Grid>
下面是效果图片,如需源码请点击 SLPerstDemo.rar 下载。