【问题标题】:How do I save Object back to the database with a null value for a date parameter?如何使用日期参数的空值将对象保存回数据库?
【发布时间】:2011-09-13 04:12:58
【问题描述】:

我的 WPF 窗口上有一个可选的日期选择器。我的意思是在将 Employee 对象保存回数据库之前不需要选择日期。但是,当尝试在没有设置日期的情况下保存它时,我无法让保存工作。错误是:

SqlDateTime 溢出。必须在 1753 年 1 月 1 日凌晨 12:00:00 到 9999 年 12 月 31 日晚上 11:59:59 之间。

我做错了什么?

private DateTime _ExpectedPromotionDate;
public DateTime ExpectedPromotionDate
{
    get
    {
        return _ExpectedPromotionDate;
    }
    set
    {
        DateTime temp;
        if (DateTime.TryParse(Convert.ToString(_ExpectedPromotionDate), out temp))
        {
            _ExpectedPromotionDate = value;
        }
        else
        {
            _ExpectedPromotionDate = DBNull.Value;
            //throw new ApplicationException("Promotion Date is mandatory.");
        }
    }
}

顺便说一下,我的保存功能是这样的

public bool Save()
{
    try
    {
        Dac.ExecuteNonQuery("Manpower_InsertEmployee",
            Dac.Parameter(CN_LoginId, LoginId),
            Dac.Parameter(CN_StoreId, StoreId),
            Dac.Parameter(CN_FirstName, FirstName),
            Dac.Parameter(CN_LastName, LastName),
            Dac.Parameter(CN_EeRole, EeRole),
            Dac.Parameter(CN_RoleRank, RoleRank),
            Dac.Parameter(CN_IsUpNComing, IsUpNComing),
            Dac.Parameter(CN_ExpectedPromoDate, ExpectedPromotionDate),
            Dac.Parameter(CN_IsPotentialManager, IsPotentialManager));
        return true;
    }
    catch
    {
        //throw new Exception (String.Format("{0} {1}", ex.Message, ex.StackTrace));
        throw new Exception("There was an error during the save process.");

    }
}

【问题讨论】:

  • 列的数据库定义是什么?它允许空值吗?当您不选择日期时,_ExpectedPromotionDate 的计算结果是什么?
  • 你的:_ExpectedPromotionDate = DBNull.Value; 是否抛出异常,我想你会在这里得到异常。
  • 数据库确实允许空值,并且我为该参数添加了一个默认值。 _expectedPromotionDate 的计算结果为:parameterValue {1/1/0001 12:00:00 AM}。 DBNull.Value 没有编译,所以我什至没有机会看到它抛出异常。

标签: c# oop


【解决方案1】:

DateTime 不可为空。

日期时间?可以为空。 (或 Nullable 如果您更喜欢该语法) 如果用户没有选择任何值,请确保调用者将此值设置为 null。

private DateTime? _ExpectedPromotionDate;
public DateTime? ExpectedPromotionDate
{
    get
    {
        return _ExpectedPromotionDate;
    }
    set
    {

            _ExpectedPromotionDate = value;

    }
}






Dac.Parameter(CN_ExpectedPromoDate, ExpectedPromotionDate??(object)DbNull.Value),

【讨论】:

    【解决方案2】:

    Sql DateTime 列不能代表所有可能的 .NET DateTime 值。我怀疑你的 UI 选择器正在返回 DateTime.MinValue,它不能在 SQL DateTime 中表示。

    SQL DateTime 的有效值范围为 1/1/1753 12:00:00 AM12/31/9999 11:59:59 PM(如错误消息所示),而 .NET DateTime 的有效值范围为 01-01-0001 00:00:00 31-12-9999 23:59:59

    此外,setter 中的保护子句没有意义,您正在解析已有的值,如果成功,则将该字段设置为 value。此外,将DateTime 字段设置为DBNull.Value 是编译错误。我不知道您的场景中涉及的确切类型,但我怀疑您在 setter 中想要这样的东西:

    if (value >= SqlDateTime.MinValue && value < SqlDateTime.MaxValue) 
        _ExpectedPromotionDate = value;
    else 
        _ExpectedPromotionDate = DBNull.Value;
    

    【讨论】:

      【解决方案3】:

      我不认为该代码不会编译。 DateTime 类型与 DBNull 不兼容。试试这样的东西?还要确保数据库中的列可以为空。

      private DateTime? _ExpectedPromotionDate;
      public DateTime? ExpectedPromotionDate
      {
          get
          {
              return _ExpectedPromotionDate;
          }
          set
          {
               _ExpectedPromotionDate = value;
          }
      
      
      }
      
      public bool Save()
      {
          try
          {
              Dac.ExecuteNonQuery("Manpower_InsertEmployee",
                  Dac.Parameter(CN_LoginId, LoginId),
                  Dac.Parameter(CN_StoreId, StoreId),
                  Dac.Parameter(CN_FirstName, FirstName),
                  Dac.Parameter(CN_LastName, LastName),
                  Dac.Parameter(CN_EeRole, EeRole),
                  Dac.Parameter(CN_RoleRank, RoleRank),
                  Dac.Parameter(CN_IsUpNComing, IsUpNComing),
                  Dac.Parameter(CN_ExpectedPromoDate, ExpectedPromotionDate.HasValue ? ExpectedPromotionDate.Value : DBNull.Value),
                  Dac.Parameter(CN_IsPotentialManager, IsPotentialManager));
              return true;
          }
          catch
          {
              //throw new Exception (String.Format("{0} {1}", ex.Message, ex.StackTrace));
              throw new Exception("There was an error during the save process.");
      
          }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2017-03-09
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2015-06-17
        • 1970-01-01
        • 2023-03-13
        • 1970-01-01
        相关资源
        最近更新 更多