如何将 vb.net 中日期的空值传递给 sql 存储过程?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/649426/
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
How can I pass a null value for date in vb.net to sql stored procedure?
提问by Nandini
My application is asp.net with vb.
我的应用程序是带有 vb 的 asp.net。
In my page I have a textbox for passing date. If I didn't enter date and on clicking submit, I have to pass null value to the stored procedure.
在我的页面中,我有一个用于传递日期的文本框。如果我没有输入日期并点击提交,我必须将空值传递给存储过程。
I tried following codes such as DBNull.Value
and DateTime.MinValue
. In that case instead of null, "#12:00:00#"
is passing. I have to pass Null.
我尝试了以下代码,例如DBNull.Value
和DateTime.MinValue
。在这种情况下,而不是 null,"#12:00:00#"
正在传递。我必须通过 Null。
回答by Binoj Antony
Just assign the parameter the value DBNull.Value
只需为参数分配值 DBNull.Value
[EDIT]
[编辑]
If you are using SqlParameter (thanks Jon) then you can set it like this if it gives any error
如果您使用的是 SqlParameter(感谢 Jon),那么如果出现任何错误,您可以像这样设置它
parameter.Value = DBNull.Value
回答by Peternac26
Dim pflt_dateto As SqlParameter = cmd.CreateParameter
pflt_dateto.Direction = ParameterDirection.Input
pflt_dateto.DbType = SqlDbType.Int
pflt_dateto.IsNullable = True
pflt_dateto.ParameterName = "flt_dateto"
If String.IsNullOrEmpty(dateto) Then
pflt_dateto.SqlValue = DBNull
Else
pflt_dateto.SqlValue = dateto
End If
cmd.Parameters.Add(pflt_dateto)
Since you set up the parameter nullable, the error will disappear. Now the issue is that .NET pass null integer as 0 so we need to deal with that.
由于您将参数设置为可空,因此错误将消失。现在的问题是 .NET 将空整数作为 0 传递,所以我们需要处理它。
Chao
赵