You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

DataTable.Select遇空值整数列无结果,填0正常的问题求助

问题分析与解决方案

问题原因

DataTable的Select方法中,当列值为**DBNull(空值)**时,使用<>进行比较会返回False。因为数据库逻辑里,空值和任何值的比较结果都不成立,所以只要Sector_Pr_1、Sector_Pr_2、Sector_Pr_3任意一列是空值,整个查询条件就无法匹配,导致查不到结果。而将空值改为0后,<> NumMin的比较能正常执行,所以可以得到正确结果。

修复方案

修改DataTable.Select的查询条件,针对每个Sector_Pr_x列,同时判断列值为空或者列值不等于目标值,这样就能覆盖空值场景。具体来说,把原来的Sector_Pr_x <> NumMin替换为(Sector_Pr_x IS NULL OR Sector_Pr_x <> NumMin)。

修改后的VB代码

For i = 0 To 3
    Dim LeastCommon As Integer = MyAr(i, 0)

    For t = i + 1 To 3
        Dim MostCommon As Integer = MyAr(t, 0)

        If MostCommon - LeastCommon > 1 Then
            Dim NumPlus As Integer = MyAr(t, 1)' number to be replaced by NumMin if not present in "Sector_Pr_1"  and not in " Sector_Pr_2" and not in "Sector_Pr_3"
            Dim NumMin As Integer = MyAr(i, 1)
                    
            ' 修改查询条件,处理空值情况
            dr = DT.Select($"Sector_Pr_4={NumPlus} AND (Sector_Pr_1 IS NULL OR Sector_Pr_1 <> {NumMin}) AND (Sector_Pr_2 IS NULL OR Sector_Pr_2 <> {NumMin}) AND (Sector_Pr_3 IS NULL OR Sector_Pr_3 <> {NumMin})")

            If dr.Count > 0 Then
                dr(0)("Sector_Pr_4") = NumMin
            End If
         End If
    Next
Next

补充说明

  • 所有Sector列均为整数类型,上述条件的类型匹配没有问题。
  • 使用字符串插值($"")让代码更易读,也可以继续用字符串拼接方式,只要保证条件格式正确即可。

内容的提问来源于stack exchange,提问作者Johny Roosen

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.31 00:09:23