简体   繁体   English

DateTime.Now C# 向数据库添加记录时出现问题

[英]Issues with DateTime.Now C# When adding records to database

I am having a strange issue with my project.我的项目有一个奇怪的问题。 I have an upload excel file that automatically updates a table.我有一个自动更新表格的上传 excel 文件。 The upload part is irrelevant, just adding it for clarity.上传部分无关紧要,只是为了清楚起见添加它。 When using this I call the dateTime as CreatedDate = System.DateTime.Now .使用它时,我将 dateTime 称为CreatedDate = System.DateTime.Now It does not get this from the excel file.它没有从 excel 文件中得到这个。 I get a Out of range error - dateTime2 to dateTime conversion.我收到超出范围错误 - dateTime2 到 dateTime 的转换。 The table has a dateTime for the column.该表具有该列的dateTime时间。 I am scratching my head because I have the same exact code in another controller for another table and it works fine.我在挠头,因为我在另一个 controller 的另一个表中有相同的确切代码,它工作正常。 Tables where created at the same time in SQL.在 SQL 中同时创建的表。 So the code and the tables are exact.所以代码和表格是准确的。 One gives the error and the other does not.一个给出错误,另一个没有。 Date comes back fine with a Breakpoint.日期通过断点恢复正常。 Is there something in the code below that you can see that would be causing this?下面的代码中是否有您可以看到会导致此问题的内容?

[HttpPost]
    public ActionResult Add(HttpPostedFileBase FileUpload)
    {
        GeneralEntities objEntity = new GeneralEntities();
        string data = "";
        if (FileUpload != null)
        {
            if (FileUpload.ContentType == "application/vnd.ms-excel" || FileUpload.ContentType == "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet")
            {
                string filename = FileUpload.FileName;
                if (filename.EndsWith(".xlsx"))
                {
                    string targetpath = Server.MapPath("~/Documents/DetailFormatInExcel/");
                    FileUpload.SaveAs(targetpath + filename);
                    string pathToExcelFile = targetpath + filename;
                    string sheetName = "Sheet1";
                    var excelFile = new ExcelQueryFactory(pathToExcelFile);
                    var Equipment = from a in excelFile.Worksheet<AirFloatTablesExcelModel>(sheetName) select a;

                    foreach (var a in Equipment)
                    {
                        var ishaveModel = db.EquipmentModels.Where(d => d.Title == a.EquipmentModel).FirstOrDefault();
                        
                        
                        AirFloatTables at = new AirFloatTables
                        {
                            AirFloatTableId = Guid.NewGuid(),
                            EquipmentModelId = ishaveModel.EquipmentModelId,
                            Size = a.Size,
                            Type = a.Type,
                            TiltHeight = a.TiltHeight,
                            DWGRef = a.DWGRef,
                            Price = a.Price,
                            SortOrder = a.SortOrder,
                            CreatedBy = User.Identity.Name,
                            CreatedDate = System.DateTime.Now
                        };
                        db.AirFloatTables.Add(at);
                        db.SaveChanges();

                    }
                }
                else
                {

                    data = "This file is not valid format";
                    TempData["Message"] = data;
                }

                return RedirectToAction("Index");
            }
            else
            {
                data = "Only Excel file format is allowed";
                TempData["Message"] = data;
                return RedirectToAction("Index");
            }
        }
        else
        {
            if (FileUpload == null)
            {
                data = "Please choose Excel file";
            }
            TempData["Message"] = data;
            return RedirectToAction("Index");
        }
    }

数据表

UPDATE: It might be worth it to say that I took the created date out of my code and then in SQL for the column I put GetDATE() .更新:值得一提的是,我从代码中取出了创建日期,然后在 SQL 中放入GetDATE()列。 The issue is still there So It is not happening on the CreatedDate .问题仍然存在所以它没有发生在CreatedDate上。 If by chance the UpdatedDate is the issue I do not know why that would be.如果碰巧UpdatedDate是问题,我不知道为什么会这样。 When debugging I do see that UpdatedDate does come back with zero's However my code does not call for an insertion of UpdatedDate So why would it try to insert it anyway?调试时,我确实看到UpdatedDate确实返回零但是我的代码不需要插入UpdatedDate那么它为什么还要尝试插入它呢? Or is it just a code thing where it sees it and flags that it is not correct.或者它只是一个代码东西,它看到它并标记它不正确。 If this is the case, then why does the code in my other controller, that is exactly the same, not do the same thing?如果是这样的话,那为什么我的另一个controller里的代码,就是一模一样,不是做同样的事情呢?

You do not store a value for UpdatedDate , which is nullable, true, but this error happens mostly when for any reason a DateTime of year, month, day, etc. having a value of 0 is attempted to be inserted/updated as a value.您没有存储UpdatedDate的值,该值可以为空,为真,但此错误主要发生在出于任何原因尝试将值为 0 的年、月、日等的 DateTime 插入/更新为值时.

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM