2015-10-28 33 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 
相關問題