简体   繁体   English

无法从“ System.DateTime输出?”中进行转换? 退出System.DateTime

[英]cannot convert from 'out System.DateTime?' to 'out System.DateTime

  if(startDate == "")
  {
      DateTime? startDateParser = null;
      DateTime.TryParse(startDate, out startDateParser);
  }

if the string startDate is empty i would like it to output a null however it complains 如果字符串startDate为空,我希望它输出一个null,但是它抱怨

cannot convert from 'out System.DateTime?' 无法从“ System.DateTime输出?”中进行转换? to 'out System.DateTime' 移出System.DateTime

parsing the null into a sql statement which the parameter accepts nulls however gets parsed in as 01/01/0001 将null解析为该参数接受null的sql语句,但是解析为01/01/0001

command.Parameters.AddWithValue("@StartDate", (r.StartDate == null ? DBNull.Value : (object)r.StartDate));

any help will be greatly appreciated 任何帮助将不胜感激

A System.DateTime cannot be null. System.DateTime不能为null。 If you require a parameter of this type to allow nulls, you can do as your error suggests and use System.DateTime? 如果您需要此类型的参数以允许为空,则可以按照错误提示进行操作,并使用System.DateTime? which allows null values. 允许空值。

SO has many questions in relation to this topic, such as the following: datetime-null-value 因此,与该主题相关的问题很多,例如: datetime-null-value

Taken from the accepted answer: 从接受的答案中获取:

For normal DateTimes, if you don't initialize them at all then they will match DateTime.MinValue , because it is a value type rather than a reference type. 对于普通的DateTimes,如果根本不初始化它们,则它们将匹配DateTime.MinValue ,因为它是一个值类型而不是引用类型。

You can also use a nullable DateTime, like this: 您还可以使用可为空的DateTime,如下所示:

DateTime? MyNullableDate;

Or the longer form: 或更长的形式:

Nullable<DateTime> MyNullableDate;

And, finally, there's a built in way to reference the default of any type. 最后,有一种内置的方式可以引用任何类型的默认值。 This returns null for reference types, but for our DateTime example it will return the same as DateTime.MinValue : 对于引用类型,此方法返回null ,但是对于我们的DateTime示例,它将返回与DateTime.MinValue相同的结果:

default(DateTime)

Use this code to resolve "cannot convert from 'out System.DateTime?' to 'out System.DateTime'" 使用此代码解决“无法从'out System.DateTime?' to 'out System.DateTime'"转换'out System.DateTime?' to 'out System.DateTime'" 'out System.DateTime?' to 'out System.DateTime'" problem. 'out System.DateTime?' to 'out System.DateTime'"问题。

DateTime tmpDate;
DateTime? dateValue = DateTime.TryParse(startDate, out temp) ? temp : (DateTime?)null;

Can use the date value you parse for the result variable. 可以将您解析的日期值用作result变量。 You can use either null or a minimum value. 您可以使用null或最小值。

object resultVal = dateValue.HasValue ? dateValue.Value : DateTime.MinValue; // or DBNull.Value

command.Parameters.AddWithValue("@StartDate",resultVal  );

Remember, with out you're giving the method your variable to manipulate. 记住,在out方法的情况下, 您要给变量提供要操作的方法。 A DateTime struct is an 8 byte value type. DateTime结构是8字节的值类型。 Variables for holding such values are 8 bytes and are expected to directly contain the value. 用于保存此类值的变量为8个字节,并且应直接包含该值。

A DateTime? DateTime? is a nullable DateTime - which is also a struct. 是可以为null的DateTime也是一个结构。 But a Nullable<T> always contains an instance of the underlying and an extra bool . 但是Nullable<T>始终包含基础实例一个额外的bool Which means that it will be at least 9 bytes in size (in practice, almost certainly 12 in most common implementations). 这意味着它将至少为9个字节(实际上,在大多数常见实现中几乎可以确定为12个字节)。 A variable for such a type is (say) 12 bytes and contains the underlying value and the bool (and the bool may be "first" in the layout) 这种类型的变量为(例如)12个字节,包含基础值和boolbool在布局中可能是“第一个”)

Now do you see why, if you're supplying the variable for some other method to manipulate, it has to be exactly the right sort of variable in order for the code inside that method to work with it correctly. 现在,您知道为什么,如果要为其他方法提供变量以供操作,则它必须是正确正确的变量,才能使该方法中的代码正确使用它。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM