C# 利用委托和事件 传入一个参数进行进行计算并返回结果
2024-10-20 07:38:29
一、委托定义
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; }
最新文章
- IOS 入门开发教程
- oc-14-对象方法调用类方法
- .NET中的三种Timer的区别和用法
- Javascript学习笔记(一)
- codevs 1725 探险 (二分)
- java(try块语句变量,和匿名类变量生存时间
- css 梯形标签页
- 用友CDM系统“货位间商品移库单(一步)”表体增加“货位可用数量”字段,根据表头的选择的货位自动带出数值
- nvarchar 和varchar区别
- Maven工程下报错:The superclass ";javax.servlet.http.HttpServlet"; was not found on the Java Build Path
- 展开被 SpringBoot 玩的日子 《 五 》 spring data jpa 的使用
- POST Raw JSON提交
- java.lang.IllegalArgumentException: No enum constant org.apache.ws.commons.schema.XmlSchemaForm.
- pytorch visdom可视化工具学习—1—详细使用-3-Generic Plots和Others
- Mysql逻辑分层、存储引擎
- linux环境下执行RF测试脚本
- elasticsearch 自定义_id
- docker 下 mysql 集群的搭建
- 理解SVG的图形填充规则
- EC断言16种判断