得到'错误。处理您的请求时发生错误。'将数据从excel文件导入服务器中的数据库时

时间:2018-10-18 17:06:16

标签: c# asp.net asp.net-mvc asp.net-mvc-4 controller

当我在计算机的本地服务器上运行Web应用程序时,没有任何问题,但是在尝试将数据从excel文件导入服务器中的数据库时发布Web应用程序时出现错误。

错误:

'错误。处理您的请求时出错。'

连接字符串:

<add name="DatabaseContext" providerName="System.Data.SqlClient" connectionString="data source=*******;initial catalog=******;persist security info=True;user id=***;password=*****;" />

导入操作:

if (Session["Name"] != null)
        {
            string filePath = string.Empty;
            if (postedFile != null)
            {
                string path = Server.MapPath("~/Uploads/");
                if (!Directory.Exists(path))
                {
                    Directory.CreateDirectory(path);
                }

                filePath = path + Path.GetFileName(postedFile.FileName);
                string extension = Path.GetExtension(postedFile.FileName);
                switch (extension)
                {
                    case ".xls": //Excel 97-03.                       
                        break;
                    case ".xlsx": //Excel 07 and above.                       
                        break;
                    default:
                        return Content("<script language='javascript' type='text/javascript'>alert('Lütfen geçerli bir dosya seçiniz!'); window.setTimeout(function(){window.location.href = '/Admin/Index';}, 0);</script>");
                }
                postedFile.SaveAs(filePath);

                string conString = string.Empty;

                switch (extension)
                {

                    case ".xls": //Excel 97-03.
                        conString = ConfigurationManager.ConnectionStrings["Excel03ConString"].ConnectionString;
                        break;
                    case ".xlsx": //Excel 07 and above.
                        conString = ConfigurationManager.ConnectionStrings["Excel07ConString"].ConnectionString;
                        break;
                    default:

                        return Content("<script language='javascript' type='text/javascript'>alert('Lütfen geçerli bir dosya seçiniz!'); window.setTimeout(function(){window.location.href = '/Admin/Index';}, 0);</script>");

                }

                DataTable dt = new DataTable();
                conString = string.Format(conString, filePath);

                using (OleDbConnection connExcel = new OleDbConnection(conString))
                {
                    using (OleDbCommand cmdExcel = new OleDbCommand())
                    {
                        using (OleDbDataAdapter odaExcel = new OleDbDataAdapter())
                        {
                            cmdExcel.Connection = connExcel;

                            //Get the name of First Sheet.
                            connExcel.Open();
                            DataTable dtExcelSchema;
                            dtExcelSchema = connExcel.GetOleDbSchemaTable(OleDbSchemaGuid.Tables, null);
                            string sheetName = dtExcelSchema.Rows[0]["TABLE_NAME"].ToString();
                            connExcel.Close();

                            //Read Data from First Sheet.
                            connExcel.Open();
                            cmdExcel.CommandText = "SELECT * From [" + sheetName + "]";
                            odaExcel.SelectCommand = cmdExcel;
                            odaExcel.Fill(dt);
                            connExcel.Close();
                        }
                    }
                }

                foreach (DataRow row in dt.Rows)
                {
                    try
                    {
                        db.TblYUInfoMember.Add(new InformaticsMember
                        {
                            FirstName = row["Adı"].ToString(),
                            LastName = row["Soyadı"].ToString(),
                            Department = row["Bölümü"].ToString(),
                            Class = row["Sınıfı"].ToString(),
                            Email = row["E-Mail"].ToString(),
                            Phone = row["Telefon"].ToString()


                            //modelID = Convert.ToInt32(row["modelID"]),
                            //EnvantereEklemeTarihi = Convert.ToDateTime(row["EnvantereEklemeTarihi"]),
                        });

                    }
                    catch (Exception)
                    {
                        return Content("<script language='javascript' type='text/javascript'>alert('Dosyayı kontrol edin!'); window.setTimeout(function(){window.location.href = '/Admin/Index';}, 0);</script>");
                        throw;
                    }

                }

                db.SaveChanges();

            }
            else
            {
                return Content("<script language='javascript' type='text/javascript'>alert('Lütfen bir dosya seçin!'); window.setTimeout(function(){window.location.href = '/Admin/Index';}, 0);</script>");
            }
            return Content("<script language='javascript' type='text/javascript'>alert('Başarılı!'); window.setTimeout(function(){window.location.href = '/Admin/Index';}, 0);</script>");
        }

0 个答案:

没有答案