开发者

LInq to Sql Insert DateTime problem

I’m getting started on Linq to sql. I am trying to insert a record. Here is my code.

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();

I received this exception on the SubmitChanges:

SqlDateTime overflow. Must be between 1/1/1753 12:00:00 AM and 12/31/9999 11:59:59 PM.

From my reading of this problem, it occurs when you have a date field and it is set to too low a value. Yet the only date field in the table I set to today’s date.

Here is the record; I don’t see what I’m doing wrong.

CREATE TABLE [dbo].[FMCSA_USER_LOG](
 [ID] [int] IDENTITY(1,1) NOT NU开发者_Go百科LL,
 [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)
)


It took me a while to shoehorn a .net datetime into a datetime2 column. I got it working with this method:

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();
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