C# 在MVC5中使用Epplus和Sqlbulkcopy将Excel文件导入数据库
我正在使用MVC5中的Epplus库和Entity Framework 6将一个大型数据文件从excel导入数据库表。将该文件复制到数据库需要花费大量时间。我希望使用sqlbulkcopy快速插入数据,但我不确定如何在我的控制器中做到这一点。 代码现在正在运行,但将所有数据导入数据库需要花费大量时间C# 在MVC5中使用Epplus和Sqlbulkcopy将Excel文件导入数据库,c#,asp.net-mvc-5,entity-framework-6,sqlbulkcopy,import-from-excel,C#,Asp.net Mvc 5,Entity Framework 6,Sqlbulkcopy,Import From Excel,我正在使用MVC5中的Epplus库和Entity Framework 6将一个大型数据文件从excel导入数据库表。将该文件复制到数据库需要花费大量时间。我希望使用sqlbulkcopy快速插入数据,但我不确定如何在我的控制器中做到这一点。 代码现在正在运行,但将所有数据导入数据库需要花费大量时间 public ActionResult Structure(FormCollection formCollection) { var usersList = new Li
public ActionResult Structure(FormCollection formCollection)
{
var usersList = new List<bomStructuredImportTgt>();
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 bomStructuredImportTgt();
user.ActualDate = Convert.ToDateTime(workSheet.Cells[rowIterator, 1].Value);
user.Description = workSheet.Cells[rowIterator, 2].Value?.ToString();
user.Level = Convert.ToInt32(workSheet.Cells[rowIterator, 3].Value);
user.ParentPartNumber = workSheet.Cells[rowIterator, 4].Value?.ToString();
user.PartNumber = workSheet.Cells[rowIterator, 5].Value?.ToString();
user.PartName = workSheet.Cells[rowIterator, 6].Value?.ToString();
user.HNS = workSheet.Cells[rowIterator, 7].Value?.ToString();
user.DWGSZ = workSheet.Cells[rowIterator, 8].Value?.ToString();
user.Part = workSheet.Cells[rowIterator, 9].Value?.ToString();
user.L1Quantity = Convert.ToInt32(workSheet.Cells[rowIterator, 10].Value);
user.ColorM = workSheet.Cells[rowIterator, 11].Value?.ToString();
user.ATTCD = workSheet.Cells[rowIterator, 12].Value?.ToString();
user.KD = workSheet.Cells[rowIterator, 13].Value?.ToString();
user.Sell = workSheet.Cells[rowIterator, 14].Value?.ToString();
user.PlGroup = workSheet.Cells[rowIterator, 15].Value?.ToString();
user.PL1 = workSheet.Cells[rowIterator, 16].Value?.ToString();
user.AT1 = workSheet.Cells[rowIterator, 17].Value?.ToString();
user.PL2 = workSheet.Cells[rowIterator, 18].Value?.ToString();
user.AT2 = workSheet.Cells[rowIterator, 19].Value?.ToString();
user.PL3 = workSheet.Cells[rowIterator, 20].Value?.ToString();
user.Plant = workSheet.Cells[rowIterator, 21].Value?.ToString();
user.SHRPCMINMAX = workSheet.Cells[rowIterator, 22].Value?.ToString();
usersList.Add(user);
}
}
}
}
using (Dev_Purchasing_New_ModelEntities excelImportDBEntities = new Dev_Purchasing_New_ModelEntities())
{
foreach (var item in usersList)
{
excelImportDBEntities.bomStructuredImportTgts.Add(item);
}
excelImportDBEntities.SaveChanges();
}
return View("Structure");
}
公共操作结果结构(FormCollection FormCollection)
{
var usersList=新列表();
if(请求!=null)
{
HttpPostedFileBase file=Request.Files[“UploadedFile”];
如果((file!=null)&&&(file.ContentLength>0)&&!string.IsNullOrEmpty(file.FileName))
{
字符串文件名=file.fileName;
字符串fileContentType=file.ContentType;
byte[]fileBytes=新字节[file.ContentLength];
var data=file.InputStream.Read(fileBytes,0,Convert.ToInt32(file.ContentLength));
使用(var package=new ExcelPackage(file.InputStream))
{
var currentSheet=package.Workbook.Worksheets;
var工作表=currentSheet.First();
var noOfCol=workSheet.Dimension.End.Column;
var noOfRow=workSheet.Dimension.End.Row;
对于(int-rowIterator=2;rowIteratorIMHO),直接通过网站加载巨大的EXCEL不是一个好主意,因为:
我现在找不到我的代码,但我曾经创建一个.CSV文件,然后使用批量复制(BCP)将其提交到表中。它比任何其他解决方案都快。我不知道如何在控制器中使用bulkcopy使用ExcelImportDBenties.Database.SqlCommand来运行SQL查询。在这里,您可以找到一些示例:我已经看到这篇文章没有任何帮助。