【发布时间】: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 没有编译,所以我什至没有机会看到它抛出异常。