Foxtable(狐表)用户栏目专家坐堂 → 每个变量值是否为空的判断


  共有1979人关注过本帖树形打印复制链接

主题:每个变量值是否为空的判断

帅哥哟,离线,有人找我吗?
huhu
  1楼 | 信息 | 搜索 | 邮箱 | 主页 | UC


加好友 发短信
等级:五尾狐 帖子:1182 积分:8225 威望:0 精华:0 注册:2015/3/30 10:44:00
每个变量值是否为空的判断  发帖心情 Post By: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个变量不为空的写代码。

 回到顶部
帅哥哟,离线,有人找我吗?
huhu
  2楼 | 信息 | 搜索 | 邮箱 | 主页 | UC


加好友 发短信
等级:五尾狐 帖子:1182 积分:8225 威望:0 精华:0 注册:2015/3/30 10:44:00
  发帖心情 Post By: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个分支,其他分支怎么弄,针对每一个不为空的都要判断一下。

 回到顶部
帅哥哟,离线,有人找我吗?
huhu
  3楼 | 信息 | 搜索 | 邮箱 | 主页 | UC


加好友 发短信
等级:五尾狐 帖子:1182 积分:8225 威望:0 精华:0 注册:2015/3/30 10:44:00
  发帖心情 Post By: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' 附近)中指定了非布尔类型的表达式。

 回到顶部
帅哥哟,离线,有人找我吗?
huhu
  4楼 | 信息 | 搜索 | 邮箱 | 主页 | UC


加好友 发短信
等级:五尾狐 帖子:1182 积分:8225 威望:0 精华:0 注册:2015/3/30 10:44:00
  发帖心情 Post By: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: '=' 附近有语法错误。是哪里出错呢?

 回到顶部