设置空的DateTime变量 [英] Set an empty DateTime variable

查看:216
本文介绍了设置空的DateTime变量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我会宣布这样一个空字符串变量:

I would declare an empty String variable like this:

    string myString = string.Empty;



是否有一个日期时间变量等效?

Is there an equivalent for a 'DateTime' variable ?

更新:

问题是我用这个日期时间作为一个参数在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 是一个您不能分配来,但正是对于这些情况(缺少值)值类型的 可空< 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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