SqlDateTime溢出。必须在linqtosql的1/1/1753 12:00:00 AM和12/31/9999 11:59:59 PM之间

时间:2014-07-26 10:19:57

标签: c# linq linq-to-sql

使用linqtosql模型将当前日期输入sql时出现问题。

这是我的代码:

using (DataClassesDataContext db = new DataClassesDataContext())
{
    List<string> file_names = null;
    if (Session["new_flag"].ToString() == "True")
    {
        Email myEmail = new Email();
        myEmail.Title = txt_ComName.Text.Trim();
        myEmail.ToUser = txt_ComName1.Text.Trim();
        myEmail.Body = CKEditor1.Text.Trim();
        if (file_upload.HasFile)
        {
            myEmail.IsFileAttached = true;
        }
        else
        {
            myEmail.IsFileAttached = false;
        }
        myEmail.SentDateTime = DateTime.Parse(System.DateTime.Now.ToString());
        db.Emails.InsertOnSubmit(myEmail);
        db.SubmitChanges();

在这里我发现了这个错误:

Server Error in '/NewsLetter' Application.

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

Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code. 

Exception Details: System.Data.SqlTypes.SqlTypeException: SqlDateTime overflow. Must be between 1/1/1753 12:00:00 AM and 12/31/9999 11:59:59 PM.

Source Error: 


Line 442:                    myEmail.SentDateTime = DateTime.Parse(System.DateTime.Now.ToString());
Line 443:                    db.Emails.InsertOnSubmit(myEmail);
Line 444:                    db.SubmitChanges();
Line 445:                    int newId = myEmail.Id;
Line 446:                    HttpFileCollection fileCollection = Request.Files;

Source File: f:\NewsLetter\Default.aspx.cs    Line: 444 

Stack Trace: 


[SqlTypeException: SqlDateTime overflow. Must be between 1/1/1753 12:00:00 AM and 12/31/9999 11:59:59 PM.]
   System.Data.SqlTypes.SqlDateTime.FromTimeSpan(TimeSpan value) +2131361
   System.Data.SqlTypes.SqlDateTime.FromDateTime(DateTime value) +232
   System.Data.SqlClient.MetaType.FromDateTime(DateTime dateTime, Byte cb) +46
   System.Data.SqlClient.TdsParser.WriteValue(Object value, MetaType type, Byte scale, Int32 actualLength, Int32 encodingByteSize, Int32 offset, TdsParserStateObject stateObj) +5002013
   System.Data.SqlClient.TdsParser.TdsExecuteRPC(_SqlRPC[] rpcArray, Int32 timeout, Boolean inSchema, SqlNotificationRequest notificationRequest, TdsParserStateObject stateObj, Boolean isCommandProc) +6248
   System.Data.SqlClient.SqlCommand.RunExecuteReaderTds(CommandBehavior cmdBehavior, RunBehavior runBehavior, Boolean returnStream, Boolean async) +987
   System.Data.SqlClient.SqlCommand.RunExecuteReader(CommandBehavior cmdBehavior, RunBehavior runBehavior, Boolean returnStream, String method, DbAsyncResult result) +162
   System.Data.SqlClient.SqlCommand.RunExecuteReader(CommandBehavior cmdBehavior, RunBehavior runBehavior, Boolean returnStream, String method) +32
   System.Data.SqlClient.SqlCommand.ExecuteReader(CommandBehavior behavior, String method) +141
   System.Data.SqlClient.SqlCommand.ExecuteDbDataReader(CommandBehavior behavior) +12
   System.Data.Common.DbCommand.ExecuteReader() +12
   System.Data.Linq.SqlClient.SqlProvider.Execute(Expression query, QueryInfo queryInfo, IObjectReaderFactory factory, Object[] parentArgs, Object[] userArgs, ICompiledSubQuery[] subQueries, Object lastResult) +1266
   System.Data.Linq.SqlClient.SqlProvider.ExecuteAll(Expression query, QueryInfo[] queryInfos, IObjectReaderFactory factory, Object[] userArguments, ICompiledSubQuery[] subQueries) +113
   System.Data.Linq.SqlClient.SqlProvider.System.Data.Linq.Provider.IProvider.Execute(Expression query) +344
   System.Data.Linq.StandardChangeDirector.DynamicInsert(TrackedObject item) +151
   System.Data.Linq.StandardChangeDirector.Insert(TrackedObject item) +235
   System.Data.Linq.ChangeProcessor.SubmitChanges(ConflictMode failureMode) +337
   System.Data.Linq.DataContext.SubmitChanges(ConflictMode failureMode) +378
   System.Data.Linq.DataContext.SubmitChanges() +23
   _Default.submit_Click(Object sender, EventArgs e) in f:\NewsLetter\Default.aspx.cs:444
   System.Web.UI.WebControls.Button.OnClick(EventArgs e) +118
   System.Web.UI.WebControls.Button.RaisePostBackEvent(String eventArgument) +112
   System.Web.UI.WebControls.Button.System.Web.UI.IPostBackEventHandler.RaisePostBackEvent(String eventArgument) +10
   System.Web.UI.Page.RaisePostBackEvent(IPostBackEventHandler sourceControl, String eventArgument) +13
   System.Web.UI.Page.RaisePostBackEvent(NameValueCollection postData) +36
   System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint) +5563

Version Information: Microsoft .NET Framework Version:4.0.30319; ASP.NET Version:4.0.30319.1

每次向sql提交当前日期值时都会发现此错误。我怎么用这句话来解决这个问题:

DateTime.Parse(System.DateTime.Now.ToString());

请帮帮我......

0 个答案:

没有答案