列表查询组件代码, 简化拼接条件SQL语句的麻烦

控件代码及测试例子:http://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'
 
那么代码如下:

None.gif             SearchCondition search  =   new  SearchCondition();
None.gif            search.AddCondition(
" AA " 1 , SqlOperator.NotEqual)
None.gif                .AddCondition(
" AA2 " " AA2Value " , SqlOperator.Like)
None.gif                .AddCondition(
" AA3 " " Value3 " , SqlOperator.Equal)
None.gif                .AddCondition(
" AA4 " " Value4 " , SqlOperator.LessThan)
None.gif                .AddCondition(
" AA5 " " Value5 " , SqlOperator.MoreThan)
None.gif                .AddCondition(
" AA6 " " Value6 " , SqlOperator.MoreThanOrEqual)
None.gif                .AddCondition(
" AA7 " " value7 " , SqlOperator.LessThanOrEqual);
None.gif            
string  conditionSql  =  search.BuildConditionSql();


2. 生成基于Enterprise Library的DbCommand对象

None.gif             Database db  =  DatabaseFactory.CreateDatabase();
None.gif            SearchCondition search 
=   new  SearchCondition();
None.gif            search.AddCondition(
" Name " " 测试 "  , SqlOperator.Like)
None.gif                  .AddCondition(
" ID " 1 , SqlOperator.MoreThanOrEqual);
None.gif            DbCommand dbComand  
=  search.BuildDbCommand(db,  " select Comments from Test " "  Order by Name " );
None.gif            
using  (IDataReader dr  =  db.ExecuteReader(dbComand))
ExpandedBlockStart.gifContractedBlock.gif            
dot.gif {
InBlock.gif                
while (dr.Read())
ExpandedSubBlockStart.gifContractedSubBlock.gif                
dot.gif{
InBlock.gif                    
this.txtSql.Text += "\r\n" + dr["Comments"].ToString();
ExpandedSubBlockEnd.gif                }

ExpandedBlockEnd.gif            }


下面是该控件的类对象图解
Search.jpg

下面我们比较一下使用该控件和不使用在列表查询页面中的代码,可以看出使用了控件后的代码大大较少了,并且可读性也增强了

1. 使用该控件, 列表查询页面中的代码

None.gif      private   string  GetCondition()
ExpandedBlockStart.gifContractedBlock.gif    
dot.gif {
InBlock.gif        SearchCondition search 
= new SearchCondition();
InBlock.gif        search.AddCondition(
"GroupID"this.ddlUserGroup.SelectedValue, SqlOperator.Equal, true)//班组ID
ExpandedSubBlockStart.gifContractedSubBlock.gif
              .AddCondition("DealGroupName"this.ddlDealGroup.SelectedValue, SqlOperator.Equal, true)/**//*消缺单位*/
InBlock.gif              .AddCondition(
"VisioStationID"this.ddlStation.SelectedValue, SqlOperator.Like, true)//变电站
ExpandedSubBlockStart.gifContractedSubBlock.gif
              .AddCondition("VisioImageID"this.ddlLine.SelectedValue, SqlOperator.Like, true)/**//*馈线*/
ExpandedSubBlockStart.gifContractedSubBlock.gif              .AddCondition(
"BugNo"this.txtBugNo.Text.Trim(), SqlOperator.Like, true)/**//*编号*/
ExpandedSubBlockStart.gifContractedSubBlock.gif              .AddCondition(
"Finder"this.ddlFindUser.SelectedValue, SqlOperator.Like, true)/**//*发现人*/
InBlock.gif              .AddCondition(
"CheckUser"this.ddlCheckUser.SelectedValue, SqlOperator.Like, true)//验收人
InBlock.gif
              .AddCondition("DeviceBug.BugType"this.ddlBugType.SelectedValue, SqlOperator.Equal, true)//缺陷类别
InBlock.gif
              .AddCondition("CurrentState"this.ddlCurrentState.SelectedValue, SqlOperator.Equal, true)//处理状态
InBlock.gif
              .AddCondition("FindDate"this.txtFindBeginDate.Text.Trim(), SqlOperator.MoreThanOrEqual, true)//发现日期
InBlock.gif
              .AddCondition("FindDate"this.txtFindEndDate.Text.Trim(), SqlOperator.LessThanOrEqual, true)//发现日期
InBlock.gif
              .AddCondition("EndDate"this.txtEndBeginDate.Text.Trim(), SqlOperator.MoreThanOrEqual, true)//消缺日期
InBlock.gif
              .AddCondition("EndDate"this.txtEndEndDate.Text.Trim(), SqlOperator.LessThanOrEqual, true);//消缺日期
InBlock.gif

InBlock.gif        
return search.BuildConditionSql();
ExpandedBlockEnd.gif    }

