Chinaunix首页 | 论坛 | 博客
  • 博客访问: 2538731
  • 博文数量: 308
  • 博客积分: 5547
  • 博客等级: 大校
  • 技术积分: 3782
  • 用 户 组: 普通用户
  • 注册时间: 2009-11-24 09:47
个人简介

hello world.

文章分类

全部博文(308)

分类: 嵌入式

2012-07-27 10:42:47

    最近在做wp7手机手机程序的开发,那么数据库的操作,我们不可以避免。其实wp7对sqlite数据库的支持不太好(我折腾将近一周,还是不行【个人意见,仅供参考】),后来没有办法,在网上看资料,说wp7对数据库的操作,还是使用linq比较的好,本人对linq没有太多了解,没办法,只有baidu,google。经过自己的摸索,终于将添加,修改,删除,查询弄好了(刚开始,只能进行添加,查询操作)。
    我因为要进行系统配置的存储,因此设计的数据表名称为SysConfig,表中有三个字段,分别为ID,Key, Value。很是简单吧。按照三层架构的模型,我设计了SysConfig,SysConfigDao,SysConfigService。三个类,代码如下:

点击(此处)折叠或打开

  1. using System;
  2. using System.Net;
  3. using System.Windows;
  4. using System.Windows.Controls;
  5. using System.Windows.Documents;
  6. using System.Windows.Ink;
  7. using System.Windows.Input;
  8. using System.Windows.Media;
  9. using System.Windows.Media.Animation;
  10. using System.Windows.Shapes;

  11. using System.Data.Linq;
  12. using System.Data.Linq.Mapping;
  13. using System.ComponentModel;
  14. using System.Collections;
  15. using System.Collections.Generic;
  16. namespace SheenClient.classes.dbmanager
  17. {
  18.     ///
  19.     /// 系统配置数据上下文类
  20.     ///
  21.     public class SysConfigDataContext : DataContext
  22.     {
  23.         public const string ConnectionStr = "Data Source=isostore:/sysconfig.sdf";
  24.         public Table<SysConfig> rows;
  25.         ///
  26.         /// 默认构造函数
  27.         ///
  28.         public SysConfigDataContext() : base(ConnectionStr)
  29.         {
  30.            
  31.         }

  32.     }

  33.     ///
  34.     /// 系统配置类
  35.     ///
  36.     [Table]
  37.     public class SysConfig : INotifyPropertyChanged, INotifyPropertyChanging
  38.     {
  39.         #region 私有成员

  40.         private int _id;
  41.         private string _key;
  42.         private string _value;

  43.         #endregion

  44.         [Column(IsPrimaryKey = true, CanBeNull = false, IsDbGenerated = true, DbType="INT NOT NULL Identity", AutoSync = AutoSync.OnInsert)]
  45.         public int ID
  46.         {
  47.             get { return _id; }
  48.             set
  49.             {
  50.                 if (_id != value)
  51.                 {
  52.                     NotifyPropertyChanging("ID");
  53.                     _id = value;
  54.                     NotifyPropertyChanged("ID");
  55.                 }
  56.             }
  57.         }

  58.         [Column]
  59.         public string Key
  60.         {
  61.             get { return _key; }
  62.             set
  63.             {
  64.                 NotifyPropertyChanging("Key");
  65.                 _key = value;
  66.                 NotifyPropertyChanged("Key");
  67.             }
  68.         }

  69.         [Column]
  70.         public string Value
  71.         {
  72.             get { return _value; }
  73.             set
  74.             {
  75.                 NotifyPropertyChanging("Value");
  76.                 _value = value;
  77.                 NotifyPropertyChanged("Value");
  78.             }
  79.         }

  80.         public event PropertyChangedEventHandler PropertyChanged;
  81.         private void NotifyPropertyChanged(string propertyName)
  82.         {
  83.             if (PropertyChanged != null)
  84.             {
  85.                 PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
  86.             }
  87.         }

  88.         public event PropertyChangingEventHandler PropertyChanging;
  89.         private void NotifyPropertyChanging(string propertyName)
  90.         {
  91.             if (PropertyChanging != null)
  92.             {
  93.                 PropertyChanging(this, new PropertyChangingEventArgs(propertyName));
  94.             }
  95.         }
  96.     }

  97.     ///
  98.     /// 系统配置数据访问层类
  99.     ///
  100.     public class SysConfigDao
  101.     {
  102.         #region 私有成员

  103.         private SysConfigDataContext _db;

  104.         #endregion

  105.         ///
  106.         /// 默认构造函数
  107.         ///
  108.         public SysConfigDao()
  109.         {
  110.             _db = new SysConfigDataContext();
  111.             if(!_db.DatabaseExists())
  112.                 _db.CreateDatabase();
  113.         }

  114.         ///
  115.         /// 添加系统配置
  116.         ///
  117.         /// 配置的对象
  118.         /// 添加成功:true;添加失败:false
  119.         public bool addSysConfig(SysConfig config)
  120.         {
  121.             try
  122.             {
  123.                 if (_db.DatabaseExists())
  124.                 {
  125.                     _db.rows.InsertOnSubmit(config);
  126.                     _db.SubmitChanges();
  127.                 }
  128.             }
  129.             catch
  130.             {
  131.                 return false;
  132.             }
  133.             return true;
  134.         }

  135.         ///
  136.         /// 获取系统配置
  137.         ///
  138.         ///
  139.         /// 存在返回:SysConfig对象;不存在返回null
  140.         public SysConfig getSysConfig(string key)
  141.         {
  142.             SysConfig result = null;
  143.             if (_db.DatabaseExists())
  144.             {
  145.                 IEnumerator<SysConfig> enumerator = _db.rows.GetEnumerator();
  146.                 while (enumerator.MoveNext())
  147.                 {
  148.                     if (enumerator.Current.Key == key)
  149.                     {
  150.                         result = new SysConfig();
  151.                         result.ID = enumerator.Current.ID;
  152.                         result.Key = enumerator.Current.Key;
  153.                         result.Value = enumerator.Current.Value;
  154.                         break;
  155.                     }
  156.                 }
  157.             }
  158.             return result;
  159.         }

  160.         ///
  161.         /// 是否含有系统配置项
  162.         ///
  163.         ///
  164.         /// 存在:true;不存在:false
  165.         public bool isSysConfig(string key)
  166.         {
  167.             SysConfig config = getSysConfig(key);
  168.             if (config == null)
  169.                 return false;
  170.             else
  171.                 return true;
  172.         }

  173.         ///
  174.         /// 删除系统配置
  175.         ///
  176.         /// 要删除的键
  177.         /// 删除成功:true;删除失败:false
  178.         public bool deleteSysConfig(string key)
  179.         {
  180.             bool result = false;
  181.             if (_db.DatabaseExists())
  182.             {
  183.                 IEnumerator<SysConfig> enumerator = _db.rows.GetEnumerator();
  184.                 while (enumerator.MoveNext())
  185.                 {
  186.                     if (enumerator.Current.Key == key)
  187.                     {
  188.                         _db.rows.DeleteOnSubmit(enumerator.Current);
  189.                         _db.SubmitChanges();
  190.                         result = true;
  191.                         break;
  192.                     }
  193.                 }
  194.             }
  195.             return result;
  196.         }

  197.         ///
  198.         /// 更新系统配置信息
  199.         ///
  200.         /// 系统配置对象
  201.         ///
  202.         public bool updateSysConfig(SysConfig config)
  203.         {
  204.             bool result = false;
  205.             if (_db.DatabaseExists())
  206.             {
  207.                 IEnumerator<SysConfig> enumerator = _db.rows.GetEnumerator();
  208.                 while (enumerator.MoveNext())
  209.                 {
  210.                     if (enumerator.Current.Key == config.Key)
  211.                     {
  212.                         enumerator.Current.Value = config.Value;
  213.                         _db.SubmitChanges();
  214.                         result = true;
  215.                         break;
  216.                     }
  217.                 }
  218.             }
  219.             return result;
  220.         }
  221.     }

  222. }
