我有兩個DateTime對象,BirthDate和HireDate。它們被正確格式化爲一個字符串,當我將它們傳遞給我的數據訪問層時,它們需要被解析成一個DateTime對象。如何管理解析空值對象的DateTime用於ADO.NET作爲DBNULL
DateTime hD = DateTime.Parse(hire);
DateTime bD = DateTime.Parse(birth);
//incase of a datestring being passed through
dateStringPassed = "7/2/1969";
但有時,串hire
和birth
爲空或空""
,如果代碼運行這樣的,我是從解析空字符串得到出現FormatException錯誤。如何管理空分析並允許將DateTime(如果爲空或空)接受爲DBNull.Value
?
我仍然無法管理用戶不通過DateTime字符串,然後解析崩潰我的代碼。
我的出生日期參數如下,並檢查變量是否爲空,然後使用DBNull.Value。
你能寫一個DateTime bD = DateTime.Parse(出生)的例子嗎?在最後使用'?'還會爲'int'工作嗎? – GivenPie
@GivenPie - 是的,它會用於'int'和任何值類型。 – Oded
很酷,謝謝Oded! – GivenPie