DBNull.Value问题 [英] DBNull.Value Question

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

问题描述

当我尝试使用它时(单位是我表中的一列): -

如果单位是DBNull.Value那么

返回1

否则

返回2

结束如果


我总是返回2个!即使Unit为NULL!

我想要一个案例,如果一个列是NULL,它应该返回''1,如果不是''2''

DBNull如何。价值工作?

When i try and use this (Where Unit is a column in my Table):-
If Unit Is DBNull.Value Then
Return "1"
Else
Return "2"
End If

I always have 2 returned! Even when Unit is NULL!
I want a case if a column is NULL the it xhould return ''1 and if not ''2''
How does DBNull.Value work?

推荐答案

不要使用IS在针对DBNull.Value进行测试时,请使用=。


使用" ="当针对值测试值并使用IS时在测试对象的

对象时。


或者,重新编写代码以使用IsDBNull()函数---> dim x as

boolean = IsDBNull(Unit)


在你的代码中,Unit是列对象还是列的值?如果你想要

来测试DBNull.Value,你需要Unit作为列的值,而

不是列对象本身。

Patrick.O.Ige <帕********* @ discussions.microsoft.com>在消息中写道

news:77 ********************************** @ microsof t.com ...
Don''t use "IS" when testing against DBNull.Value, use "=".

Use "=" when testing a value against a value and use "IS" when testing an
object against an object.

Or, re-phrase your code to use the IsDBNull() function ---> dim x as
boolean = IsDBNull(Unit)

In your code, is Unit a column object or the value of a column? If you want
to test against DBNull.Value, you need Unit to be the value of a column and
not the column object itself.
"Patrick.O.Ige" <Pa*********@discussions.microsoft.com> wrote in message
news:77**********************************@microsof t.com...
当我尝试使用它(其中Unit是我表中的一列)时: -
如果Unit为DBNull.Value则
返回" 1
否则
返回2
结束如果

我总是有2回复!即使Unit为NULL!
我想要一个案例,如果列是NULL,它应该返回''1,如果不是''2''
DBNull.Value如何工作?
When i try and use this (Where Unit is a column in my Table):-
If Unit Is DBNull.Value Then
Return "1"
Else
Return "2"
End If

I always have 2 returned! Even when Unit is NULL!
I want a case if a column is NULL the it xhould return ''1 and if not ''2''
How does DBNull.Value work?



Scott:

这在C#中是正确的,但在VB.Net中,你必须在检查时使用IS

DbNull.Value ...使用=将导致编译错误。


Patrick:

你说Unit是你桌子的一列吗?一列不能是DbNull ...只有一行的特定值。我想看看上面的代码(Unit得到

设置)。另外,在旁注中,您可以重写您的代码,如:


如果单位是DbNull.Value那么

返回1

结束如果

返回" 2"


则不需要else,因为第一个if语句将

如果为true则会自动退出该函数(因为

返回)...但实际上这更像是一个样式问题:)无论如何,显示更多

代码:)


Karl


-

我的ASP.Net教程
http://www.openmymind.net/

Scott M. < S - *** @ nospam.nospam>在留言中写道

news:eo ************** @ TK2MSFTNGP14.phx.gbl ...
Scott:
This is true in C#, but in VB.Net you have to use IS when checking against
DbNull.Value...using = will result in a compilation error.

Patrick:
You say Unit is a column of your table? A column can''t be DbNull...only a
specific value of a row. I''d like to see code above this (where Unit gets
set). Also, on a side note, you can rewrite your code like:

If Unit Is DbNull.Value Then
return "1"
end if
return "2"

there''s no need for the else, since the first if statement will
automatically break out of the function if true (because of the
return)...but really that''s more a style issue :) Anyways, show up more
code :)

Karl

--
MY ASP.Net tutorials
http://www.openmymind.net/
"Scott M." <s-***@nospam.nospam> wrote in message
news:eo**************@TK2MSFTNGP14.phx.gbl...
不要用" IS"在针对DBNull.Value进行测试时,请使用=。

使用" ="当针对值测试值并使用IS时在针对对象测试
对象时。