2. 普通做法,不使用控件,列表查询页面中的代码
ContractedBlock.gif ExpandedBlockStart.gif Code
None.gif        private string GetCondition()
ExpandedBlockStart.gifContractedBlock.gif        
dot.gif{
InBlock.gif            
string condition = "";
InBlock.gif            
if ( this.ddlUserGroup.SelectedValue != "0")
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                condition 
+= string.Format( " GroupID = {0}" , this.ddlUserGroup.SelectedValue.ToString() );
ExpandedSubBlockEnd.gif            }

InBlock.gif
InBlock.gif            
//消缺单位
InBlock.gif
            if ( this.ddlDealGroup.SelectedValue != "0")
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                
if (condition == "")
ExpandedSubBlockStart.gifContractedSubBlock.gif                
dot.gif{
InBlock.gif                    condition 
+= string.Format( " DealGroupName = '{0}'" , this.ddlDealGroup.SelectedItem.Text );
ExpandedSubBlockEnd.gif                }

InBlock.gif                
else
ExpandedSubBlockStart.gifContractedSubBlock.gif                
dot.gif{
InBlock.gif                    condition 
+= string.Format( " And DealGroupName = '{0}'" , this.ddlDealGroup.SelectedItem.Text );
ExpandedSubBlockEnd.gif                }

ExpandedSubBlockEnd.gif            }

InBlock.gif
InBlock.gif            
InBlock.gif            
if (this.txtStation.Text.Trim() != "")
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                
if (condition == "")
ExpandedSubBlockStart.gifContractedSubBlock.gif                
dot.gif{
InBlock.gif                    condition 
+= string.Format(" Station like '%{0}%'",this.txtStation.Text.Trim() );
ExpandedSubBlockEnd.gif                }

InBlock.gif                
else
ExpandedSubBlockStart.gifContractedSubBlock.gif                
dot.gif{
InBlock.gif                    condition 
+= string.Format(" And Station like '%{0}%' ",this.txtStation.Text.Trim() );
ExpandedSubBlockEnd.gif                }

ExpandedSubBlockEnd.gif            }

InBlock.gif
InBlock.gif            
if (this.txtLineName.Text.Trim() != "")
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{                
InBlock.gif                
if (condition == "")
ExpandedSubBlockStart.gifContractedSubBlock.gif                
dot.gif{
InBlock.gif                    condition 
+= string.Format(" LineName like '%{0}%'",this.txtLineName.Text.Trim() );
ExpandedSubBlockEnd.gif                }

InBlock.gif                
else
ExpandedSubBlockStart.gifContractedSubBlock.gif                
dot.gif{
InBlock.gif                    condition 
+= string.Format(" And LineName like '%{0}%' ",this.txtLineName.Text.Trim() );
ExpandedSubBlockEnd.gif                }

ExpandedSubBlockEnd.gif            }

InBlock.gif
InBlock.gif            
//编号
InBlock.gif
            if (this.txtBugNo.Text.Trim() != "")
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                
if (condition == "")
ExpandedSubBlockStart.gifContractedSubBlock.gif                
dot.gif{
InBlock.gif                    condition 
+= string.Format("BugNo like '%{0}%'",this.txtBugNo.Text.Trim() );
ExpandedSubBlockEnd.gif                }

InBlock.gif                
else
ExpandedSubBlockStart.gifContractedSubBlock.gif                
dot.gif{
InBlock.gif                    condition 
+= string.Format(" And BugNo like '%{0}%'",this.txtBugNo.Text.Trim() );
ExpandedSubBlockEnd.gif                }

ExpandedSubBlockEnd.gif            }

InBlock.gif
InBlock.gif            
//发现人
InBlock.gif
            if ( ddlFindUser.SelectedIndex >= 1 )
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                
if (condition == "")
ExpandedSubBlockStart.gifContractedSubBlock.gif                
dot.gif{
InBlock.gif                    condition 
+= string.Format("Finder like '%{0}%'",ddlFindUser.SelectedValue  );
ExpandedSubBlockEnd.gif                }

InBlock.gif                
else
ExpandedSubBlockStart.gifContractedSubBlock.gif                
dot.gif{
InBlock.gif                    condition 
+= string.Format(" And Finder like '%{0}%'",ddlFindUser.SelectedValue );
ExpandedSubBlockEnd.gif                }

ExpandedSubBlockEnd.gif            }

InBlock.gif
InBlock.gif            
//验收人
InBlock.gif
            if ( this.ddlCheckUser.SelectedIndex >= 1 )
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                
if (condition == "")
ExpandedSubBlockStart.gifContractedSubBlock.gif                
dot.gif{
InBlock.gif                    condition 
+= string.Format("CheckUser like '%{0}%'",this.ddlCheckUser.SelectedValue );
ExpandedSubBlockEnd.gif                }

InBlock.gif                
else
ExpandedSubBlockStart.gifContractedSubBlock.gif                
dot.gif{
InBlock.gif                    condition 
+= string.Format(" And CheckUser like '%{0}%'",this.ddlCheckUser.SelectedValue );
ExpandedSubBlockEnd.gif                }

