在VB.NET中将dbNull转换为字符串的简单方法

6

我希望找到一种更简单的方法来检查值是否为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)为空,它将返回一个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。 - Brian Hibbert
1
http://stackoverflow.com/a/29611932/1070452 - Ňɏssa Pøngjǣrdenlarp
5个回答

15
对于字符串类型,您可以直接使用以下方式 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帖子:从类型“DBNull”转换为类型“String”

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


1
".ToString()是处理字符串数据库类型的最简单的.Net方式。" - NoAlias
非常感谢。我不得不去处理一个旧项目,这让我很烦恼。直到此刻我才喜欢上Van。 - dgo

3
你可以利用 If运算符 来减少几行代码:
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))

1

你应该可以将一个空字段与字符串连接起来 - 它应该转换为一个空字符串。也就是说,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

您也可以在查询中解决此问题-将null转换为有用的(或空)字符串。示例查询来自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。 - rheitzman
将此验证放入SQL查询中是一个好点子。由于未指定DB引擎,建议使用更通用的Coalesce而不是IsNull也很聪明。 - NoAlias

0
最简单的方法就是在字段或字符串后面添加一个 ""。 例如:
  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

0
我在数据网格的单元格中得到了一些空数据;为了正确检索这些数据,我将“”字符串与单元格值连接起来:
Dim readVal As String = "" & row.Cells(2).Value

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接