或者,重新编写代码以使用IsDBNull()函数---> dim x as
boolean = IsDBNull(Unit)

在你的代码中,Unit是列对象还是列的值?如果
想要对DBNull.Value进行测试,则需要Unit为
列的值,而不是列对象本身。

Patrick.O.Ige ; <帕********* @ discussions.microsoft.com>在消息中写道
新闻:77 ********************************** @ microsof t.com。 ..
Don''t use "IS" when testing against DBNull.Value, use "=".

Use "=" when testing a value against a value and use "IS" when testing an
object against an object.

Or, re-phrase your code to use the IsDBNull() function ---> dim x as
boolean = IsDBNull(Unit)

In your code, is Unit a column object or the value of a column? If you want to test against DBNull.Value, you need Unit to be the value of a column and not the column object itself.
"Patrick.O.Ige" <Pa*********@discussions.microsoft.com> wrote in message
news:77**********************************@microsof t.com...
当我尝试使用它(其中Unit是我表中的一列)时: -
如果Unit是DBNull.Value那么
返回" 1"
其他
返回2
结束如果

我总是有2回复!即使Unit为NULL!
我想要一个案例,如果列是NULL,它应该返回''1,如果不是''2''
DBNull.Value如何工作?
When i try and use this (Where Unit is a column in my Table):-
If Unit Is DBNull.Value Then
Return "1"
Else
Return "2"
End If

I always have 2 returned! Even when Unit is NULL!
I want a case if a column is NULL the it xhould return ''1 and if not ''2''
How does DBNull.Value work?




回复的Thx ...

我所拥有的是: -

受保护的函数图像(ByVal单位作为对象)作为字符串

如果单位是DBNull.Value则

返回Bad

Else

返回好

结束如果

结束功能

单位是我表中的一列,即。单位包含值 5拼接

面包"

我的问题是我需要在我的SQL语句中使用ISNULL吗?



Scott M.写道:
Thx for the reply...
What i have is this:-
Protected Function Image(ByVal Unit As Object) As String
If Unit Is DBNull.Value Then
Return "Bad"
Else
Return "Good"
End If
End Function
Unit is a column in my table ie. the unitd contains value " 5 pieced of
bread "
My Question is would i need to use ISNULL in my SQL statement?


"Scott M." wrote:
不要使用IS在针对DBNull.Value进行测试时,请使用=。

使用" ="当针对值测试值并使用IS时在针对对象测试
对象时。

或者,重新编写代码以使用IsDBNull()函数---> dim x as
boolean = IsDBNull(Unit)

在你的代码中,Unit是列对象还是列的值?如果你想要对DBNull.Value进行测试,你需要Unit作为列的值,而不是列对象本身。

" Patrick.O.Ige" ; <帕********* @ discussions.microsoft.com>在消息中写道
新闻:77 ********************************** @ microsof t.com。 ..
Don''t use "IS" when testing against DBNull.Value, use "=".

Use "=" when testing a value against a value and use "IS" when testing an
object against an object.

Or, re-phrase your code to use the IsDBNull() function ---> dim x as
boolean = IsDBNull(Unit)

In your code, is Unit a column object or the value of a column? If you want
to test against DBNull.Value, you need Unit to be the value of a column and
not the column object itself.
"Patrick.O.Ige" <Pa*********@discussions.microsoft.com> wrote in message
news:77**********************************@microsof t.com...
当我尝试使用它(其中Unit是我表中的一列)时: -
如果Unit是DBNull.Value那么
返回" 1"
其他
返回2
结束如果

我总是有2回复!即使Unit为NULL!
我想要一个案例,如果列是NULL,它应该返回''1,如果不是''2''
DBNull.Value如何工作?
When i try and use this (Where Unit is a column in my Table):-
If Unit Is DBNull.Value Then
Return "1"
Else
Return "2"
End If

I always have 2 returned! Even when Unit is NULL!
I want a case if a column is NULL the it xhould return ''1 and if not ''2''
How does DBNull.Value work?




这篇关于DBNull.Value问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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