Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/asp.net-mvc/17.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 在MVC应用程序中批量更新单个数据库_C#_Asp.net Mvc_Linq_Entity Framework - Fatal编程技术网

C# 在MVC应用程序中批量更新单个数据库

C# 在MVC应用程序中批量更新单个数据库,c#,asp.net-mvc,linq,entity-framework,C#,Asp.net Mvc,Linq,Entity Framework,编辑2:在浏览了其他问题和你们这些可爱的人的建议后,我找到了答案,这个程序现在运行得非常棒!谢谢大家 编辑:我已经根据其他用户的建议更新了我的代码。说真的,你们真是太棒了 我有一个可以在本地主机上工作的页面,但不能在开发/测试服务器上工作 以下是该页面的工作原理: 用户上传xml文件并发布 程序通过Xml文件进行解析,以创建BoxViewModels列表 程序使用 函数导入名为deleteAllbox。这个函数导入只执行“从dbo.box中删除” 使用BoxViewModels列表,程序将更新数

编辑2:在浏览了其他问题和你们这些可爱的人的建议后,我找到了答案,这个程序现在运行得非常棒!谢谢大家

编辑:我已经根据其他用户的建议更新了我的代码。说真的,你们真是太棒了

我有一个可以在本地主机上工作的页面,但不能在开发/测试服务器上工作

