将空值传递给 .vb.net 中的整数变量
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/18247455/
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
pass null value to integer variable in .vb.net
提问by user2674855
i have integer variable like this:
我有这样的整数变量:
Dim valetid as integer
If cvalettype.Checked Then
valetid = RecordID("vtid", "VType_tbl", "Vtype", cmbvalettype.Text)
Else
valetid = 0
End If
if condition coming to else case the valetid taking 0 value.that is saving in database as 0 only. if condition coming to else case i want to save my valetid in mydatabase as Null(now in my database saving valetid as 0).in my database Valetid datatype i declared as int.how i can do this?
如果条件出现在 else 情况下,valetid 取 0 值。即仅在数据库中保存为 0。如果出现其他情况,我想将我的 valetid 在我的数据库中保存为 Null(现在在我的数据库中将 valetid 保存为 0)。在我的数据库 Valetid 数据类型中,我声明为 int。我可以这样做吗?
回答by Douglas Barbin
First, your integer variable has to be nullable in both the VB code and the database. Assuming that the database allows nulls for this field, you can do something like this:
首先,您的整数变量在 VB 代码和数据库中都必须可以为空。假设数据库允许此字段为空,您可以执行以下操作:
Dim valetid as Integer?
If cvalettype.Checked Then
valetid = RecordID("vtid", "VType_tbl", "Vtype", cmbvalettype.Text)
Else
valetid = Nothing
End If
' Do stuff with your valetid variable here
Or, as Neolisk prefers it:
或者,正如 Neolisk 喜欢的那样:
Dim valetid as Nullable(Of Integer)
If cvalettype.Checked Then
valetid = RecordID("vtid", "VType_tbl", "Vtype", cmbvalettype.Text)
Else
valetid = Nothing
End If
' Do stuff with your valetid variable here
回答by Lectere
I would;
我会;
If cvalettype.Checked Then
valetid = RecordID("vtid", "VType_tbl", "Vtype", cmbvalettype.Text)
Else
valetid = nothing
End If
And then later you can test;
然后你可以测试;
if valetid isnot nothing then
'write valetid to database
else
'write dbnull to database
end if
回答by Christian Sauer
Is your integer declared as nullable integer? If so, you could simply do this:
您的整数是否声明为可空整数?如果是这样,你可以简单地这样做:
Dim valetid as integer
If cvalettype.Checked Then
valetid = RecordID("vtid", "VType_tbl", "Vtype", cmbvalettype.Text)
Else
valetid.hasvalue =false
End If

