我有两个 DateTime 对象,BirthDate 和 HireDate。它们被正确格式化为字符串,当我将它们传递到我的数据访问层时,它们需要被解析为 DateTime 对象。
DateTime hD = DateTime.Parse(hire);
DateTime bD = DateTime.Parse(birth);
//incase of a datestring being passed through
dateStringPassed = "7/2/1969";
但有时,字符串hire
和birth
为 null 或 empty ""
,如果代码像这样运行,我会从 Parsing a empty string 得到 FormatException 错误。如何管理空解析并允许 DateTime(如果为空或 null)被接受为DBNull.Value
?
如果用户没有通过 DateTime 字符串,我仍然无法管理,然后解析使我的代码崩溃。
我的出生日期参数如下,如果为 null,则检查变量,然后使用 DBNull.Value。