C# 如何管理将 DateTime 与 ADO.NET 一起使用的空对象解析为 DBNULL
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/13555505/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to manage parsing an null object for DateTime to be used with ADO.NET as DBNULL
提问by GivenPie
I have two DateTime objects, BirthDate and HireDate. They are correctly formatted as a string and when I pass them through to my data access layer, they need to be parsed into a DateTime object.
我有两个 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";
But sometimes, the strings hireand birthare null or empty "", if the code is run like this, I get a FormatException error from Parsing a empty string. How can I manage empty parses and allow the DateTime, if empty or null, be accepted as DBNull.Value?
但有时,字符串hireandbirth为 null 或 empty "",如果代码像这样运行,我会从解析空字符串中收到 FormatException 错误。如何管理空解析并允许 DateTime(如果为空或 null)被接受为DBNull.Value?
I still cannot manage incase the user does not pass through a DateTime string, then the parse crashes my code.
我仍然无法管理 incase 用户没有通过 DateTime 字符串,然后解析使我的代码崩溃。
My parameter for birth date is as follows and checks the variable if null, then use DBNull.Value.
我的出生日期参数如下,如果为空,则检查变量,然后使用 DBNull.Value。
采纳答案by Oded
You need to use nullabledate times - the shortcut syntax would be DateTime?(note the ?at the end).
您需要使用可为空的日期时间 - 快捷语法是DateTime?(注意?末尾)。
DateTime? hD = null;
if(!string.IsNullOrWhitespace(hire )) // string.IsNullOrEmpty if on .NET pre 4.0
{
hD = DateTime.Parse(hire);
}
You can test for hD.HasValueand if it doesn't use DbNull.Valueinstead.
您可以测试hD.HasValue它是否不使用DbNull.Value。
回答by Honza Brestan
The Parsemethod can't handle empty strings, but you can use nullable DateTime and do something like this:
该Parse方法无法处理空字符串,但您可以使用可为空的 DateTime 并执行以下操作:
DateTime? hD = String.IsNullOrEmpty(hire) ? (DateTime?)null : DateTime.Parse(hire)
But even safer would be using TryParseinstead:
但更安全的是使用TryParse:
DateTime? hD = null;
DateTime.TryParse(hire, out hD);
Then for storing this value, you can test hD.HasValue:
然后为了存储这个值,你可以测试hD.HasValue:
if(hD.HasValue) { /* use hD */ }
else { /* else use DBNull.Value */ }
Since C# 7, you can use shorter syntax for inline out parameters and you can avoid nullable type altogether:
从 C# 7 开始,您可以对内联输出参数使用更短的语法,并且可以完全避免可空类型:
if (DateTime.TryParse(hire, out var hD)) { /* use hD */ }
else { /* use DBNull.Value */ }
回答by Jens Granlund
If you use this method, any thing that is not a correct date will return a DBNull.Value:
如果您使用此方法,任何日期不正确的内容都将返回DBNull.Value:
/// <summary>
/// Parses a date string and returns
/// a DateTime if it is a valid date,
/// if not returns DBNull.Value
/// </summary>
/// <param name="date">Date string</param>
/// <returns>DateTime or DBNull.Value</returns>
public static object CreateDBDateTime(string date)
{
DateTime result;
if (DateTime.TryParse(date, out result))
{
return result;
}
return DBNull.Value;
}

