VB.NET 中的可空类型?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1123844/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Nullable Types in VB.NET?
提问by KuldipMCA
Can Nullable Types be used in VB.NET? If so, is it possible to have a Nullable Integer that I can use with a field that accepts NULL in SQL Server? Examples would be appreciated.
可以在 VB.NET 中使用可空类型吗?如果是这样,是否有可能有一个 Nullable Integer 可以与 SQL Server 中接受 NULL 的字段一起使用?示例将不胜感激。
回答by stevehipwell
VB.Net does have nullable types, they can be declared in the following two different ways.
VB.Net 确实有可空类型,它们可以通过以下两种不同的方式声明。
Dim iNullable As Integer?
or
或者
Dim iNullable As Nullable(Of Integer)
回答by Konrad Rudolph
Nullable types can be used in VB.NET same as in C#.
You can't assign Null
, Nothing
or DBNull
to a bare Integer
in VB but you canuse a Nullable(Of Integer)
instead:
您不能将Null
,Nothing
或分配给VB 中DBNull
的裸Integer
,但您可以使用 aNullable(Of Integer)
代替:
Dim x As Integer? = Nothing
Alternatively, it sometimes (but rarely) makes sense to box the integer value in an Object
(which can be Nothing
).
或者,有时(但很少)将整数值装箱Object
(可以是Nothing
)是有意义的。
回答by Marc Gravell
Integers (System.Int32
etc) in .NET are not directly nullable; however, there is Nullable-of-T
that allows you to make any value-type nullable-ish. Note that you may have to check the database against DBNull
rather than null
/Nothing
.
System.Int32
.NET 中的整数(等)不能直接为空;但是,有Nullable-of-T
一个允许您使任何值类型为 nullable-ish。请注意,您可能必须检查数据库DBNull
而不是null
/ Nothing
。
So yes, you can do something very similar.
所以是的,你可以做一些非常相似的事情。
回答by Nicholas
In a number of cases Nothing
will get converted to the default value. To use Nothing
the same way you would use null
you need to cast it to the correct nullable type.
在许多情况下Nothing
会被转换为默认值。要使用Nothing
与使用相同的方式,null
您需要将其强制转换为正确的可为空类型。
Dim str As String
Dim int As Nullable(Of Integer) ' or use As Integer?
Dim reader As SqlDataReader
Dim colA As Integer = reader.GetOrdinal("colA")
Dim colB As Integer = reader.GetOrdinal("colB")
str = If(reader.IsDBNull(colA), DirectCast(Nothing, String), reader.GetString(colA))
int = If(reader.IsDBNull(colB), DirectCast(Nothing, Nullable(Of Integer)), reader.GetInt32(colB))
回答by Vadim Levkovsky
Only way I know to enter NULL's into db from client code (as asked in comments below question) is to use default values in SQL equal to NULL.
我知道从客户端代码将 NULL 输入 db 的唯一方法(如问题下面的评论中所问)是在 SQL 中使用等于 NULL 的默认值。
So in Update/Insert stored procedure you can use something like:
因此,在更新/插入存储过程中,您可以使用以下内容:
create proc dbo.UpdateSomeTable
@Id int,
@Status bit = NULL
as
begin
update dbo.SomeTable
set Status = @Status
where Id = @Id
end
and somewhere in code
和代码中的某个地方
Dim pars As List(Of SqlParameter) = New List(Of SqlParameter)
With pars
.Add(New SqlParameter("@Id", LogbookNoteId))
If Flag Then
.Add(New SqlParameter("@Status", Flag))
End If
End With
ExecuteNonQuery("dbo.UpdateSomeTable", pars, CommandType.StoredProcedure)
回答by Craig
Nope. You will have to modify the insert or update query to not add (or update) that value to get a null in the database.
不。您将不得不修改插入或更新查询以不添加(或更新)该值以在数据库中获取空值。