自从上篇随笔《厚积薄发,丰富的公用类库积累,助你高效进行系统开发(1)》一文以来,得到同行很多人的鼎力支持和关注,并且在大家的推动下,这篇文章也是上榜博客头条、编辑推荐、10天内推荐排行等荣誉,很多人对这些类库很是感兴趣,也希望进一步详细介绍相关类库的使用。本随笔系列将逐步介绍相关的类库的详细使用,并逐步整理成CHM的帮助文档,作为类库使用的指引手册,同时我会对类库进行进一步的丰富、提炼和优化,随笔将逐步发送,感谢大家的支持和鼓励。

1、程序配置管理辅助类 AppConfig 

实现效果

  1. 本辅助类主要是用来方便获取或设置系统配置项的辅助类,可以用于读取*.exe.Config文件或者Web.config的文件内容,或者可以读取指定文件的配置项。
    辅助类默认从当前目录中按顺序检索Web.Config和*.exe.Config文件。如果找到一个,则使用它作为默认的配置文件,不需要指定文件路径。

  2. 读取的文件格式是一般的XML配置文件格式,如下所示。 
    <?xml version="1.0" encoding="utf-8" ?>   
    <configuration>   
      
    <configSections>   
        
    <section name="dataConfiguration"   
                 type
    ="Microsoft.Practices.EnterpriseLibrary.Data.Configuration.DatabaseSettings, Microsoft.Practices.EnterpriseLibrary.Data"/>   
      
    </configSections>   
      
    <connectionStrings>   
        
    <add name="DataAccess" providerName="System.Data.SqlClient"   
             connectionString
    ="Persist Security Info=False;Data Source=(local);Initial Catalog=Warehouse;User ID=sa;Password=123456"/>   
      
    </connectionStrings>   
      
    <dataConfiguration defaultDatabase="DataAccess"/>   
       
      
    <appSettings>   
        
    <!--软件名称-->   
        
    <add key="ApplicationName" value="深田之星仓库管理系统"/>   
        
    <!--开发商名称-->   
        
    <add key="Manufacturer" value="广州爱启迪技术有限公司"/>   
        
    <!--字典、权限组件的数据库类型:access、sqlserver等,默认为sqlserver可不写-->   
        
    <add key="ComponentDbType" value="sqlserver"/>   
      
    </appSettings>   
    </configuration>  

实现代码

  1. 读取配置项目。
    AppConfig config = new AppConfig();    
    string Manufacturer = config.AppConfigGet("Manufacturer");    
    string ApplicationName = config.AppConfigGet("ApplicationName");    
    string AppWholeName = string.Format("{0}-{1}", Manufacturer, ApplicationName);  

  2. 读取复杂的连接字符串配置,如上面的EnterpriseLibrary的连接字符串 DataAccess 配置项目的ConnectString。

     

    /// <summary>    
    /// 测试数据库是否正常连接    
    /// </summary>    
    /// <returns></returns>    
    public static bool TestConnection(string connectionString)    
    {    
        
    bool result = false;    
       
        
    using (DbConnection connection = new SqlConnection(connectionString))    
        {    
            connection.Open();    
            
    if (connection.State == System.Data.ConnectionState.Open)    
            {    
                result 
    = true;    
            }    
        }    
       
        
    return result;    
    }    
       
    public static bool TestConnection()    
    {    
        AppConfig config 
    = new AppConfig();    
        
    return TestConnection(config.GetConnectionString("DataAccess"));    
    }  

     

  3. 写入配置项内容。
    AppConfig config = new AppConfig();    
    //保存地址(标准调用)    
    config.AppConfigSet("PictureRootDir"this.txtPath.Text);    
       
    //保存EnterpriseLibray数据库配置项(自定义设置)    
    string connectionString = string.Format("Provider=Microsoft.Jet.OLEDB.4.0;Data Source={0}Database1.mdb;User ID=Admin;Jet OLEDB:Database Password=;", System.AppDomain.CurrentDomain.BaseDirectory);    
    config.SetConnectionString(
    "DataAccess", connectionString);    
    //更新配置文件,以便起效    
    ConfigurationManager.RefreshSection("dataConfiguration");    
    ConfigurationManager.RefreshSection(
    "connectionStrings");   
    ConfigurationManager.RefreshSection("appSettings"); 

      

2、消息提示对话框辅助类 MessageUtil

 实现效果

   1、本辅助类主要是用来方便实现标准统一的消息提示对话框。

   2、封装的消息提示对话框包括个各种常用的对话框,如下所示:

 

实现代码

MessageUtil.ShowTips("提示信息对话框");    
MessageUtil.ShowWarning(
"警告消息提示框");    
MessageUtil.ShowError(
"错误消息提示框");    
   
MessageUtil.ShowYesNoAndTips(
"提示对话框,有Yes/No按钮");    
MessageUtil.ShowYesNoAndWarning(
"警告对话框,有Yes/No按钮");    
MessageUtil.ShowYesNoAndError(
"错误对话框,有Yes/No按钮");    
MessageUtil.ShowYesNoCancelAndTips(
"提示对话框,有Yes/No/Cancel按钮");    
   
MessageUtil.ConfirmYesNo(
"确认对话框,有Yes/No对话框");  
MessageUtil.ConfirmYesNoCancel("确认对话框,有Yes/No/Cancel对话框"); 

 

 3、日历类辅助类 CCalendar

  实现效果

  1. 本辅助类主要是用来方便显示日期时间、农历、生肖的日历类。
  2. 其效果如下所示 

 实现代码  

CCalendar cal = new CCalendar();  
this
.lblCalendar.Text = cal.GetDateInfo(System.DateTime.Now).Fullinfo;  

可以修改XML文件,实现对节日、时间提示等信息调整。 

