以文本方式查看主题

-  Foxtable(狐表)  (http://foxtable.com/bbs/index.asp)
--  专家坐堂  (http://foxtable.com/bbs/list.asp?boardid=2)
----  每个变量值是否为空的判断  (http://foxtable.com/bbs/dispbbs.asp?boardid=2&id=95816)

--  作者:huhu
--  发布时间:2017/2/6 11:35:00
--  每个变量值是否为空的判断
比如有5个变量A,B,C,D,E.
需要考虑全部为空的情况,全部不为空。
if A="" and B=“” and C="" and D="" and E="" then
代码
elseif A<>"" and B<>“” and C<>"" and D<>"" and E<>"" then
代码
end if

但还有比如A为空,其他4个不为空。A,B为空,其他3个不为空,等等
一个一个写if else是不是太多了,也容易出错。
有什么办法可以做到,只需要判断5个变量不为空的写代码。

--  作者:有点色
--  发布时间:2017/2/6 13:35:00
--  

 直接说你要做什么。逻辑是什么。一般,这样判断

 

Dim str As String = ""

If A <> "" Then

    str &= "A不为空,"

End If

If B <> "" Then

    str &= "B不为空,"

End If

If C <> "" Then

     str &= "C不为空,"

End If


--  作者:huhu
--  发布时间:2017/2/6 15:18:00
--  
 <WebMethod()>
    Public Function Detailedquery(ByVal scddhm As String, ByVal gdhh As String, ByVal wlbm As String, ByVal kwlb As String, ByVal startdate As String, ByVal enddate As String) As String
        Dim cnStr As String = "Data Source=172.16.11.201;Initial Catalog=songjiang;Integrated Security=False;User ID=sa;Password=bdcom103liujy;"
        Dim cn As New SqlClient.SqlConnection(cnStr)
        cn.Open()
        Dim state As String = ""
        If scddhm = "" And gdhh = "" And wlbm = "" And kwlb = "" And startdate = "" And enddate = "" Then
            Dim adapter As New SqlClient.SqlDataAdapter("s elect * from [可用数量表]", cn)
            Dim dt As New DataTable
            adapter.Fill(dt)
            cn.Close()
            For Each dr As DataRow In dt.Rows
                state &= "生产订单号码:" & dr("生产订单号码") & "工单行号:" & dr("工单行号") & "物料编码:" & dr("物料编码") & "库位类别:" & dr("库位类别") & "入库时间:" & dr("入库时间") & "|"
            Next
        ElseIf scddhm <> "" And gdhh <> "" And wlbm <> "" And kwlb <> "" And startdate <> "" And enddate <> "" Then
            Dim adapter As New SqlClient.SqlDataAdapter("se lect * from [可用数量表] where 生产订单号码 = \'" & scddhm & "\' and 工单行号 = \'" & gdhh & "\' and 物料编码 = \'" & wlbm & "\' and 库位类别 = \'" & kwlb & "\' and 入库时间 >= \'" & startdate & "\'  and 入库时间 <= \'" & enddate & "\' ", cn)
            Dim dt As New DataTable
            adapter.Fill(dt)
            cn.Close()
            For Each dr As DataRow In dt.Rows
                state &= "生产订单号码:" & dr("生产订单号码") & "工单行号:" & dr("工单行号") & "物料编码:" & dr("物料编码") & "库位类别:" & dr("库位类别") & "入库时间:" & dr("入库时间") & "|"
            Next
        End If
        Return state
    End Function

红色代码是条件。但这仅仅是2个分支,其他分支怎么弄,针对每一个不为空的都要判断一下。

--  作者:有点色
--  发布时间:2017/2/6 15:23:00
--  

红色代码,你复制多段即可。

 

Dim filter As String = "1=1"
If r.isnull("一级地址") Then
    filter &= " and 一级地址 is null"
Else
    filter &= " and 一级地址 = \'" & r("一级地址") & "\'"
End If

Dim dr As DataRow = DataTables("五级地址网格关联表").Find(filter)

 

http://www.foxtable.com/webhelp/scr/1058.htm

 


--  作者:huhu
--  发布时间:2017/2/6 16:46:00
--  
Public Function Detailedquery(ByVal scddhm As String, ByVal gdhh As String, ByVal wlbm As String, ByVal kwlb As String, ByVal startdate As String, ByVal enddate As String) As String
        Dim cnStr As String = "Data Source=172.16.11.201;Initial Catalog=songjiang;Integrated Security=False;User ID=sa;Password=bdcom103liujy;"
        Dim cn As New SqlClient.SqlConnection(cnStr)
        cn.Open()
        Dim state As String = ""
        Dim filter As String = "1=1"
        If scddhm = "" And gdhh = "" And wlbm = "" And kwlb = "" And startdate = "" And enddate = "" Then
            Dim adapter1 As New SqlClient.SqlDataAdapter("s elect * from [可用数量表]", cn)
            Dim dt As New DataTable
            adapter1.Fill(dt)
            cn.Close()
            For Each dr As DataRow In dt.Rows
                state &= "生产订单号码:" & dr("生产订单号码") & "工单行号:" & dr("工单行号") & "物料编码:" & dr("物料编码") & "库位类别:" & dr("库位类别") & "入库时间:" & dr("入库时间") & "|"
            Next
            Return state
        Else
            If scddhm <> "" Then
                filter &= "and 生产订单号码 = \'" & scddhm & "\' "
            Else
            End If
            If gdhh <> "" Then
                filter &= "and 工单行号 = \'" & gdhh & "\'"
            End If
            If wlbm <> "" Then
                filter &= "and 物料编码 = \'" & wlbm & "\'"
            End If
            If kwlb <> "" Then
                filter &= "库位类别 = \'" & kwlb & "\'"
            End If
            If startdate <> "" Then
                filter &= "入库时间 >= \'" & startdate & "\'"
            End If
            If enddate <> "" Then
                filter &= "入库时间 <= \'" & enddate & "\'"
            End If
            Dim adapter As New SqlClient.SqlDataAdapter("s elect * from [可用数量表] where filter", cn)
            Dim dt As New DataTable
            adapter.Fill(dt)
            cn.Close()
            For Each dr As DataRow In dt.Rows
                state &= "生产订单号码:" & dr("生产订单号码") & "工单行号:" & dr("工单行号") & "物料编码:" & dr("物料编码") & "库位类别:" & dr("库位类别") & "入库时间:" & dr("入库时间") & "|"
            Next
            Return state
        End If
    End Function

报这个错误:System.Data.SqlClient.SqlException: 在应使用条件的上下文(在 \'filter\' 附近)中指定了非布尔类型的表达式。

--  作者:有点色
--  发布时间:2017/2/6 16:49:00
--  
Dim adapter As New SqlClient.SqlDataAdapter("select * from [可用数量表] where " & filter, cn)
--  作者:huhu
--  发布时间:2017/2/6 17:25:00
--  
<WebMethod()>
    Public Function Detailedquery(ByVal scddhm As String, ByVal gdhh As String, ByVal wlbm As String, ByVal kwlb As String, ByVal startdate As String, ByVal enddate As String) As String
        Dim cnStr As String = "Data Source=172.16.11.201;Initial Catalog=songjiang;Integrated Security=False;User ID=sa;Password=bdcom103liujy;"
        Dim cn As New SqlClient.SqlConnection(cnStr)
        cn.Open()
        Dim state As String = ""

        If scddhm = "" And gdhh = "" And wlbm = "" And kwlb = "" And startdate = "" And enddate = "" Then
            Dim adapter1 As New SqlClient.SqlDataAdapter("s elect * from [可用数量表]", cn)
            Dim dt As New DataTable
            adapter1.Fill(dt)
            cn.Close()
            For Each dr As DataRow In dt.Rows
                state &= "生产订单号码:" & dr("生产订单号码") & "工单行号:" & dr("工单行号") & "物料编码:" & dr("物料编码") & "库位类别:" & dr("库位类别") & "入库时间:" & dr("入库时间") & "|"
            Next
            Return state
        Else
            Dim filter As String = "1=1"
            If scddhm <> "" Then
                Filter &= "and 生产订单号码 = \'" & scddhm & "\' "
            Else
            End If
            If gdhh <> "" Then
                Filter &= "and 工单行号 = \'" & gdhh & "\'"
            End If
            If wlbm <> "" Then
                Filter &= "and 物料编码 = \'" & wlbm & "\'"
            End If
            If kwlb <> "" Then
                Filter &= "库位类别 = \'" & kwlb & "\'"
            End If
            If startdate <> "" Then
                Filter &= "入库时间 >= \'" & startdate & "\'"
            End If
            If enddate <> "" Then
                Filter &= "入库时间 <= \'" & enddate & "\'"
            End If
            Dim adapter As New SqlClient.SqlDataAdapter("s elect * from [可用数量表] where" & filter, cn)
            Dim dt As New DataTable
            adapter.Fill(dt)
            cn.Close()
            For Each dr As DataRow In dt.Rows
                state &= "生产订单号码:" & dr("生产订单号码") & "工单行号:" & dr("工单行号") & "物料编码:" & dr("物料编码") & "库位类别:" & dr("库位类别") & "入库时间:" & dr("入库时间") & "|"
            Next
            Return state
        End If
    End Function

System.Data.SqlClient.SqlException: \'=\' 附近有语法错误。是哪里出错呢?

--  作者:有点色
--  发布时间:2017/2/6 17:27:00
--  

            Dim filter As String = "1=1"

            If scddhm <> "" Then
                Filter &= " and 生产订单号码 = \'" & scddhm & "\' "
            Else
            End If
            If gdhh <> "" Then
                Filter &= " and 工单行号 = \'" & gdhh & "\'"
            End If
            If wlbm <> "" Then
                Filter &= " and 物料编码 = \'" & wlbm & "\'"
            End If
            If kwlb <> "" Then
                Filter &= " and 库位类别 = \'" & kwlb & "\'"
            End If
            If startdate <> "" Then
                Filter &= " and 入库时间 >= \'" & startdate & "\'"
            End If
            If enddate <> "" Then
                Filter &= " and 入库时间 <= \'" & enddate & "\'"
            End If
            Dim adapter As New SqlClient.SqlDataAdapter("s elect * from [可用数量表] where " & filter, cn)