我有一个MVC应用程序,允许管理员用户将excel文件上传到系统上;然后,控制器使用excel数据创建一个数据集,然后使用SqlBulkCopy使用该数据集填充“学校”和“ School2”数据库。
当我使用IIS Express在本地测试这些上传文件时,它们完美工作,尽管当我按下导入按钮时,部署到AWS Elastic beantalk的同一版本也会引发错误。控制台中的错误显示为:无法加载资源:服务器以状态500(内部服务器错误)响应。无论如何,我可以获得有关此错误的更多信息吗?我检查了EC2日志,唯一可以看到的与错误有关的行是(也没有给出错误的原因):
2019-10-28 16:53:19 172.30.2.190 POST /上传-80 user1@test.com 37.228.251.230 Mozilla / 5.0 +(Windows + NT + 10.0; + Win64; + x64)+ AppleWebKit / 537.36 +(KHTML,+ like + Gecko)+ Chrome / 77.0.3865.120 + Safari / 537.36 http://campbookingsys-dev.eu-west-1.elasticbeanstalk.com/Upload 500 0 0 93 错误图片:https://i.ibb.co/QpR7HmV/errodb.png
有人能看到任何清楚的原因来说明为什么此操作在本地可以正常运行,但在部署到弹性beantalk时却不起作用?我的数据库设置正确,因为我可以在应用程序上查看数据库中的实体框架数据列表。我也可以登录,以便数据库连接正常。唯一的问题是,上载按钮在部署时不起作用(尽管确实在IIS上本地起作用)
我的UploadController:
public class UploadController : Controller
{
SqlConnection con = new SqlConnection(@"Data Source=bookingdb.cwln7mwjvxdd.eu-west-1.rds.amazonaws.com,1433;Initial Catalog=modeldb;User ID=USER;Password=PASSWORD;Database=modeldb;Connect Timeout=30;Encrypt=False;TrustServerCertificate=False;ApplicationIntent=ReadWrite;MultiSubnetFailover=False");
OleDbConnection Econ;
public ActionResult Index()
{
return View();
}
[HttpPost]
public ActionResult Index(HttpPostedFileBase file)
{
string filename = Guid.NewGuid() + Path.GetExtension(file.FileName);
string filepath = "/excelfolder/" + filename;
file.SaveAs(Path.Combine(Server.MapPath("/excelfolder"), filename));
InsertExceldata(filepath, filename);
return View();
}
[HttpPost]
public ActionResult Index2(HttpPostedFileBase file)
{
string filename = Guid.NewGuid() + Path.GetExtension(file.FileName);
string filepath = "/excelfolder/" + filename;
file.SaveAs(Path.Combine(Server.MapPath("/excelfolder"), filename));
InsertExceldata2(filepath, filename);
return RedirectToAction("Index");
}
private void ExcelConn(string filepath)
{
string constr = string.Format(@"Provider=Microsoft.ACE.OLEDB.12.0;Data Source={0};Extended Properties=""Excel 12.0 Xml;HDR=YES;""", filepath);
Econ = new OleDbConnection(constr);
}
private void InsertExceldata(string filepath, string filename)
{
string fullpath = Server.MapPath("/excelfolder/") + filename;
ExcelConn(fullpath);
string query = string.Format("Select * from [{0}]", "Sheet1$");
OleDbCommand Ecom = new OleDbCommand(query, Econ);
Econ.Open();
DataSet ds = new DataSet();
OleDbDataAdapter oda = new OleDbDataAdapter(query, Econ);
Econ.Close();
oda.Fill(ds);
DataTable dt = ds.Tables[0];
SqlBulkCopy objbulk = new SqlBulkCopy(con);
objbulk.DestinationTableName = "dbo.Schools";
objbulk.ColumnMappings.Add("AcademicYear", "AcademicYear");
objbulk.ColumnMappings.Add("RollNumber", "RollNumber");
objbulk.ColumnMappings.Add("OfficialSchoolName", "OfficialSchoolName");
objbulk.ColumnMappings.Add("Address1", "Address1");
objbulk.ColumnMappings.Add("Address2", "Address2");
objbulk.ColumnMappings.Add("Address3", "Address3");
objbulk.ColumnMappings.Add("Address4", "Address4");
objbulk.ColumnMappings.Add("County", "County");
objbulk.ColumnMappings.Add("Eircode", "Eircode");
objbulk.ColumnMappings.Add("LocalAuthority", "LocalAuthority");
objbulk.ColumnMappings.Add("X", "X");
objbulk.ColumnMappings.Add("Y", "Y");
objbulk.ColumnMappings.Add("ITMEast", "ITMEast");
objbulk.ColumnMappings.Add("ITMNorth", "ITMNorth");
objbulk.ColumnMappings.Add("Latitude", "Latitude");
objbulk.ColumnMappings.Add("Longitude", "Longitude");
con.Open();
objbulk.WriteToServer(dt);
con.Close();
}
private void InsertExceldata2(string filepath, string filename)
{
string fullpath = Server.MapPath("/excelfolder/") + filename;
ExcelConn(fullpath);
string query = string.Format("Select * from [{0}]", "Sheet1$");
OleDbCommand Ecom = new OleDbCommand(query, Econ);
Econ.Open();
DataSet ds = new DataSet();
OleDbDataAdapter oda = new OleDbDataAdapter(query, Econ);
Econ.Close();
oda.Fill(ds);
DataTable dt = ds.Tables[0];
SqlBulkCopy objbulk = new SqlBulkCopy(con);
objbulk.DestinationTableName = "dbo.School2";
objbulk.ColumnMappings.Add("RollNumber", "RollNumber");
objbulk.ColumnMappings.Add("OfficialSchoolName", "OfficialSchoolName");
objbulk.ColumnMappings.Add("Address1", "Address1");
objbulk.ColumnMappings.Add("Address2", "Address2");
objbulk.ColumnMappings.Add("Address3", "Address3");
objbulk.ColumnMappings.Add("Address4", "Address4");
objbulk.ColumnMappings.Add("County", "County");
objbulk.ColumnMappings.Add("Eircode", "Eircode");
objbulk.ColumnMappings.Add("PhoneNumber", "PhoneNumber");
objbulk.ColumnMappings.Add("Email", "Email");
objbulk.ColumnMappings.Add("PrincipalName", "PrincipalName");
objbulk.ColumnMappings.Add("DeisSchool", "DeisSchool");
objbulk.ColumnMappings.Add("SchoolGender", "SchoolGender");
objbulk.ColumnMappings.Add("PupilAttendanceType", "PupilAttendanceType");
objbulk.ColumnMappings.Add("IrishClassification", "IrishClassification");
objbulk.ColumnMappings.Add("GaeltachtArea", "GaeltachtArea");
objbulk.ColumnMappings.Add("FeePayingSchool", "FeePayingSchool");
objbulk.ColumnMappings.Add("Religion", "Religion");
objbulk.ColumnMappings.Add("OpenClosedStatus", "OpenClosedStatus");
objbulk.ColumnMappings.Add("TotalGirls", "TotalGirls");
objbulk.ColumnMappings.Add("TotalBoys", "TotalBoys");
objbulk.ColumnMappings.Add("TotalPupils", "TotalPupils");
con.Open();
objbulk.WriteToServer(dt);
con.Close();
}
}
我还尝试在EC2上安装excel OLEDB提供程序驱动程序,但仍然出现错误。我的RDS是否需要访问这些驱动程序?如果是这样,我不确定如何实现此目标,因为我不能像在EC2上那样仅在RDS上安装Access Jet驱动程序。