我正在寻找如何检查特定行的列数据。我发现这个:
对于每一行在tablePiglet.Rows' - 数据表如果不是DBNull.Value.Equals (row(CurrentLocation))然后 MessageBox.Show(Not Empty)否则 MessageBox.Show(Empty)结束如果我试图运行应用程序,代码不执行。我在 For Each 中添加了一个断点,但它刚刚结束。这是为什么?在CurrentLocation字段上还有一个数据,因此它应该显示为空。
我想要的是检查该行的 CurrentLocation 是否为空。 类似: SELECT CurrentLocation FROM tableFoo WHERE id = 1 然后检查它是否为空。 上面的代码是否正确?
解决方案如果你知道你只有一行,而你知道它,因为在你的db
SELECT CurrentLocation FROM tableFoo WHERE id = 1在这种情况下,你不需要循环,你知道,这是一行
如果tablePiglet.Rows.Count> 0 Then 如果IsDbNull(tablePiglet.Rows(0)(CurrentLocation))然后 MessageBox.Show(Empty)否则 MessageBox.Show )结束如果结束如果>
但如果你确实有很多行,请创建List
Dim l作为新的List(of String) 对于i在tablePiglet.Rows.count-1中的整数= 0 如果IsDbNull(tablePiglet.Rows(i)(CurrentLocation ))然后 l.Add(i.ToString()) end if next 如果l.Count> 0 Then MessageBox.Show(Empty rows are& string.Join(,,l.ToArray())它将列出所有具有空CurrentLocation
的行I am looking on how to check the column data of a specific row. I found this:
For Each row In tablePiglet.Rows ' - data table If Not DBNull.Value.Equals(row("CurrentLocation")) Then MessageBox.Show("Not Empty") Else MessageBox.Show("Empty") End If NextI tried to run the app, and the code does not execute. I added a breakpoint in For Each but it just ended. Why is that? There had been yet a data on CurrentLocation field, so it should display empty.
What I want is to check whether that row's CurrentLocation is empty or not. Something like : SELECT CurrentLocation FROM tableFoo WHERE id=1 then check if its empty or not. Is the code above the right thing to do? Or are there other better ways?
解决方案If you know that you only have a single row, and you know it because in your db only one row may have a certain id as you pointed here
SELECT CurrentLocation FROM tableFoo WHERE id=1In this case you don't need to loop, you KNOW, this is a single row
If tablePiglet.Rows.Count > 0 Then If IsDbNull(tablePiglet.Rows(0)("CurrentLocation")) Then MessageBox.Show("Empty") Else MessageBox.Show("Not Empty") End If End IfThat should do it
But if you do have many rows, ok, create List
Dim l as new List(of String) For i as integer = 0 in tablePiglet.Rows.count-1 If IsDbNull(tablePiglet.Rows(i)("CurrentLocation")) Then l.Add(i.ToString()) end if next if l.Count > 0 Then MessageBox.Show("Empty rows are " & string.Join(",", l.ToArray())It will list all the rows with empty "CurrentLocation"
更多推荐
检查列是否为空
发布评论