表单控件的副产品——查询控件

时间:2022-04-27
本文章向大家介绍表单控件的副产品——查询控件,主要内容包括其使用实例、应用技巧、基本知识点总结和需要注意事项,具有一定的参考价值,需要的朋友可以参考一下。

查询控件

    当初在写表单控件的时候,突然想到,这个表单控件稍微修改一下不就是一个查询控件了吗?

    那么查询控件需要做的什么事情呢?          1、自己描绘控件,比如能够自己添加文本框、下拉列表框这一类的控件。          2、可以自己获取用户输入的信息,根据查询方式组合where 后面的 SQL语句 。

    是不是和表单控件很像呢?

    在表单控件的SaveData()里面我们可以得到字段名称和对应的用户输入的信息,那么我们就可以写成这种方式。


         for (int i = 0; i < info.Length; i++)
             {
                iCntl = this.FindControl("c_" + info[i].ColSysName) as IGetControlValue;
                 if (iCntl != null)
                     query = ColSysName + "like '%" + iCntl.GetControlValue() + "%'";
 
             }

    当然并不是所有的查询都是只用 like 就可以搞定的,这里需要一个查询方式的属性,于是我们可以扩展一下ControlInfos 加一个属性(FindKind),用来记录查询方式。

    然后根据这个属性,我们就可以来组合SQL语句了,准确的说是where 后面的SQL语句。目前的方法还是需要使用case 。

string str = iCntl.GetControlValue() 
 switch ( infos[i].FindKind)
             {
                 case 1:
                     if (DoubleType.FromString(inputInfo[i]) != 0.0)
                     {
                         str2 = " [" + info[i].ColSysName + "]=" + str;
                     }
                     goto Label_0615;
                case 2:
                     str2 = " [" + info[i].ColSysName + "]='" + str + "'";
                     goto Label_0615;
                case 3:
                     str2 = " [" + info[i].ColSysName + "] like '%" + str + "%'";
                     goto Label_0615;
                case 4:
                     str2 = " [" + info[i].ColSysName + "] like '" + str + "%'";
                     goto Label_0615;
                case 5:
                     str2 = " [" + info[i].ColSysName + "] like '%" + str + "'";
                     goto Label_0615;
                case 6:
                     str2 = " [" + info[i].ColSysName + "] like '" + str + "'";
                     goto Label_0615;
                case 11:
                     str2 = " [" + info[i].ColSysName + "]>'" + str + "'";
                     goto Label_0615;
                case 12:
                     str2 = " [" + info[i].ColSysName + "]<'" +str + "'";
                     goto Label_0615;
                case 13:
                     str2 = " [" + info[i].ColSysName + "]>='" +str + "'";
                     goto Label_0615;
                case 14:
                     str2 = " [" + info[i].ColSysName + "]<='" + str + "'";
                     goto Label_0615;
                case 0x15:
                     strArray3 = inputInfo[i].Split(new char[] { '|' });
                     if (strArray3[0].Length != 0)
                     {
                         goto Label_04B1;
                     }
                     str2 = "";
                     goto Label_0615;
                case 0x16:
                     strArray2 = inputInfo[i].Split(new char[] { '|' });
                     if (strArray2[0].Length != 0)
                     {
                         break;
                     }
                     str2 = "";
                     goto Label_0615;
                case 0x17:
                     str3 = inputInfo[i].Replace("|", ",");
                     if (str3.Length != 0)
                     {
                         goto Label_0549;
                     }
                     str2 = "";
                     goto Label_0615;
                case 0x18:
                     str4 = inputInfo[i].Replace(",", "','").Replace("|", "','");
                     if (str4.Length != 0)
                     {
                         goto Label_05CA;
                     }
                     str2 = "";
                     goto Label_0615;
                case 30:
                     str2 = "";
                     goto Label_0615;
                default:
                     goto Label_0615;
             }

不好意思,原来使用vb.net来写的,这个是用Reflector反编译的,不知道为什么还给弄出来goto了。看来我写的代码不用混淆也可以了,我自己都看不懂了。还是发一个“原版”的吧。

