Когда я запускаю свое веб-приложение на локальном сервере моего компьютера, никаких проблем не возникает, но я получаю сообщение об ошибке при публикации своего веб-приложения при попытке импортировать данные из файла Excel в базу данных на сервере.
Ошибка:
'Ошибка.Произошла ошибка при обработке вашего запроса. '
Строка подключения:
<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>");
}