列表查询组件代码, 简化拼接条件SQL语句的麻烦
控件代码及测试例子:https://files.cnblogs.com/wuhuacong/CommonSearch.rar
使用场景: 在列表页面中,一般有好几个条件, 用户进行查询时候,需要根据这几个条件进行过滤查询.但在组装这些过滤条件的时候,代码比较烦琐臃肿,本组件代码为解决该问题而设计。
使用目的: 1.减少对参数非空的条件判断 2. 可以构造出参数化的DbCommand对象,简化操作. 3.适当修改后可以用于其他数据访问的参数化参数生成.4.构造Sql语句或者参数化条件更加易读
1. 生成SQL条件语句
如有几个字段,需要根据不同的字段进行过滤,想生成的SQL语句如下:
Where (1=1) AND AA2 Like '%AA2Value%' AND AA6 >= 'Value6' AND AA7 <= 'value7' AND AA3 = 'Value3' AND AA4 < 'Value4' AND AA5 > 'Value5' AND AA <> '1'
那么代码如下:
SearchCondition search = new SearchCondition();
search.AddCondition("AA", 1, SqlOperator.NotEqual)
.AddCondition("AA2", "AA2Value", SqlOperator.Like)
.AddCondition("AA3", "Value3", SqlOperator.Equal)
.AddCondition("AA4", "Value4", SqlOperator.LessThan)
.AddCondition("AA5", "Value5", SqlOperator.MoreThan)
.AddCondition("AA6", "Value6", SqlOperator.MoreThanOrEqual)
.AddCondition("AA7", "value7", SqlOperator.LessThanOrEqual);
string conditionSql = search.BuildConditionSql();
search.AddCondition("AA", 1, SqlOperator.NotEqual)
.AddCondition("AA2", "AA2Value", SqlOperator.Like)
.AddCondition("AA3", "Value3", SqlOperator.Equal)
.AddCondition("AA4", "Value4", SqlOperator.LessThan)
.AddCondition("AA5", "Value5", SqlOperator.MoreThan)
.AddCondition("AA6", "Value6", SqlOperator.MoreThanOrEqual)
.AddCondition("AA7", "value7", SqlOperator.LessThanOrEqual);
string conditionSql = search.BuildConditionSql();
2. 生成基于Enterprise Library的DbCommand对象
Database db = DatabaseFactory.CreateDatabase();
SearchCondition search = new SearchCondition();
search.AddCondition("Name", "测试" , SqlOperator.Like)
.AddCondition("ID", 1, SqlOperator.MoreThanOrEqual);
DbCommand dbComand = search.BuildDbCommand(db, "select Comments from Test", " Order by Name");
using (IDataReader dr = db.ExecuteReader(dbComand))
{
while (dr.Read())
{
this.txtSql.Text += "\r\n" + dr["Comments"].ToString();
}
}
SearchCondition search = new SearchCondition();
search.AddCondition("Name", "测试" , SqlOperator.Like)
.AddCondition("ID", 1, SqlOperator.MoreThanOrEqual);
DbCommand dbComand = search.BuildDbCommand(db, "select Comments from Test", " Order by Name");
using (IDataReader dr = db.ExecuteReader(dbComand))
{
while (dr.Read())
{
this.txtSql.Text += "\r\n" + dr["Comments"].ToString();
}
}
下面是该控件的类对象图解
下面我们比较一下使用该控件和不使用在列表查询页面中的代码,可以看出使用了控件后的代码大大较少了,并且可读性也增强了
1. 使用该控件, 列表查询页面中的代码
private string GetCondition()
{
SearchCondition search = new SearchCondition();
search.AddCondition("GroupID", this.ddlUserGroup.SelectedValue, SqlOperator.Equal, true)//班组ID
.AddCondition("DealGroupName", this.ddlDealGroup.SelectedValue, SqlOperator.Equal, true)/*消缺单位*/
.AddCondition("VisioStationID", this.ddlStation.SelectedValue, SqlOperator.Like, true)//变电站
.AddCondition("VisioImageID", this.ddlLine.SelectedValue, SqlOperator.Like, true)/*馈线*/
.AddCondition("BugNo", this.txtBugNo.Text.Trim(), SqlOperator.Like, true)/*编号*/
.AddCondition("Finder", this.ddlFindUser.SelectedValue, SqlOperator.Like, true)/*发现人*/
.AddCondition("CheckUser", this.ddlCheckUser.SelectedValue, SqlOperator.Like, true)//验收人
.AddCondition("DeviceBug.BugType", this.ddlBugType.SelectedValue, SqlOperator.Equal, true)//缺陷类别
.AddCondition("CurrentState", this.ddlCurrentState.SelectedValue, SqlOperator.Equal, true)//处理状态
.AddCondition("FindDate", this.txtFindBeginDate.Text.Trim(), SqlOperator.MoreThanOrEqual, true)//发现日期
.AddCondition("FindDate", this.txtFindEndDate.Text.Trim(), SqlOperator.LessThanOrEqual, true)//发现日期
.AddCondition("EndDate", this.txtEndBeginDate.Text.Trim(), SqlOperator.MoreThanOrEqual, true)//消缺日期
.AddCondition("EndDate", this.txtEndEndDate.Text.Trim(), SqlOperator.LessThanOrEqual, true);//消缺日期
return search.BuildConditionSql();
}
{
SearchCondition search = new SearchCondition();
search.AddCondition("GroupID", this.ddlUserGroup.SelectedValue, SqlOperator.Equal, true)//班组ID
.AddCondition("DealGroupName", this.ddlDealGroup.SelectedValue, SqlOperator.Equal, true)/*消缺单位*/
.AddCondition("VisioStationID", this.ddlStation.SelectedValue, SqlOperator.Like, true)//变电站
.AddCondition("VisioImageID", this.ddlLine.SelectedValue, SqlOperator.Like, true)/*馈线*/
.AddCondition("BugNo", this.txtBugNo.Text.Trim(), SqlOperator.Like, true)/*编号*/
.AddCondition("Finder", this.ddlFindUser.SelectedValue, SqlOperator.Like, true)/*发现人*/
.AddCondition("CheckUser", this.ddlCheckUser.SelectedValue, SqlOperator.Like, true)//验收人
.AddCondition("DeviceBug.BugType", this.ddlBugType.SelectedValue, SqlOperator.Equal, true)//缺陷类别
.AddCondition("CurrentState", this.ddlCurrentState.SelectedValue, SqlOperator.Equal, true)//处理状态
.AddCondition("FindDate", this.txtFindBeginDate.Text.Trim(), SqlOperator.MoreThanOrEqual, true)//发现日期
.AddCondition("FindDate", this.txtFindEndDate.Text.Trim(), SqlOperator.LessThanOrEqual, true)//发现日期
.AddCondition("EndDate", this.txtEndBeginDate.Text.Trim(), SqlOperator.MoreThanOrEqual, true)//消缺日期
.AddCondition("EndDate", this.txtEndEndDate.Text.Trim(), SqlOperator.LessThanOrEqual, true);//消缺日期
return search.BuildConditionSql();
}
2. 普通做法,不使用控件,列表查询页面中的代码
Code
专注于代码生成工具、.Net/.NetCore 框架架构及软件开发,以及各种Vue.js的前端技术应用。著有Winform开发框架/混合式开发框架、微信开发框架、Bootstrap开发框架、ABP开发框架、SqlSugar开发框架等框架产品。
转载请注明出处:撰写人:伍华聪 http://www.iqidi.com
转载请注明出处:撰写人:伍华聪 http://www.iqidi.com