public ActionResult Create([Bind(Include = "UserId,FirstName,LastName,DOB,CreateTS")] User user)
        {
            if (ModelState.IsValid)
            {
                db.Users.Add(user);
                db.SaveChanges();
                return(RedirectToAction("Home"));
            }

            return(View(user));
        }
 public void SaveChanges()
 {
     context.SaveChanges();
 }
Пример #3
0
        public JsonResult UploadExcel(mvc_users users, HttpPostedFileBase FileUpload)
        {
            List <string> data = new List <string>();

            if (FileUpload != null)
            {
                // tdata.ExecuteCommand("truncate table OtherCompanyAssets");
                if (FileUpload.ContentType == "application/vnd.ms-excel" || FileUpload.ContentType == "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet")
                {
                    string filename   = FileUpload.FileName;
                    string targetpath = Server.MapPath("~/testdata/");
                    FileUpload.SaveAs(targetpath + filename);
                    string pathToExcelFile  = targetpath + filename;
                    var    connectionString = "";
                    if (filename.EndsWith(".xls"))
                    {
                        connectionString = string.Format("Provider=Microsoft.Jet.OLEDB.4.0; data source={0}; Extended Properties=Excel 8.0;", pathToExcelFile);
                    }
                    else if (filename.EndsWith(".xlsx"))
                    {
                        connectionString = string.Format("Provider=Microsoft.ACE.OLEDB.12.0;Data Source={0}; Extended Properties=\"Excel 12.0; HDR=YES; IMEX=1\";", pathToExcelFile);
                        //connectionString = string.Format("Provider=Microsoft.ACE.OLEDB.12.0;Data Source={0};Extended Properties=\"Excel 12.0 Xml;HDR=YES;IMEX=1\";", pathToExcelFile);
                        // <add name="Testdb_2007" connectionString="Provider=Microsoft.ACE.OLEDB.12.0;Data Source={0};Extended Properties='Excel 8.0;HDR=YES'"/>
                    }

                    var adapter = new OleDbDataAdapter("SELECT * FROM [工作表1$]", connectionString);
                    var ds      = new DataSet();

                    adapter.Fill(ds, "ExcelTable");

                    DataTable dtable = ds.Tables["ExcelTable"];

                    string sheetName = "工作表1";

                    var excelFile    = new ExcelQueryFactory(pathToExcelFile);
                    var artistAlbums = from a in excelFile.Worksheet <mvc_users>(sheetName) select a;

                    foreach (var a in artistAlbums)
                    {
                        try
                        {
                            if (a.Name != "" && a.Address != "" && a.ContactNo != "")
                            {
                                mvc_users TU = new mvc_users();
                                TU.Name      = a.Name;
                                TU.Address   = a.Address;
                                TU.ContactNo = a.ContactNo;
                                _db.mvc_users.Add(TU);

                                _db.SaveChanges();
                            }
                            else
                            {
                                data.Add("<ul>");
                                if (a.Name == "" || a.Name == null)
                                {
                                    data.Add("<li> name is required</li>");
                                }
                                if (a.Address == "" || a.Address == null)
                                {
                                    data.Add("<li> Address is required</li>");
                                }
                                if (a.ContactNo == "" || a.ContactNo == null)
                                {
                                    data.Add("<li>ContactNo is required</li>");
                                }

                                data.Add("</ul>");
                                data.ToArray();
                                return(Json(data, JsonRequestBehavior.AllowGet));
                            }
                        }

                        catch (DbEntityValidationException ex)
                        {
                            foreach (var entityValidationErrors in ex.EntityValidationErrors)
                            {
                                foreach (var validationError in entityValidationErrors.ValidationErrors)
                                {
                                    Response.Write("Property: " + validationError.PropertyName + " Error: " + validationError.ErrorMessage);
                                }
                            }
                        }
                    }
                    //deleting excel file from folder
                    //if ((System.IO.File.Exists(pathToExcelFile)))
                    //{
                    //    System.IO.File.Delete(pathToExcelFile);
                    //}
                    return(Json("success", JsonRequestBehavior.AllowGet));
                }
                else
                {
                    //alert message for invalid file format
                    data.Add("<ul>");
                    data.Add("<li>Only Excel file format is allowed</li>");
                    data.Add("</ul>");
                    data.ToArray();
                    return(Json(data, JsonRequestBehavior.AllowGet));
                }
            }
            else
            {
                data.Add("<ul>");
                if (FileUpload == null)
                {
                    data.Add("<li>Please choose Excel file</li>");
                }
                data.Add("</ul>");
                data.ToArray();
                return(Json(data, JsonRequestBehavior.AllowGet));
            }
        }