ExpandedSubBlockEnd.gif            }

InBlock.gif
InBlock.gif            
//缺陷类别
InBlock.gif
            if (this.ddlBugType.SelectedValue.Trim() != "#")
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                
if (condition == "")
ExpandedSubBlockStart.gifContractedSubBlock.gif                
dot.gif{
InBlock.gif                    condition 
+= string.Format("DeviceBug.BugType={0}",this.ddlBugType.SelectedValue.Trim() );
ExpandedSubBlockEnd.gif                }

InBlock.gif                
else
ExpandedSubBlockStart.gifContractedSubBlock.gif                
dot.gif{
InBlock.gif                    condition 
+= string.Format(" And DeviceBug.BugType={0}",this.ddlBugType.SelectedValue.Trim() );
ExpandedSubBlockEnd.gif                }

ExpandedSubBlockEnd.gif            }

InBlock.gif            
InBlock.gif            
//处理状态
InBlock.gif
            if (this.ddlCurrentState.SelectedValue.Trim() != "#")
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                
if (condition == "")
ExpandedSubBlockStart.gifContractedSubBlock.gif                
dot.gif{
InBlock.gif                    condition 
+= string.Format("CurrentState='{0}'",this.ddlCurrentState.SelectedValue.Trim() );
ExpandedSubBlockEnd.gif                }

InBlock.gif                
else
ExpandedSubBlockStart.gifContractedSubBlock.gif                
dot.gif{
InBlock.gif                    condition 
+= string.Format(" And CurrentState='{0}'",this.ddlCurrentState.SelectedValue.Trim() );
ExpandedSubBlockEnd.gif                }

ExpandedSubBlockEnd.gif            }

InBlock.gif
InBlock.gif            
//发现日期
InBlock.gif
            if (this.txtFindBeginDate.Text.Trim() != "")
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                
if (condition == "")
ExpandedSubBlockStart.gifContractedSubBlock.gif                
dot.gif{
InBlock.gif                    condition 
+= string.Format("FindDate>='{0}'",this.txtFindBeginDate.Text.Trim() );
ExpandedSubBlockEnd.gif                }

InBlock.gif                
else
ExpandedSubBlockStart.gifContractedSubBlock.gif                
dot.gif{
InBlock.gif                    condition 
+= string.Format(" And FindDate>='{0}'",this.txtFindBeginDate.Text.Trim() );
ExpandedSubBlockEnd.gif                }

ExpandedSubBlockEnd.gif            }

InBlock.gif            
if (this.txtFindEndDate.Text.Trim() != "")
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                
if (condition == "")
ExpandedSubBlockStart.gifContractedSubBlock.gif                
dot.gif{
InBlock.gif                    condition 
+= string.Format("FindDate<='{0}'",this.txtFindEndDate.Text.Trim() );
ExpandedSubBlockEnd.gif                }

InBlock.gif                
else
ExpandedSubBlockStart.gifContractedSubBlock.gif                
dot.gif{
InBlock.gif                    condition 
+= string.Format(" And FindDate<='{0}'",this.txtFindEndDate.Text.Trim() );
ExpandedSubBlockEnd.gif                }

ExpandedSubBlockEnd.gif            }

InBlock.gif
InBlock.gif            
//消缺日期
InBlock.gif
            if (this.txtEndBeginDate.Text.Trim() != "")
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                
if (condition == "")
ExpandedSubBlockStart.gifContractedSubBlock.gif                
dot.gif{
InBlock.gif                    condition 
+= string.Format("EndDate>='{0}'",this.txtEndBeginDate.Text.Trim() );
ExpandedSubBlockEnd.gif                }

InBlock.gif                
else
ExpandedSubBlockStart.gifContractedSubBlock.gif                
dot.gif{
InBlock.gif                    condition 
+= string.Format(" And EndDate>='{0}'",this.txtEndBeginDate.Text.Trim() );
ExpandedSubBlockEnd.gif                }

ExpandedSubBlockEnd.gif            }

InBlock.gif            
if (this.txtEndEndDate.Text.Trim() != "")
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                
if (condition == "")
ExpandedSubBlockStart.gifContractedSubBlock.gif                
dot.gif{
InBlock.gif                    condition 
+= string.Format("EndDate<='{0}'",this.txtEndEndDate.Text.Trim() );
ExpandedSubBlockEnd.gif                }

InBlock.gif                
else
ExpandedSubBlockStart.gifContractedSubBlock.gif                
dot.gif{
InBlock.gif                    condition 
+= string.Format(" And EndDate<='{0}'",this.txtEndEndDate.Text.Trim() );
ExpandedSubBlockEnd.gif                }

ExpandedSubBlockEnd.gif            }

InBlock.gif            
return condition;        
ExpandedBlockEnd.gif        }
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值