自從上篇隨筆《厚積薄發(fā),豐富的公用類庫積累,助你高效進行系統開發(fā)(1)》一文以來,得到同行很多人的鼎力支持和關注,并且在大家的推動下,這篇文章也是上榜博客頭條、編輯推薦、10天內推薦排行等榮譽,很多人對這些類庫很是感興趣,也希望進一步詳細介紹相關類庫的使用。本隨筆系列將逐步介紹相關的類庫的詳細使用,并逐步整理成CHM的幫助文檔,作為類庫使用的指引手冊,同時我會對類庫進行進一步的豐富、提煉和優(yōu)化,隨筆將逐步發(fā)送,感謝大家的支持和鼓勵。
1、程序配置管理輔助類 AppConfig **
實現效果
1、 本輔助類主要是用來方便獲取或設置系統配置項的輔助類,實現快速讀寫配置文件的內容,可以用于讀取.exe.Config文件或者Web.config的文件內容,或者可以讀取指定文件的配置項。
2、 輔助類默認從當前目錄中按順序檢索Web.Config和.exe.Config文件。如果找到一個,則使用它作為默認的配置文件,不需要指定文件路徑。
3、 讀取的文件格式是一般的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="深田之星倉庫管理系統"/>
<!--開發(fā)商名稱-->
<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、本輔助類主要是用來方便實現標準統一的消息提示對話框,由于各種按鈕的樣式、標題都進行了統一分裝,因此調用的時候,這些參數都可以不用考慮,省卻很多繁瑣的參數指定,是Winform開發(fā)中非常常用的一個類庫輔助類。

實現代碼
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;
一般節(jié)日會提示相關的節(jié)日信息,另外可以通過修改XML文件,實現對節(jié)日、時間提示等信息調整。
<?xml version="1.0" encoding="gb2312" ?>
<HELLO>
<!-- 公歷節(jié)日開始 -->
<AD>
<feast day="0101" name="元旦" sayhello="yes">
<hello>新年好!祝您在新的一年里身體健康,事業(yè)進步!</hello>
<!-- 從網站根目錄算起 -->
<img>./img/theme/0101.gif</img>
</feast>
<feast day="0202" name="世界濕地日" sayhello="no">
<img></img>
<hello></hello>
</feast>
<feast day="0210" name="國際氣象節(jié)" sayhello="no">
<img></img>
<hello></hello>
</feast>
<feast day="0214" name="情人節(jié)" 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、 本輔助類主要是用來方便動態(tài)設置托盤圖標。該輔助類用于一些實時連接要求或者狀態(tài)變化能夠及時通過圖表來顯示的程序,通過閃動的圖標及文本,可以有效提示用戶相關的程序狀態(tài),提供用戶的使用體驗。
2、 動態(tài)設置托盤圖標,其效果如下所示


** 實現步驟**

2、 在代碼引用相關的代碼實現動態(tài)調用。
** 實現代碼**
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>
/// 設置托盤圖標的狀態(tài)
/// </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、 提供的操作,包括有創(chuàng)建表、DataTable和實體類集合IList<T>相互轉化、表排序、表過濾等操作,以求達到快速進行DataTable操作的目的。
實現代碼
1、 根據字段創(chuàng)建表對象,多個列用逗號(,)分開,默認為表對象的列為string。
string columns = @"流水號,備注,供貨商,操作員,庫房名稱,備件編號(pm碼),備件名稱,圖號,規(guī)格型號,材質,備件屬類,備件類別,
單位,最新單價(元),入庫數量,總價,入庫日期,來源,庫位,部門,使用位置";
DataTable dt = DataTableHelper.CreateTable(columns);
2、根據字段創(chuàng)建表對象,多個列用逗號(,)分開。如需要制定列的類型,在字段后加上“|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(where, this.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(where, this.winGridViewPager1.PagerInfo);
DataTable dt = DataTableHelper.ToDataTable<CustomerInfo>(list);
DataTable dtSort = DataTableHelper.SortedTable(dt, new string[]{"Number asc", "Type desc"});