设置一个空的DateTime变量 [英] Set an empty DateTime variable
问题描述
我将声明一个空的String变量,如下所示:
I would declare an empty String variable like this:
string myString = string.Empty;
有一个DateTime变量的等价物吗?
Is there an equivalent for a 'DateTime' variable ?
更新:
问题是我使用这个'DateTime'作为SQL中StoredProcedure的参数。
例如:
The problem is I use this 'DateTime' as a parameter for a 'StoredProcedure' in SQL. E.g:
DateTime? someDate = null;
myCommand.Parameters.AddWithValue("@SurgeryDate", someDate);
当我运行此代码时,异常被捕获告诉我StoredProcedure预期'@SurgeryDate'参数。
但是我提供了。
任何想法为什么?
When I run this code an exception is catched telling me the 'StoredProcedure' expected a '@SurgeryDate' parameter. But i provided it. Any idea why?
推荐答案
由于 DateTime
是值类型,您不能为其分配 null
,但完全针对这些情况(缺少值) Nullable< T>
被引入 - 使用可空的 DateTime
代替:
Since DateTime
is a value type you cannot assign null
to it, but exactly for these cases (absence of a value) Nullable<T>
was introduced - use a nullable DateTime
instead:
DateTime? myTime = null;
这篇关于设置一个空的DateTime变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!