在vb.net中处理dbnull数据 [英] handling dbnull data in vb.net

查看:246
本文介绍了在vb.net中处理dbnull数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想生成从MS-Access数据库检索并存储在 DataTable 对象/变量myDataTable中的数据的格式化输出。但是,myDataTable中的某些字段包含 dbNull 数据。因此,如果 lastname intials sID 中任何一个字段的值是 dbNull 。

 将myDataTable设置为DataTable 
dim tmpStr作为字符串
将sID设置为整数= 1

...
myDataTable = myTableAdapter.GetData()'从MS-Access表
...

中读取数据每个myItem作为myDataTable.Rows中的DataRow。

tmpStr = nameItem( lastname)+ + nameItem( initials)

If myItem( sID)= sID然后
'做
结束如果

'打印tmpStr

下一个

因此,当字段中可能包含 dbNull 时,无需每次检查数据是否为dbNull,我如何使上述代码起作用在此问题中?

解决方案

我知道的唯一方法是对其进行测试,即使可以简化,也可以进行合并。

  If NOT IsDbNull(myItem( sID))AndAlso myItem( sID)= sId然后
'执行成功
ELSE
'失败
结束,如果

我在VB中编写的内容是这样,即使您使用的是多种语言,也是如此。



编辑



已清理以使用IsDbNull使其更具可读性


I want to generate some formatted output of data retrieved from an MS-Access database and stored in a DataTable object/variable, myDataTable. However, some of the fields in myDataTable cotain dbNull data. So, the following VB.net code snippet will give errors if the value of any of the fields lastname, intials, or sID is dbNull.

   dim myDataTable as DataTable
   dim tmpStr as String
   dim sID as Integer = 1

   ...
   myDataTable = myTableAdapter.GetData() ' Reads the data from MS-Access table
   ...

   For Each myItem As DataRow In myDataTable.Rows

    tmpStr = nameItem("lastname") + " " + nameItem("initials")

    If myItem("sID")=sID Then
        ' Do something
    End If

    ' print tmpStr

   Next

So, how do i get the above code to work when the fields may contain dbNull without having to check each time if the data is dbNull as in this question?

解决方案

The only way that i know of is to test for it, you can do a combined if though to make it easy.

If NOT IsDbNull(myItem("sID")) AndAlso myItem("sID") = sId Then
   'Do success
ELSE
   'Failure
End If

I wrote in VB as that is what it looks like you need, even though you mixed languages.

Edit

Cleaned up to use IsDbNull to make it more readable

这篇关于在vb.net中处理dbnull数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