abh*_*bhi 3 c# datetime nullable
我目前正在将日期字段设置为Datetime.Now,这是通过数据库上的SQL查询完成的.
我做了
SELECT NVL(mydatefield, sysdate) FROM myView;
Run Code Online (Sandbox Code Playgroud)
这不是我能想到的最优雅的方法.更好的做法是检查NULL.我发现DateTime类不支持我用于int值的语法.
你看到了什么方法被使用?你有什么偏好?如何使DateTime处理空值?我不希望DateTime.ParseExact我的前端代码抛出异常.
DateTime?是可以为空的DateTime.您还可以使用default(DateTime)查找未设置的DateTime字段.最后一个选项是使用DateTime.Min或DateTime.Max作为特殊值.
而不是DateTime.ParseExact你可以使用DateTime.TryParse.
string input = "2010-10-04";
DateTime result;
bool success = DateTime.TryParse(input, out result);
Run Code Online (Sandbox Code Playgroud)