在参数传递一个空到DateTime字段中存储过程 [英] Pass a NULL in a parameter to a DateTime field in a stored procedure
问题描述
我有一个存储过程使用我公司供应的参数,更新数据库,但我有传递NULL存储过程麻烦
I have a stored procedure which updates a database using the parameters I supply but I'm having trouble passing a NULL to the stored procedure
我需要做的NULL字段是DateTime字段
The field I need to make NULL is a DateTime field
DB.Parameters.AddWithValue("@date", NULL)
这给我的错误
NULL未声明。 'NULL'不变的是不再支持;使用'System.DBNull'代替
'NULL' is not declared. 'Null' constant is no longer supported; use 'System.DBNull' instead
所以,我想
DB.Parameters.AddWithValue("@date", DBNull.Value.ToString())
但是,这产生值 1900-01-01 00:00:00.000
中,因为它的传球列,
到外地
But this produces the value 1900-01-01 00:00:00.000
in the column as it's passing a ""
to the field
我也试过
DB.Parameters.AddWithValue("@date", DBNull.Value)
但它会产生这个错误
But it produces this error
类型的'System.DBNull'值不能被转换成字符串''。
Value of type 'System.DBNull' cannot be converted to 'String'.
有没有人有什么想法?
推荐答案
尝试这样的事情,使用 添加
而非 AddWithValue
:
Try something like this, using Add
rather than AddWithValue
:
DB.Parameters.Add("@date", SqlDbType.DateTime).Value = DBNull.Value;
这篇关于在参数传递一个空到DateTime字段中存储过程的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!