以下是该页面的工作原理:

  • 用户上传xml文件并发布
  • 程序通过Xml文件进行解析,以创建BoxViewModels列表
  • 程序使用 函数导入名为deleteAllbox。这个函数导入只执行“从dbo.box中删除”
  • 使用BoxViewModels列表,程序将更新数据库 带着所有的盒子
  • 这在我的localhost上非常有效(我已经验证了所有312个框都成功地输入到了box表中),但是当我在我的Dev/Test服务器上使用同一个文件进行尝试时,唯一发生的事情就是删除了所有的框。此外,我的本地主机上的程序大约需要一两分钟才能完成,而Dev/Test上的程序只需要几秒钟就可以完成。我希望开发/测试所花费的时间至少和我的本地主机一样长。我认为这不是文件大小的问题,因为文件大小只有112Kb。这就像带有boxesList的foreach循环没有任何框,或者完全被忽略

    这是我的代码,以防有用。我认为解决方案可能是我需要在web.config中设置的设置,但我的想法也可能是错误的

        [AcceptVerbs(HttpVerbs.Post)]
        public ActionResult FileUpload(HttpPostedFileBase fileFromPage)
        {
            if (fileFromPage != null) //User must upload a file.
            {
                if (fileFromPage.ContentType == "text/xml") //File must be an xml file.
                {
                    List<BoxViewModel> boxesList = new List<BoxViewModel>();
    
                    try //No errors will be caught if all boxes in the xml file are valid.
                    {
    
                        XmlDocument xmlDoc = new XmlDocument();
                        xmlDoc.Load(fileFromPage.InputStream);
    
                        XmlNodeList boxNodeList = xmlDoc.SelectNodes("/initialize/boxes/box");
    
                        //Write each box in the xml to a BoxViewModel, then add to the boxesList for updating the database later.
                        foreach (XmlNode boxNode in boxNodeList)
                        {
                            XmlNode currentBoxNode = boxNode;
                            BoxViewModel currentBox = new BoxViewModel();
    
                            currentBox.Compliment = currentBoxNode.SelectSingleNode("compliment").InnerText;
                            currentBox.Description = currentBoxNode.SelectSingleNode("description").InnerText;
                            currentBox.BoxType = currentBoxNode.SelectSingleNode("boxtype").InnerText;
                            currentBox.InsideLength = decimal.Parse(currentBoxNode.SelectSingleNode("il").InnerText);
                            currentBox.InsideBreadth = decimal.Parse(currentBoxNode.SelectSingleNode("ib").InnerText);
                            currentBox.InsideHeight = decimal.Parse(currentBoxNode.SelectSingleNode("ih").InnerText);
                            currentBox.OutsideLength = decimal.Parse(currentBoxNode.SelectSingleNode("ol").InnerText);
                            currentBox.OutsideBreadth = decimal.Parse(currentBoxNode.SelectSingleNode("ob").InnerText);
                            currentBox.OutsideHeight = decimal.Parse(currentBoxNode.SelectSingleNode("oh").InnerText);
                            currentBox.BoxWeight = decimal.Parse(currentBoxNode.SelectSingleNode("bw").InnerText);
                            currentBox.BoxGrossWeight = decimal.Parse(currentBoxNode.SelectSingleNode("bgw").InnerText);
                            currentBox.BoxStrength = int.Parse(currentBoxNode.SelectSingleNode("boxstrength").InnerText);
    
                            if (string.IsNullOrEmpty(currentBox.Compliment))
                                throw new Exception();
                            if (string.IsNullOrEmpty(currentBox.Description))
                                throw new Exception();
                            if (string.IsNullOrEmpty(currentBox.BoxType))
                                throw new Exception();
    
                            boxesList.Add(currentBox);
                        }
                    }
                    catch
                    {
                        ViewBag.MsgText = "There was an error when processing the file!";
                        ViewBag.MsgColor = "Red";
                    }
    
                    try
                    {
                        if (boxesList.Count > 0) //Only update if there is at least one box.
                        {
                            dbEntities.DeleteAllBoxes(); //Clear the Boxes table for the new boxes.
    
                            foreach (BoxViewModel box in boxesList) //For each box in the boxesList, add a new box to the database.
                            {
                                Box newBox = new Box()
                                {
                                    BoxGrossWeight = box.BoxGrossWeight,
                                    //BoxID = model.BoxID,
                                    BoxStrength = box.BoxStrength,
                                    BoxType = box.BoxType,
                                    BoxWeight = box.BoxWeight,
                                    Compliment = box.Compliment,
                                    Description = box.Description,
                                    InsideBreadth = box.InsideBreadth,
                                    InsideHeight = box.InsideHeight,
                                    InsideLength = box.InsideLength,
                                    OutsideBreadth = box.OutsideBreadth,
                                    OutsideHeight = box.OutsideHeight,
                                    OutsideLength = box.OutsideLength,
                                    UpdateDateTime = DateTime.Now
                                };
    
                                dbEntities.Boxes.AddObject(newBox);
                            }
    
                            //Save changes then redirect to page where user can see the results of the upload.
                            dbEntities.SaveChanges();
                            return RedirectToAction("Index");
                        }
                        else
                        {
                            ViewBag.MsgText = "There are no boxes in this file!";
                            ViewBag.MsgColor = "Red";
                            return View();
                        }
                    }
                    catch
                    {
                        ViewBag.MsgText = "There was an error while updating the database!";
                        ViewBag.MsgColor = "Red";
                    }
                }
                else
                {
                    ViewBag.MsgText = "The file must be an xml file!";
                    ViewBag.MsgColor = "Red";
                }
            }
            else
            {
                ViewBag.MsgText = "You must attach a file!";
                ViewBag.MsgColor = "Red";
            }
    
            return View();
        }
    
    [AcceptVerbs(HttpVerbs.Post)]
    公共操作结果文件上载(HttpPostedFileBase fileFromPage)
    {
    if(fileFromPage!=null)//用户必须上载文件。
    {
    if(fileFromPage.ContentType==“text/xml”)//文件必须是xml文件。
    {
    列表框列表=新列表();
    try//如果xml文件中的所有框都有效,则不会捕获任何错误。
    {
    XmlDocument xmlDoc=新的XmlDocument();
    加载(fileFromPage.InputStream);
    XmlNodeList-boxNodeList=xmlDoc.SelectNodes(“/initialize/box/box”);
    //将xml中的每个框写入一个BoxViewModel,然后添加到boxesList,以便以后更新数据库。
    foreach(boxNodeList中的XmlNode boxNode)
    {
    XmlNode currentBoxNode=boxNode;
    BoxViewModel currentBox=新的BoxViewModel();
    currentBox.compaid=currentBoxNode。选择SingleNode(“compaid”)。InnerText;
    currentBox.Description=currentBoxNode.SelectSingleNode(“Description”).InnerText;
    currentBox.BoxType=currentBoxNode。选择SingleNode(“BoxType”)。InnerText;
    currentBox.InsidelLength=decimal.Parse(currentBoxNode.SelectSingleNode(“il”).InnerText);
    currentBox.InsideBradth=decimal.Parse(currentBoxNode.SelectSingleNode(“ib”).InnerText);
    currentBox.InsideHight=decimal.Parse(currentBoxNode.SelectSingleNode(“ih”).InnerText);
    currentBox.OutsideLength=decimal.Parse(currentBoxNode.SelectSingleNode(“ol”).InnerText);
    currentBox.OutsideWidth=decimal.Parse(currentBoxNode.SelectSingleNode(“ob”).InnerText);
    currentBox.OutsideHeight=decimal.Parse(currentBoxNode.SelectSingleNode(“oh”).InnerText);
    currentBox.BoxWeight=decimal.Parse(currentBoxNode.SelectSingleNode(“bw”).InnerText);
    currentBox.BoxGrossWight=decimal.Parse(currentBoxNode.SelectSingleNode(“bgw”).InnerText);
    currentBox.BoxStrength=int.Parse(currentBoxNode.SelectSingleNode(“BoxStrength”).InnerText);
    if(string.IsNullOrEmpty(currentBox.恭维))
    抛出新异常();
    if(string.IsNullOrEmpty(currentBox.Description))
    抛出新异常();
    if(string.IsNullOrEmpty(currentBox.BoxType))
    抛出新异常();
    boxesList.Add(当前框);
    }
    }
    抓住
    {
    ViewBag.MsgText=“处理文件时出错!”;
    ViewBag.MsgColor=“红色”;
    }
    尝试
    {
    if(boxesList.Count>0)//仅当至少有一个框时更新。
    {
    dbEntities.DeleteAllBox();//清除新框的框表。
    foreach(boxesList中的BoxViewModel框)//对于boxesList中的每个框,向数据库中添加一个新框。
    {
    Box newBox=newBox()
    {
    boxgrosswight=box.boxgrosswight,
    //BoxID=model.BoxID,
    BoxStrength=box.BoxStrength,
    BoxType=box.BoxType,
    BoxWeight=box.BoxWeight,
    恭维,恭维,
    Description=box.Description,
    insidebradth=box.insidebradth,
    InsidehHeight=box.insidehHeight,
    InsidelLength=box.InsidelLength,
    OutsideWidth=box.OutsideWidth,
    外侧高度=箱。外侧高度,
    OutsideLength=box.OutsideLength,
    UpdateDateTime=DateT