我正在开始使用Linq to sql。我想插入一条记录。这是我的代码。
FMCSA_USER_LOG _MyUserLog = new FMCSA_USER_LOG();
_MyUserLog.USER_ID = model.UserID;
_MyUserLog.ACTIVITY_TYPE_ID = model.ActivityTypeID;
_MyUserLog.ACTIVITY_TARGET = model.ActivityTarget;
_MyUserLog.DESCRIPTION = model.Description;
_MyUserLog.ACTIVITY_TIME = DateTime.Today;
_MyUserLog.ACTIVITY_TYPE_ID = null;
_MyUserLog.DESCRIPTION = null;
db.FMCSA_USER_LOGs.InsertOnSubmit(_MyUserLog);
db.SubmitChanges();
我在SubmitChanges上收到了这个例外:
SqlDateTime溢出。必须在之间 1/1/1753 12:00:00 AM和12/31/9999 晚上11:59:59。
从我对这个问题的解读中,当你有一个日期字段并且它设置得太低时会发生这种情况。然而,表格中唯一的日期字段是我今天的日期。
这是记录;我不明白我做错了什么。
CREATE TABLE [dbo].[FMCSA_USER_LOG](
[ID] [int] IDENTITY(1,1) NOT NULL,
[USER_ID] [int] NULL,
[ACTIVITY_TIME] [datetime2](7) NOT NULL,
[ACTIVITY_TYPE_ID] [int] NULL,
[ACTIVITY_TARGET] [varchar](50) NULL,
[DESCRIPTION] [varchar](8000) NULL,
CONSTRAINT [PK_FMCSA_USER_LOG] PRIMARY KEY CLUSTERED
([ID] ASC)
)
答案 0 :(得分:0)
我花了一段时间将.net日期时间变成了datetime2列。 我得到了这个方法:
FMCSA_USER_LOG _MyUserLog = new FMCSA_USER_LOG();
var MyAwesomeDateTime = DateTime.Today.ToString("yyyy-MM-dd HH:mm:ss:fff");
var MyAwesomeDateTime2 = DateTime.ParseExact(MyAwesomeDateTime , "yyyy-MM-dd HH:mm:ss:fff", CultureInfo.InvariantCulture);
_MyUserLog.USER_ID = model.UserID;
_MyUserLog.ACTIVITY_TYPE_ID = model.ActivityTypeID;
_MyUserLog.ACTIVITY_TARGET = model.ActivityTarget;
_MyUserLog.DESCRIPTION = model.Description;
_MyUserLog.ACTIVITY_TIME = MyAwesomeDateTime2;
_MyUserLog.ACTIVITY_TYPE_ID = null;
_MyUserLog.DESCRIPTION = null;
db.FMCSA_USER_LOGs.InsertOnSubmit(_MyUserLog);
db.SubmitChanges();