如何检查它是否为空?
我从数据库中检索数据,如下所示。如何检查从数据库检索的值是否为空?
Private Function GetBatch() As DataSet
Dim dataset As New DataSet
Dim adapter As Data.SqlClient.SqlDataAdapter
Dim cn As New System.Data.SqlClient.SqlConnection(connectionstring())
GetBatchCommand.Connection = cn
adapter = New Data.SqlClient.SqlDataAdapter(GetBatchCommand)
adapter.Fill(dataset)
Return dataset
End Function
Dim dataset As New DataSet
dataset = GetBatch()
With dataset.Tables(0)
Dim PersonID As String = .Rows(int).Item("personId")
我想检查 personID 是否为空。怎么办呢?
I retrieve data from database like below. How do I check whether the value retrieved from database is null?
Private Function GetBatch() As DataSet
Dim dataset As New DataSet
Dim adapter As Data.SqlClient.SqlDataAdapter
Dim cn As New System.Data.SqlClient.SqlConnection(connectionstring())
GetBatchCommand.Connection = cn
adapter = New Data.SqlClient.SqlDataAdapter(GetBatchCommand)
adapter.Fill(dataset)
Return dataset
End Function
Dim dataset As New DataSet
dataset = GetBatch()
With dataset.Tables(0)
Dim PersonID As String = .Rows(int).Item("personId")
I'd like to check whether personID is null. How do do that?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
尝试 DataRow 的 IsNull 方法 检查 null 值:
或者使用 IsDBNull 方法 :
或者手动检查如果该值等于 DBNull :
Try DataRow's IsNull method to check null values :
Or use IsDBNull method :
Or manually Check if the value equals DBNull :
DBNull
DBNull
您还可以使用 Convert.DbNull 常量。
You can also use the Convert.DbNull constant.
在将值分配给 PersonID 之前,您必须检查该值是否为空,
如下所示:
我建议将该代码段提取到辅助方法中,该方法获取给定列的值或默认值。
You have to check if the value is null before you assign it to PersonID
like:
I would recommend to extract that piece of code into a helper method which gets either the value or a default for a given column.
如果表没有行,则可能会发生这种情况,此时 ds.Table(0).Rows(int).Item("personId") 将返回空引用异常,
因此您必须使用两个条件,
我认为它会解决您的问题。 .可能仅存在较小的语法变化
This situation can occur if table with no rows, that time ds.Table(0).Rows(int).Item("personId") will return null reference exception
so you have to use two conditions
I think It will solve your issue..just minor syntax variation may be present