DataRow.IsNull() 没有 return return 正确 vb.net
DataRow.IsNull() does not return return correctly vb.net
更新:
只看调试器,它告诉我该行不为空。
我正在使用 DataRow.IsNull 方法来检查该行是否为空。现在,如果该行为空,则我需要输入默认字符串,否则 return 行中的值。
但是,这似乎没有按预期工作?我错过了什么吗?
问题:
它只为某些行而不是其他行放置默认字符串。
不知道自己是否理解正确?谁能指出我正确的方向。
请注意,数据库中的 DataSet
是 return,我会循环遍历每一行。
代码:
For i As Integer = 0 To ds.Tables(0).Rows.Count - 1
Dim row = ds.Tables(0).Rows(i)
Dim obj As Foo = New Foo
With
{.FooAccNum = If(row.IsNull("accnum"), "Not Set", row.Field(Of String)("accnum")),
.FooPartsNumber = If(row.IsNull("partsnumber"), "Not Set", row.Field(Of String)("partsnumber")),
.FooGroup = If(row.IsNull("group"), "", row.Field(Of String)("group")),
.FooCustomer = If(row.IsNull("customer"), "No record exists", row.Field(Of String)
("customer"))
FooList.Add(obj )
Next
返回的数据集:
如您所见,客户列为空。
查看:
正如您在我的网格视图中看到的那样,它没有将客户的某些行值设置为 "No record exists"。这是为什么?
可能是这些值不为空而只是空字符串的情况 - 然后它们在 UI 中显示相同但未通过 IsNull
检查。附加值检查应该可以帮助您处理它们:
.FooCustomer = If(row.IsNull("customer") Or String.IsNullOrWhiteSpace(row.Field(Of String)("customer")),
"No record exists",
row.Field(Of String)("customer")
更新:
只看调试器,它告诉我该行不为空。
我正在使用 DataRow.IsNull 方法来检查该行是否为空。现在,如果该行为空,则我需要输入默认字符串,否则 return 行中的值。
但是,这似乎没有按预期工作?我错过了什么吗?
问题: 它只为某些行而不是其他行放置默认字符串。
不知道自己是否理解正确?谁能指出我正确的方向。
请注意,数据库中的 DataSet
是 return,我会循环遍历每一行。
代码:
For i As Integer = 0 To ds.Tables(0).Rows.Count - 1
Dim row = ds.Tables(0).Rows(i)
Dim obj As Foo = New Foo
With
{.FooAccNum = If(row.IsNull("accnum"), "Not Set", row.Field(Of String)("accnum")),
.FooPartsNumber = If(row.IsNull("partsnumber"), "Not Set", row.Field(Of String)("partsnumber")),
.FooGroup = If(row.IsNull("group"), "", row.Field(Of String)("group")),
.FooCustomer = If(row.IsNull("customer"), "No record exists", row.Field(Of String)
("customer"))
FooList.Add(obj )
Next
返回的数据集:
如您所见,客户列为空。
查看:
正如您在我的网格视图中看到的那样,它没有将客户的某些行值设置为 "No record exists"。这是为什么?
可能是这些值不为空而只是空字符串的情况 - 然后它们在 UI 中显示相同但未通过 IsNull
检查。附加值检查应该可以帮助您处理它们:
.FooCustomer = If(row.IsNull("customer") Or String.IsNullOrWhiteSpace(row.Field(Of String)("customer")),
"No record exists",
row.Field(Of String)("customer")