編寫Helper類的目的主要是簡化在C# .NET環境下的編程。尤其是簡化在Windows Forms中的Combox控件、DataGridView控件、以及DataTable對象與數據庫進行交互的目的。
使用下面的Helper類的前提是要引用
using JCFXBL.SrvInterface;
using JCFXBL.SrvInterface.Core.ProtocolLayer;
對應的程序集如下:
源代碼如下:
[csharp]
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;
}
}
}
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;
}
}
}