Private Sub btn_click(ByVal Sender As Object, ByVal E As EventArgs)

        'MyBase.Context.Response.Write("内部事件<BR>")

        Dim colInfo() As ColumnsInfo = _Col.SetColumnsInfos()

        Dim inputInfo() As String = _Col.GetInputInfo



        If inputInfo Is Nothing Then

            System.Web.HttpContext.Current.Response.Write("_") '没有输入,或者输入有误!

            Return

        End If



        '调用外部事件——查询前

        OnBtnClick(Sender, E)





        '字段的查询方式。1:= int;2:=string; 3:like %n%;  4:like n%; 5:like %n ;6:like n;11:> string;12:< string;13:>= string;14: <= string

        Dim find As String = ""

        Dim tmp As String = ""

        Dim i As Int32

        For i =  To colInfo.Length - 

            If inputInfo(i).Length >  And inputInfo(i) <> "-999999" And inputInfo(i) <> "_n_" Then

                '输入了信息,组成查询条件

                Select Case colInfo(i).SearchKind

                    Case       '= int

                        If inputInfo(i) <>  Then

                            tmp = " [" & colInfo(i).ColSysName & "]=" & inputInfo(i)

                        End If

                    Case       '= string

                        tmp = " [" & colInfo(i).ColSysName & "]='" & inputInfo(i) & "'"

                    Case       'like %n%

                        tmp = " [" & colInfo(i).ColSysName & "] like '%" & inputInfo(i) & "%'"

                    Case       'like n%

                        tmp = " [" & colInfo(i).ColSysName & "] like '" & inputInfo(i) & "%'"

                    Case       'like %n

                        tmp = " [" & colInfo(i).ColSysName & "] like '%" & inputInfo(i) & "'"

                    Case       'like n

                        tmp = " [" & colInfo(i).ColSysName & "] like '" & inputInfo(i) & "'"

                    Case       '> string

                        tmp = " [" & colInfo(i).ColSysName & "]>'" & inputInfo(i) & "'"

                    Case       '< string

                        tmp = " [" & colInfo(i).ColSysName & "]<'" & inputInfo(i) & "'"

                    Case       '>= string

                        tmp = " [" & colInfo(i).ColSysName & "]>='" & inputInfo(i) & "'"

                    Case       '<= string

                        tmp = " [" & colInfo(i).ColSysName & "]<='" & inputInfo(i) & "'"

                    Case       'between int

                        Dim aa() As String = inputInfo(i).Split("|")

                        If aa().Length =  Then

                            tmp = ""

                        Else

                            If aa().Length =  Then

                                aa() = aa()

                            End If

                            tmp = " [" & colInfo(i).ColSysName & "] between " & aa() & " and " & aa()

                        End If

                    Case       'between string

                        Dim aa() As String = inputInfo(i).Split("|")

                        If aa().Length =  Then

                            tmp = ""

                        Else

                            If aa().Length =  Then

                                aa() = aa()

                            End If

                            tmp = " [" & colInfo(i).ColSysName & "] between '" & aa() & "' and '" & aa() & "'"

                        End If



                    Case      'col in () 多选查询 数字方式

                        Dim a23 As String = inputInfo(i).Replace("|", ",")

                        'System.Web.HttpContext.Current.Response.Write(a23)

                        If a23.Length =  Then

                            tmp = ""

                        Else

                            tmp = " [" & colInfo(i).ColSysName & "] in (" & a23 & ")"

                        End If



                    Case      'col in () 多选查询 字符串方式

                        Dim a23 As String = inputInfo(i).Replace(",", "','")

                        a23 = a23.Replace("|", "','")

                        'System.Web.HttpContext.Current.Response.Write(a23)

                        If a23.Length =  Then

                            tmp = ""

                        Else

                            tmp = " [" & colInfo(i).ColSysName & "] in ('" & a23 & "')"

                        End If



                    Case      '不生成查询条件

                        tmp = ""



                End Select



                '加到查询条件里面

                If tmp.Length >  Then

                    If find.Length >  Then

                        find &= " and " & tmp

                    Else

                        find = tmp

                    End If

                End If



            End If

            tmp = ""

        Next



        If _OutSearch.Length >  Then

            '有外部的查询条件,加到 find 里面

            If find.Length >  Then

                find = find & " and " & _OutSearch

            Else

                find = _OutSearch

            End If

        End If



        'System.Web.HttpContext.Current.Response.Write(find)

        If Not _PageTurn Is Nothing Then

            If _PageTurn.SetQuery.Length =  Then

                _PageTurn.SqlQuery = find

            Else

                If find.Length =  Then

                    _PageTurn.SqlQuery = _PageTurn.SetQuery

                Else

                    _PageTurn.SqlQuery = _PageTurn.SetQuery & " and " & find

                End If



            End If



            _Search = find

            _PageTurn.SqlQuery = find

            _PageTurn.CreateQuery()

            _PageTurn.BindFirstPage()



        End If



       

    End Sub

#End Region

    当然这里有一个适用范围和习惯的问题。我是习惯使用DataTable来装载数据,而填充 DataTable 需要SQL语句,那么我只需要得到 where 后面的部分,查询的功能就可以实现了,所以对于我来说,查询控件能够输出像 "myName like '%jyk%'" 这样的字符串就已经够用了。     还记得分页控件吗?分页控件有一个属性:myPage.SqlQuery = "";这个属性就是用来给分页控件设置查询条件的,正好可以查询控件对应上,这两个控件一配合起来,查询、分页就变得非常的简单了。

对其他的查询方式的支持的考虑     我没有用过使用实体类来显示数据的方式,所以也不知道对于实体类来说,查询是怎么做的,不过这个查询控件至少可以提供字段名和对应的值,应该是可以用得上的吧。

    可能有些人喜欢使用存储过程来返回记录,查询条件也会写在存储过程里面,那么在调用存储过程的时候需要传递存储过程的参数,这种情况,查询控件也可以帮上点忙吧。

抽象     这样出现了一个问题,由于两个控件比较象,但是总不能等表单控件写好了,然后复制粘贴,再改一改,查询控件就诞生了吧。我们是不是应该对于相同的地方进行“抽象”呢,把相同的代码放在基类里面。可能你会说,就两种情况,有必要抽象吗,还弄一个基类出来是不是多此一举呢?我的回答是:很有必要。除非这两个控件件写完了之后就再也不需要修改了。

    只是单独写很好写,不用顾忌其他,但是要考虑到其他的用法的话,那就要十分小心了。