一、委托定义
1: public class TestData
2: {
3: //定义委托
4: public delegate void Get_TestDataEventHandler(Get_TestDataEventArgs e);
5: //定义事件
6: public event Get_TestDataEventHandler Get_TestData;
7: //定义数据类型 传递和获取 Observer所感兴趣的信息
8: //继承EventArgs类
9: public class Get_TestDataEventArgs : EventArgs
10: {
11: //传递两个参数 strWhere strIN
12: //获取一个数据 dataset 类型 执行查询后返回的数据集
13: public readonly string strWhere,strIN;
14: private DataSet ds;
15:
16: /// <summary>
17: /// 参数类型初始化
18: /// </summary>
19: /// <param name="strWhere">数据库查询语句 WHERE </param>
20: /// <param name="strIN">数据库查询语句 IN </param>
21: public Get_TestDataEventArgs(string strWhere,string strIN)
22: {
23: this.strWhere = strWhere;
24: this.strIN = strIN;
25: }
26: // 设置方法执行完毕后的Dataset类型的数据
27: public DataSet Data
28: {
29: set { ds = value; }
30: get { return ds; }
31: }
32: }
33: private DataSet _testdata;
34:
35: /// <summary>
36: /// 返回数据 DataSet
37: /// </summary>
38: public DataSet Testdata
39: {
40: get { return _testdata; }
41: }
42:
43: //判断事件是否有赋值
44: protected virtual void onGet_TestData(Get_TestDataEventArgs e)
45: {
46: if (Get_TestData != null)
47: {
48: Get_TestData(e);
49: _testdata = e.Data;
50: }
51: }
52: /// <summary>
53: /// 开始获取数据
54: /// </summary>
55: /// <param name="e"></param>
56: public void Start(Get_TestDataEventArgs e)
57: {
58: onGet_TestData(e);
59: }
60:
61: }
二、调用委托方法定义
/// <summary> /// 多芯查询 有IN条件 /// </summary> /// <param name="e"></param> public void Getdata_Method_Multicore(TestData.Get_TestDataEventArgs e) { string sql = "SELECT * "; sql += " WHERE (Name IN ("+e.strIN+"))"; e.Data = dbs.Query(sql); } /// <summary> /// 双并查询 有IN条件 /// </summary> /// <param name="e"></param> public void Getdata_Method_TwainCore(TestData.Get_TestDataEventArgs e) { string sql = " SELECT * "; sql += " WHERE "+e.strWhere+" AND (Result LIKE N'%PASS%') AND (Name IN ("+e.strIN+"))"; e.Data = dbs.Query(sql); } /// <summary> /// 单芯查询 无 IN条件 /// </summary> /// <param name="e"></param> public void GetData_Method_OneCore(TestData.Get_TestDataEventArgs e) { string sql = " SELECT * "; sql += " WHERE " + e.strWhere + " AND (Result LIKE N'%PASS%') "; e.Data = dbs.Query(sql); }
三、应用
TestData _TestData = new TestData();
_TestData.Get_TestData += _Method_User_3D.GetData_Method_OneCore; //3D 数据查询方法设置
string strWhere = "Where 语句"; //Where 语句 string strIN = "IN 语句"; //IN 语句 为空 Maticsoft.DAL.TestData.Get_TestDataEventArgs e = new DAL.TestData.Get_TestDataEventArgs(strWhere, strIN); //委托数据类型初始化 _TestData.Start(e); //开始查询 DataSet ds = _TestData.Testdata; //查询到的数据 if (ds.Tables[0].Rows.Count > 0) //如果有记录 则表示为良品 { _result_3D = true; } else { _result_3D = false; }