<?xml version="1.0" encoding="gb2312" ?>   
<
HELLO>   
   
  
<!-- 公历节日开始 -->   
  
<AD>   
    
<feast day="0101" name="元旦" sayhello="yes">   
      
<hello>新年好!祝您在新的一年里身体健康,事业进步!</hello>   
      
<!-- 从网站根目录算起 -->   
      
<img>./img/theme/0101.gif</img>   
    
</feast>   
    
<feast day="0202" name="世界湿地日" sayhello="no">   
      
<img></img>   
      
<hello></hello>   
    
</feast>   
    
<feast day="0210" name="国际气象节" sayhello="no">   
      
<img></img>   
      
<hello></hello>   
    
</feast>   
    
<feast day="0214" name="情人节" sayhello="yes">   
      
<hello>祝天下有情人终成眷属!</hello>   
      
<img>./img/theme/0214.gif</img>   
    
</feast>   
    
<feast day="0301" name="世界图书日" sayhello="no">   
      
<img></img>   
      
<hello></hello>   
    
</feast>   
.............

 

4、托盘图标辅助类 NotifyIconHelper 

 实现效果

  1. 本辅助类主要是用来方便动态设置托盘图标。
  2. 动态设置托盘图标,其效果如下所示 



 实现步骤

  1. 在主窗体添加一个NotifyIcon组件,并设置好相关的属性,如下所示。



2、在代码引用相关的代码实现动态调用。 

实现代码

public partial class FrmMain : Form    
{    
    
private NotifyIconHelper notifyHelper;    
    
private const string ClientName = "数据采集终端";//PC式采集器终端    
   
    
public frmMain()    
    {    
        InitializeComponent();    
   
        
this.Text = ClientName;    
        
//初始化托盘图标    
        notifyHelper = new NotifyIconHelper(this.notifyIcon1);    
        notifyHelper.Icon_Conntected 
= Resources.Connected;    
        notifyHelper.Icon_Shrink1 
= Resources.shrink1;    
        notifyHelper.Icon_Shrink2 
= Resources.shrink2;    
        notifyHelper.Icon_UnConntect 
= Resources.unConnected;    
        notifyHelper.Text_Conntected 
= string.Format("{0}:终端已经连接", ClientName);    
        notifyHelper.Text_UnConntect 
= string.Format("{0}:终端未连接", ClientName);    
        notifyHelper.NotifyStatus 
= NotifyIconHelper.Status.TwinkleNotice;    
    }    
   
    
/// <summary>    
    
/// 设置托盘图标的状态    
    
/// </summary>    
    
/// <param name="status"></param>    
    public void SetNotifyStatus(NotifyIconHelper.Status status)    
    {                
        notifyHelper.NotifyStatus 
= status;    
   
        
if (status == NotifyIconHelper.Status.Offline)    
        {    
            
this.Invoke(new MethodInvoker(delegate()    
            {    
                
this.Icon = notifyHelper.Icon_UnConntect;    
            }));                    
        }    
        
else if (status == NotifyIconHelper.Status.Online)    
        {    
            
this.Invoke(new MethodInvoker(delegate()    
            {    
                
this.Icon = notifyHelper.Icon_Conntected;    
            }));       
        }    
    }  

 

5、DataTable操作辅助类 DataTableHelper 

实现效果

  1. 本辅助类主要是用来方便对DataTable进行相关操作的辅助类,该类是非常常用的工具类,常用与数据显示、转换和报表生成等操作中。
  2. 提供的操作,包括有创建表、DataTable和实体类集合IList<T>相互转化、表排序、表过滤等操作,以求达到快速进行DataTable操作的目的。  

实现代码

  1. 根据字段创建表对象,多个列用逗号(,)分开,默认为表对象的列为string。
    string columns = @"流水号,备注,供货商,操作员,库房名称,备件编号(pm码),备件名称,图号,规格型号,材质,备件属类,备件类别,
    单位,最新单价(元),入库数量,总价,入库日期,来源,库位,部门,使用位置
    ";    
    DataTable dt 
    = DataTableHelper.CreateTable(columns);   

  2. 根据字段创建表对象,多个列用逗号(,)分开。如需要制定列的类型,在字段后加上“|int”格式的字符。 
    string tableColumns = "ID|int,ItemNo,ItemName,StockQuantity|int,Manufacture,MapNo,Specification,Material,ItemBigType,ItemType,
    Unit,Price|decimal,Source,StoragePos,UsagePos,Note,WareHouse,Dept";    
    DataTable dt = DataTableHelper.CreateTable(tableColumns);  

  3. 实体类转DataTable对象操作ListToDataTable,其对应的反操作函数DataTableToList使用方法类似。
    string where = GetSearchSql();    
    IList
    <CustomerInfo> list = BLLFactory<Customer>.Instance.Find(wherethis.winGridViewPager1.PagerInfo);    
    DataTable dt 
    = DataTableHelper.ToDataTable<CustomerInfo>(list);    
    this.winGridViewPager1.DataSource = dt.DefaultView;    
    this.winGridViewPager1.dataGridView1.Refresh(); 
  4. DataTable对象排序操作SortedTable,可对表多列进行排序操作。
    string where = GetSearchSql();    
    IList
    <CustomerInfo> list = BLLFactory<Customer>.Instance.Find(wherethis.winGridViewPager1.PagerInfo);    
    DataTable dt 
    = DataTableHelper.ToDataTable<CustomerInfo>(list);    
    DataTable dtSort 
    = DataTableHelper.SortedTable(dt, new string[]{"Number asc""Type desc"});

     

由于篇幅原因,下篇继续整理发布出来,谢谢支持。

作者: 伍华聪 发表于 2011-07-04 22:05 原文链接

推荐.NET配套的通用数据层ORM框架:CYQ.Data 通用数据层框架