【问题标题】:Simple way to convert dbNull to a string in VB.NET在 VB.NET 中将 dbNull 转换为字符串的简单方法
【发布时间】:2015-10-28 20:26:25
【问题描述】:

我正在寻找一种更简单的方法来检查值是否为 dbNull,如果是,则将其转换为空字符串。

我需要这种情况的一个例子是:

Dim dt As New DataTable
Dim conn As New OleDbConnection(someConnStr)
Dim adap As New OleDbDataAdapter(qryCSSInfo, cssConn)
adap.Fill(dt)


Dim someStr As String = "The first column of the first row returned: " & dt.rows(0).item(0)
Msgbox(someStr)

问题是,如果数据库中的 dt.rows(0).item(0) 为 null,它将作为 dbNull 值返回,显然不能附加到字符串。

我对这个问题的解决方案是使用 if 语句将值替换为空白字符串:

Dim dt As New DataTable
Dim conn As New OleDbConnection(someConnStr)
Dim adap As New OleDbDataAdapter(qryCSSInfo, cssConn)
adap.Fill(dt)


If Not isDBNull(dt.rows(0).item(0)) then
     Dim someStr As String = "The first column of the first row returned: " & dt.rows(0).item(0)
Else
     Dim someStr As String = "The first column of the first row returned: " & ""
End If
Msgbox(someStr)

这对我的目的来说很好用,但是如果我必须对表格中需要使用的每一列进行此项检查,就会变得不堪重负。假设我想用这个字符串显示表中的 10 列。我必须对每一个都进行检查,以确保它们不为空。有没有更简单或更简单的方法?

【问题讨论】:

  • 使用 ole 变得相当麻烦。 ADO.NET 倾向于为您生成 isColNameNull() 函数,因此使用它会更好一些,但想法是一样的。在尝试使用数据之前,您必须检查 dbnull。

标签: vb.net dbnull


【解决方案1】:

对于字符串类型可以直接使用dt.rows(0).item(0).ToString()这种方式,不用If条件

adap.Fill(dt)

Dim someStr As String = "The first column of the first row returned: " & dt.rows(0).item(0).ToString()

MsgBox(somestr)

即你可以完全省略 if 语句。根据MSDN,任何 DBNull 值都将使用.ToString() 转换为 EmptyString

还可以查看此 SO 帖子 Conversion from type 'DBNull' to type 'String'

但是,对于整数、双精度等非字符串数据库列类型,您必须使用 IsDBNull 应用检查以避免任何异常。

【讨论】:

  • .ToString() 是处理字符串数据库类型的最简单的 .Net 方法。
  • 非常感谢。我不得不去做一个旧项目,这让我很生气。直到此刻我才讨厌面包车
【解决方案2】:

您可以利用If Operator 减少几行代码:

Dim someStr As String = "The first column of the first row returned: " & _
                        If(dt.rows(0).item(0) Is DbNull.Value, String.Empty, dt.rows(0).item(0))

【讨论】:

    【解决方案3】:

    您应该能够将空字段与字符串连接 - 它应该转换为空字符串。也就是说 row.IsNull(index) 是一个很好的测试。

        SQL = "Select top 10 Region, CompanyName FROM Suppliers"
        Dim dt As DataTable = Gen.GetDataTable(SQL, scon)
        For Each row As DataRow In dt.Rows
            MsgBox(row("companyName") & " region: " & row("Region")) ' null allowed
            If row.IsNull("region") Then ' .Net test for Null
                MsgBox(row("companyName") & " region is null")
            Else
                'continue
            End If
        Next
    

    您也可以在查询中解决此问题 - 将空值隐藏为有用(或空)字符串。示例查询来自 SQL Server,我不知道您的数据库是否支持 COALESCE。

        MsgBox("COALESCE") ' SQL Server - may not be the same in ODBC databases
        SQL = "Select top 10 COALESCE(Region,'na') Region, CompanyName FROM Suppliers"
        dt = Gen.GetDataTable(SQL, scon)
        For Each row As DataRow In dt.Rows
            MsgBox(row("companyName") & " region: " & row("Region"))
        Next
    

    一些编码说明:

        Dim dt As New DataTable
        Dim conn As New OleDbConnection(someConnStr)
        Dim adap As New OleDbDataAdapter(qryCSSInfo, cssConn)
        adap.Fill(dt)
    
        If Not IsDBNull(dt.Rows(0).Item(0)) Then ' in OP
            '...
        End If
    
        ' save some typing if you know there will be only one record
        ' will throw exception is no rows are returned, check for expected count
        Dim row As DataRow = dt.Rows(0)
        If Not IsDBNull(row(0)) Then
            '...
        End If
        ' or 
        If Not row.IsNull(0) Then
            '...
        End If
    
        ' note the fields can be accessed by name so you can avoid hard coding field position
        If Not row.IsNull("FieldName") Then
            '...
        End If
    

    【讨论】:

    • 请注意,SQL 查询中的 null 以相反的方式工作 - 在 SQL 中连接字符串和 null 返回 null。
    • 把这个验证放在 SQL 查询中是一个好点。由于未指定 DB 引擎,因此在 IsNull 之上推荐更通用的 Coalesce 也很聪明。
    【解决方案4】:

    最简单的方法是在字段或字符串后添加一个“”。 例如:

      dim EmptyString as string = Nullfield() & ""
      if EmptyString = ""
         ' in the sample, it should.
      end if
    

    因此,您可以在代码中使用:

     If dt.rows(0).item(0) & "" = "" then
          ' it should be...
     end if
    

    【讨论】:

      【解决方案5】:

      我在数据网格的单元格中获取了一些空数据;正确检索该数据 我将“”字符串连接到单元格值:

      Dim readVal As String = "" & row.Cells(2).Value
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2013-09-16
        • 2010-12-21
        • 1970-01-01
        • 2014-12-22
        • 2015-12-15
        • 2014-03-05
        • 2017-08-16
        • 2017-06-09
        相关资源
        最近更新 更多