【发布时间】:2018-09-26 22:24:49
【问题描述】:
我正在尝试使用 C# 和 SQL 插入记录,但是当日期列为空时出现此错误,我搜索了类似的案例但尚未解决。它插入 01/01/1900,表中的列是 Datetime 任何想法或类似案例链接。
[HttpPost]
public HttpResponseMessage save (Education edu)
{
Dictionary<string, object> xmt = new Dictionary<string, object>();
xmt.Add("@Staff_Key", edu.Staff_Key);
xmt.Add("@Type_Education", edu.Type_Education);
xmt.Add("@Qual", edu.Qual);
xmt.Add("@Uni", edu.Uni);
xmt.Add("@Date_Issue", edu.Date_Issue.ToString() == null ? "Null" : edu.Date_Issue.ToString());
xmt.Add("@Notes", edu.Notes);
obj.ExecNonQuery(
@"insert into HR_DocEducation (Staff_Key,Type_Education,Qual,Uni,Date_Issue,Notes)
values (@Staff_Key,@Type_Education,@Qual,@Uni,@Date_Issue,@Notes) ",xmt);
HttpResponseMessage response = Request.CreateResponse(HttpStatusCode.Created);
return response;
【问题讨论】:
-
是的,它在类代码中可以为空
-
DateTime 不是可为空的类型。如果您不提供值,则它等于 DateTime.MinValue 这就是它采用 01/01/1900 并插入表中的原因。
-
请注意,检查
ToString()调用的结果是否为null几乎永远不会起作用。但是如果不了解更多细节就很难提供帮助 -edu.Date_Issue的类型是什么?
标签: c#