编写Helper类的目的主要是简化在C# .NET环境下的编程。尤其是简化在Windows Forms中的Combox控件、DataGridView控件、以及DataTable对象与数据库进行交互的目的。
使用下面的Helper类的前提是要引用
using JCFXBL.SrvInterface;
using JCFXBL.SrvInterface.Core.ProtocolLayer;
对应的程序集如下:
源代码如下:
using JCFXBL.SrvInterface; using JCFXBL.SrvInterface.Core.ProtocolLayer; using System; using System.Collections.Generic; using System.Data; using System.Linq; using System.Text; using System.Windows.Forms; namespace JCFXBL.API { public class Helper { public static string ServerUrl; /// <summary> /// 把DIC字典数据自动填充进combox控件 /// </summary> /// <param name="dic">字典名称</param> /// <param name="cmb">combox控件对象</param> public static void FillComboxByDic(string dic, ComboBox cmb) { BIRequest BIR = new BIRequest(); BIR.url = Helper.ServerUrl + "Execute.ashx?Serlet=GetDICStore&ACT=GET&dic=" + dic; if (BIR.Request()) { try { SrvResponse r = new SrvResponse(BIR.Response()); DataTable dt = r.GetResultTable(); cmb.DataSource = dt; cmb.DisplayMember = "value"; cmb.ValueMember = "key"; } catch (Exception ex) { // MessageBox.Show(ex.Message); } } } /// <summary> /// 把数据表中对应的数据字段填充进combox控件 /// </summary> /// <param name="cmb">combox控件对象</param> /// <param name="Parms">查询参数</param> /// <param name="act">存储过程名称</param> /// <param name="DisplayMember">combox显示字段</param> /// <param name="ValueMember">combox值字段</param> public static void FillComboBoxByTable(ComboBox cmb, System.Collections.ArrayList Parms, string act, string DisplayMember, string ValueMember) { BIRequest BIR = new BIRequest(); BIR.url = Helper.ServerUrl + "Execute.ashx?Serlet=GetDBStore&ACT=" + act; if (Parms != null) for (int i = 0; i < Parms.Count; i++) { PostParm p = (PostParm)Parms[i]; BIR.AddFormParam(p.ParmName, p.ParmValue); } if (BIR.Request()) { try { SrvResponse r = new SrvResponse(BIR.Response()); DataTable dt = r.GetResultTable(); cmb.DataSource = dt; cmb.DisplayMember = DisplayMember;// ""; cmb.ValueMember = ValueMember;// ""; } catch (Exception ex) { // MessageBox.Show(ex.Message); } } } /// <summary> /// 把数据库中的数据填充到DataGridView控件 /// </summary> /// <param name="dgv">DataGridView控件对象</param> /// <param name="act">存储过程名称</param> /// <param name="Parms">查询参数</param> public static void FillDataGridViewByTable(DataGridView dgv, string act, System.Collections.ArrayList Parms) { BIRequest BIR = new BIRequest(); BIR.url = JCFXBL.API.Helper.ServerUrl + "Execute.ashx?Serlet=GetDBStore&ACT="+act; if (Parms != null) for (int i = 0; i < Parms.Count; i++) { PostParm p = (PostParm)Parms[i]; BIR.AddFormParam(p.ParmName, p.ParmValue); } if (BIR.Request()) { SrvResponse r = new SrvResponse(BIR.Response()); if (r.return_value == "1") { DataTable dt = r.GetResultTable(); dgv.DataSource = dt; } else { MessageBox.Show(BIR.Response()); } } else { MessageBox.Show(BIR.Response()); } } /// <summary> /// 把从数据库查询到的数据填充到一个DataTable对象中 /// </summary> /// <param name="act">存储过程名称</param> /// <param name="Parms">查询参数</param> /// <returns></returns> public static DataTable FillDataTable(string act, System.Collections.ArrayList Parms) { BIRequest BIR = new BIRequest(); BIR.url = JCFXBL.API.Helper.ServerUrl + "Execute.ashx?Serlet=GetDBStore&ACT=" + act; if (Parms != null) for (int i = 0; i < Parms.Count; i++) { PostParm p = (PostParm)Parms[i]; BIR.AddFormParam(p.ParmName, p.ParmValue); } if (BIR.Request()) { SrvResponse r = new SrvResponse(BIR.Response()); if (r.return_value == "1") { DataTable dt = r.GetResultTable(); return dt; } else { MessageBox.Show(BIR.Response()); } } else { MessageBox.Show(BIR.Response()); } return null; } } }