在vb.net 中,使用datareader讀取sql-server 數據庫時,當遇到數據是null時就出錯,"無法取得值,因為它是"dbnull""
Line 381: Get
Line 382: Try
Line 383: Return CType(Me(Me.tableitem.upc_noColumn),String)
Line 384: Catch e As InvalidCastException
Line 385: Throw New StrongTypingException("无法获取值,因为它是 DBNull。", e)
---------------------------------------------------------------
可以加个判断语句
---------------------------------------------------------------
if Me.tableitem.upc_noColumn is dbnull then
'do your work
end if
---------------------------------------------------------------
用GetString方法:
Dim Str As String
If datareader[0]=DBNull.Value Then
Str = NULL
Else
Str = datareader.GetString(0)
---------------------------------------------------------------
Acony(潇河) 说得对,就要这么判断,我原来也经常碰到这种问题的。
---------------------------------------------------------------
If not IsDbNull(DataSet21.item(iditem).item_no) then
your code.
end if
加個not不就行了^-^.