以文本方式查看主题

-  Foxtable(狐表)  (http://www.foxtable.com/bbs/index.asp)
--  专家坐堂  (http://www.foxtable.com/bbs/list.asp?boardid=2)
----  关于查询过于复杂的困惑  (http://www.foxtable.com/bbs/dispbbs.asp?boardid=2&id=41980)

--  作者:东坡一剑
--  发布时间:2013/10/31 21:20:00
--  关于查询过于复杂的困惑

If Forms("主界面").opened Then
    MyTimers("计划1").Enabled = False
    Forms("主界面").controls("库存警告").Enabled = False
   
    Dim Today As Date = Date.Today
    Dim Names As String = "日一二三四五六"
    Dim Result As String
    Result = "星期" & Names.Chars(Today.DayOfWeek)
    Forms("主界面").controls("登录账号").Text = user.Name
    Forms("主界面").controls("用户姓名").Text = user.Name
    Forms("主界面").controls("用户分组").Text = User.Group
    Forms("主界面").controls("登录日期").Text = Today & " " & Result
    Dim cmd1,cmd2 As new SQLCommand
    Dim dt1,dt2 As DataTable
    Dim s As String
 下面的黑字和红字部分的代码几乎完全一样,红字代码可以正常执行,而黑字部分,执行到绿字部分出错,提示为"查询过于复杂."甚为不解,请大家帮忙看看!
    If user.Group = "兰州嘉禾天悦商贸有限公司"  Then
        cmd1.C
        cmd2.C
        cmd1.CommandText = "Select * from {库存表} where 库房 <>\'" & user.Group & "库房\'"
        cmd2.CommandText = "Select * from {库存表} where 库房 =\'" & user.Group & "库房\'"
        dt1 = cmd1.ExecuteReader()
        dt2 = cmd2.ExecuteReader()
MessageBox.Show(1)
        If dt2.DataRows.count > 0 Then
            For Each dr2 As DataRow In dt2.DataRows
                Dim dr1 As DataRow = dt1.Find("货号 = \'" & dr2("货号") & "\' And 颜色 =\'" & dr2("颜色") & "\'")
                If dr1 Is Nothing Then
                    s = s & "Or [_Identify] =" & dr2("_Identify")
                End If
            Next
            s = s.trim("O")
            s = s.trim("r")
MessageBox.Show(2)
            If s > "" Then
                Forms("主界面").controls("库存警告").Enabled = True
                Forms("主界面").controls("库存警告").Image = GetImage("kcjg.gif")
                MyTimers("计划1").Enabled = True
 MessageBox.Show(3)  
                DataTables("库存表").LoadFilter = ""
MessageBox.Show(4) 
                DataTables("库存表").LoadFilter = s
MessageBox.Show(s) 
                DataTables("库存表").Load()
 
            Else
                Forms("主界面").controls("库存警告").Image = GetImage("44.gif")
                MyTimers("计划1").Enabled = False
                Forms("主界面").controls("库存警告").Enabled = False
            End If
        End If
    End If

   
    If user.Type = UserTypeEnum.User Then
        If user.Group <> "兰州嘉禾天悦商贸有限公司" Then
            cmd1.C
            cmd1.CommandText = "Select * from {库存表} where 库房 =\'" & user.Group & "库房\'" & " And 库区 =\'展厅\'"
            dt1 = cmd1.ExecuteReader()
            cmd2.C
            cmd2.CommandText = "select * from {库存表} where 库房 =\'" & user.Group & "库房\'" & " And 库区 =\'后堂\'"
            dt2 = cmd2.ExecuteReader()
            If dt2.DataRows.count > 0 Then
                For Each dr2 As DataRow In dt2.DataRows
                    Dim dr1 As DataRow = dt1.Find("货号 = \'" & dr2("货号") & "\' And 颜色 =\'" & dr2("颜色") & "\'")
                    If dr1 Is Nothing Then
                        s = s & "Or [_Identify] =" & dr2("_Identify")
                    End If
                Next
                s = s.trim("O")
                s = s.trim("r")
                If s > "" Then
                    Forms("主界面").controls("库存警告").Enabled = True
                    Forms("主界面").controls("库存警告").Image = GetImage("kcjg.gif")
                    MyTimers("计划1").Enabled = True
                    DataTables("库存表").LoadFilter = ""
                    DataTables("库存表").LoadFilter = s
MessageBox.Show(s)
                    DataTables("库存表").Load()
                Else
                    Forms("主界面").controls("库存警告").Image = GetImage("44.gif")
                    MyTimers("计划1").Enabled = False
                    Forms("主界面").controls("库存警告").Enabled = False
                End If
            End If
        End If
    End If
End If


--  作者:有点甜
--  发布时间:2013/10/31 21:27:00
--  
 这个简单,你msgbox(s)看看就知道了。

 Or 的前面最好记得加空格
 

--  作者:东坡一剑
--  发布时间:2013/10/31 21:44:00
--  
msgbox(s)已经做了,应该不是你说的问题。因为前后两段的msgbox(s)出现的字符结构都是一样的。唯一不同的是:黑字部分代码msgbox(s)出现的字符串超级长,因为符合条件的记录很多,足有几百条。而红字部分只有短短的几行。我怀疑是当loadfilter的字符串长度有一定的限制。不知是不是这样?
--  作者:有点甜
--  发布时间:2013/10/31 21:52:00
--  
 试试改成这种

For Each dr2 As DataRow In dt2.DataRows
    Dim dr1 As DataRow = dt1.Find("货号 = \'" & dr2("货号") & "\' And 颜色 =\'" & dr2("颜色") & "\'")
    If dr1 Is Nothing Then
        s = s & dr2("_Identify") & ","
    End If
Next

。。。。。。

DataTables("库存表").LoadFilter = "[_Identify] in (" & s.trim(",") & ")"

--  作者:东坡一剑
--  发布时间:2013/10/31 22:04:00
--  
谢谢!我试试。
--  作者:东坡一剑
--  发布时间:2013/10/31 22:36:00
--  

果然就成功了。

但是之前为什么就不行呢?

难道数据多少确实有影响?


--  作者:有点甜
--  发布时间:2013/10/31 23:02:00
--  
 不知道,应该是sql语句的问题,得看你生成了什么的sql语句才能说。