上面的代码,是最基本的操作,下面的代码,为SysConfigService服务类,实现对SysConfigDao类的调用,代码如下:

点击(此处)折叠或打开

  1. using System;
  2. using System.Net;
  3. using System.Windows;
  4. using System.Windows.Controls;
  5. using System.Windows.Documents;
  6. using System.Windows.Ink;
  7. using System.Windows.Input;
  8. using System.Windows.Media;
  9. using System.Windows.Media.Animation;
  10. using System.Windows.Shapes;

  11. using SheenClient.classes.dbmanager;
  12. namespace SheenClient.classes.dbmanager.service
  13. {
  14.     ///
  15.     /// 系统配置服务类
  16.     ///
  17.     ///
  18.     /// 使用SysConfigStorageSetting替换SysConfigDao,
  19.     /// 因对sqlite在wp7的操作还不理解,因此采用SysConfigStorageSetting
  20.     ///
  21.     public class SysConfigService
  22.     {
  23.         #region 私有成员

  24.         SysConfigDao _dao = null;
  25.         //SysConfigStorageSetting _setting = null;
  26.         #endregion

  27.         ///
  28.         /// 默认构造函数
  29.         ///
  30.         public SysConfigService()
  31.         {
  32.             _dao = new SysConfigDao();
  33.             //_setting = new SysConfigStorageSetting();
  34.             
  35.         }

  36.         ///
  37.         /// 保存配置信息
  38.         ///
  39.         /// 配置的键
  40.         /// 键对应的值
  41.         public void save(string key, string value)
  42.         {
  43.             SysConfig config = new SysConfig();
  44.             config.Key = key;
  45.             config.Value = value;
  46.             if (_dao.isSysConfig(key))
  47.                 _dao.updateSysConfig(config);
  48.             else
  49.                 _dao.addSysConfig(config);

  50.             //if (_setting.hasSysConfig(key))
  51.             // _setting.updateSysConfig(key, value);
  52.             //else
  53.             // _setting.addSysConfig(key, value);

  54.         }

  55.         ///
  56.         /// 获取系统配置值
  57.         ///
  58.         /// 要查找的键
  59.         /// 键对应的值
  60.         public string getSysConfig(string key)
  61.         {
  62.             string result = "";
  63.             if (_dao.isSysConfig(key))
  64.                 result = _dao.getSysConfig(key).Value;
  65.             //if (_setting.hasSysConfig(key))
  66.             // result = _setting.getSysConfig(key);

  67.             return result;
  68.         }
  69.     }
  70. }

阅读(2947) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~