【发布时间】:2019-09-27 11:09:42
【问题描述】:
我正在将数据从我的 excel 文件导入到 sql 数据库。我正在使用 MVC 5 和实体框架 6。我正在使用 epplus 导入数据,使用 sqlbulkcopy 将数据导入 sql。 我收到 3 个错误: 1) excelImportDBEntities.Database.Connection 2) sqlbulkcopy 是一个命名空间,但用作一种类型 3) 对象阅读器
public async System.Threading.Tasks.Task<ActionResult>
ApplicationAsync(FormCollection formCollection)
{
var usersList = new List<bomApplicationImportTgt>();
if (Request != null)
{
HttpPostedFileBase file = Request.Files["UploadedFile"];
if ((file != null) && (file.ContentLength > 0) && !string.IsNullOrEmpty(file.FileName))
{
string fileName = file.FileName;
string fileContentType = file.ContentType;
byte[] fileBytes = new byte[file.ContentLength];
var data = file.InputStream.Read(fileBytes, 0, Convert.ToInt32(file.ContentLength));
using (var package = new ExcelPackage(file.InputStream))
{
var currentSheet = package.Workbook.Worksheets;
var workSheet = currentSheet.First();
var noOfCol = workSheet.Dimension.End.Column;
var noOfRow = workSheet.Dimension.End.Row;
for (int rowIterator = 2; rowIterator <= noOfRow; rowIterator++)
{
var user = new bomApplicationImportTgt();
user.date = Convert.ToDateTime(workSheet.Cells[rowIterator, 1].Value);
user.Description = workSheet.Cells[rowIterator, 2].Value?.ToString();
user.SequenceNumber = Convert.ToInt32(workSheet.Cells[rowIterator, 3].Value);
user.PartNumber = workSheet.Cells[rowIterator, 4].Value?.ToString();
user.PartsName = workSheet.Cells[rowIterator, 5].Value?.ToString();
user.SP = workSheet.Cells[rowIterator, 6].Value?.ToString();
user.INT = workSheet.Cells[rowIterator, 7].Value?.ToString();
user.SN = workSheet.Cells[rowIterator, 8].Value?.ToString();
user.SZ = workSheet.Cells[rowIterator, 9].Value?.ToString();
user.C = workSheet.Cells[rowIterator, 10].Value?.ToString();
user.E_F = workSheet.Cells[rowIterator, 11].Value?.ToString();
user.Block = workSheet.Cells[rowIterator, 12].Value?.ToString();
user.SEC = workSheet.Cells[rowIterator, 13].Value?.ToString();
user.Item = workSheet.Cells[rowIterator, 14].Value?.ToString();
user.SUF = workSheet.Cells[rowIterator, 15].Value?.ToString();
user.Model = workSheet.Cells[rowIterator, 16].Value?.ToString();
user.M_E_F = workSheet.Cells[rowIterator, 17].Value?.ToString();
user.OP = workSheet.Cells[rowIterator, 18].Value?.ToString();
user.Type = workSheet.Cells[rowIterator, 19].Value?.ToString();
user.Quantity = workSheet.Cells[rowIterator, 20].Value?.ToString();
user.PLGRPCD = workSheet.Cells[rowIterator, 21].Value?.ToString();
user.PL1 = workSheet.Cells[rowIterator, 22].Value?.ToString();
user.ATC1 = workSheet.Cells[rowIterator, 23].Value?.ToString();
user.PL2 = workSheet.Cells[rowIterator, 24].Value?.ToString();
user.ATC2 = workSheet.Cells[rowIterator, 25].Value?.ToString();
user.PL3 = workSheet.Cells[rowIterator, 26].Value?.ToString();
user.ATC3 = workSheet.Cells[rowIterator, 27].Value?.ToString();
user.Plant = workSheet.Cells[rowIterator, 28].Value?.ToString();
user.SHR = workSheet.Cells[rowIterator, 29].Value?.ToString();
user.DC_Number = workSheet.Cells[rowIterator, 30].Value?.ToString();
user.FileName = fileName;
usersList.Add(user);
}
}
}
}
using (Dev_Purchasing_New_ModelEntities excelImportDBEntities = new Dev_Purchasing_New_ModelEntities())
{
await new BulkWriter().InsertAsync(usersList, "bomApplicationImportTgt", excelImportDBEntities.Database.Connection, CancellationToken.None);
}
return View("Application");
}
我正在尝试加快导入速度,这就是我使用 sqlbulkcopy 的原因,但由于这些错误,我无法这样做。
【问题讨论】:
-
SqlBulkCopy 用于 Sql Server。看起来您正在根据变量名称
excelImportDBEntities写入 Excel。如果不是这种情况并且您知道这是一个 SqlConnection 实例,那么您可以强制转换它或创建一个新的 SqlConnection 实例并将您正在使用的连接字符串传递给它。 -
yes excelImportDBEntities 用于将数据从excel保存到sql。
-
我不确定是什么导致了这个错误 excelImportDBEntities.Database.Connection
-
如果
Database.Connection是一个SQLConnection你可以直接转换它((SqlConnection)Database.Connection)如果不是你可以创建一个新的SQL连接var sqlconn = new SqlConnection(excelImportDBEntities.Database.Connection.ConnectionString);(别忘了把它包装在一个using语句中)
标签: c# asp.net-mvc-5 entity-framework-6 epplus sqlbulkcopy