检查列是否为空 [英] Check if Column is empty

查看:145
本文介绍了检查列是否为空的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在寻找如何检查特定行的列数据。我发现这个:

 对于每一行在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
    Next

I 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=1

In 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 If

That 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"

这篇关于检查列是否为空的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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