2015-10-28 54 views
2

我正在寻找一种更简单的方法来检查值是否为dbNull,并将其转换为空字符串(如果有的话)。将dbNull转换为VB.NET中的字符串的简单方法

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

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列表,我想用这个字符串显示。我必须对每一个进行检查以确保它们不为空。有没有更容易或更简单的方法?

+0

与OLE它变得相当繁琐。 ADO.NET倾向于为您生成isColNameNull()函数,所以使用它会更好一些,但想法是一样的。在尝试使用数据之前,您必须检查dbnull。 –

+1

http://stackoverflow.com/a/29611932/1070452 – Plutonix

回答

5

对于字符串类型,您可以直接使用这种方式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值将被转换为EmptyString与.ToString()

还要检查该SO发布Conversion from type 'DBNull' to type 'String'

然而,对于非字符串数据库列类型,例如整型,双打必须使用IsDBNull避免任何申请检查例外。

+1

.ToString()是最简单的。网络方式来处理这个字符串数据库类型。 – N0Alias

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)) 
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 

你也可以在查询中解决这个问题 - 将空值转换为有用的(或空的)字符串。该示例查询来自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 
+0

请注意,SQL查询中的空值以相反的方式工作 - SQL中字符串和null的串联返回null。 – rheitzman

+0

将此验证放入SQL查询中是一个很好的观点。由于没有指定数据库引擎,建议在IsNull上方更通用的Coalesce也很聪明。 – N0Alias

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 
相关问题