我正在Asp.Net MVC上传一个Excel文件。它在我的本地计算机和实时服务器上运行良好,但是当上传近2000条记录时,上传记录需要半个多小时。
如何提高上传速度?我正在使用sql server 2012,ASP.NET MVC。
try
{
DataSet ds = new DataSet();
if (Request.Files["file"].ContentLength > 0)
{
ds.Clear();
string fileExtension =
System.IO.Path.GetExtension(Request.Files["file"].FileName);
if (fileExtension == ".xls" || fileExtension == ".xlsx")
{
string fileLocation = Server.MapPath("~/Content/ExcelForUpload/") + Request.Files["file"].FileName;
if (System.IO.File.Exists(fileLocation))
{
System.IO.File.Delete(fileLocation);
}
Request.Files["file"].SaveAs(fileLocation);
string excelConnectionString = string.Empty;
excelConnectionString = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" +
fileLocation + ";Extended Properties=\"Excel 12.0;HDR=Yes;IMEX=2\"";
//connection String for xls file format.
if (fileExtension == ".xls")
{
excelConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" +
fileLocation + ";Extended Properties=\"Excel 8.0;HDR=Yes;IMEX=2\"";
}
//connection String for xlsx file format.
else if (fileExtension == ".xlsx")
{
excelConnectionString = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" +
fileLocation + ";Extended Properties=\"Excel 12.0;HDR=Yes;IMEX=2\"";
}
//Create Connection to Excel work book and add oledb namespace
OleDbConnection excelConnection = new OleDbConnection(excelConnectionString);
excelConnection.Open();
DataTable dt = new DataTable();
dt = excelConnection.GetOleDbSchemaTable(OleDbSchemaGuid.Tables, null);
if (dt == null)
{
return null;
}
String[] excelSheets = new String[dt.Rows.Count];
int t = 0;
//excel data saves in temp file here.
foreach (DataRow row in dt.Rows)
{
ds.Clear();
excelSheets[t] = row["TABLE_NAME"].ToString();
t++;
}
OleDbConnection excelConnection1 = new OleDbConnection(excelConnectionString);
string query = string.Format("Select * from [{0}]", excelSheets[0]);
using (OleDbDataAdapter dataAdapter = new OleDbDataAdapter(query, excelConnection1))
{
// dataAdapter.Dispose();
dataAdapter.Fill(ds);
}
excelConnection.Close();
System.IO.File.Delete(fileLocation);
}
ItemCategory objcategory;
Segment objSegment;
Size objSize;
ProductModel objproductmodel;
string tempcategoryname;
string tempsegmentname;
string tempclassificationname;
string productmodelname;
int catid;
int segid;
int classificationId;
for (int i = 0; i < ds.Tables[0].Rows.Count; i++)
{
objcategory = new ItemCategory();
objSegment = new Segment();
objSize = new Size();
objproductmodel = new ProductModel();
tempcategoryname = ds.Tables[0].Rows[i][0].ToString();
if (db.ItemCategories.Where(x => x.CategoryName.ToLower().Trim() == tempcategoryname.ToLower().Trim()).Count() > 0)
{
catid = db.ItemCategories.Where(x => x.CategoryName.ToLower().Trim() == tempcategoryname.ToLower().Trim()).FirstOrDefault().ID;
objproductmodel.CategoryID = catid;
}
else
{
objcategory.CategoryName = tempcategoryname;
objcategory.IsActive = true;
objcategory.CreatedBy = 523;
objcategory.CreatedDate = DateTime.UtcNow.AddHours(5);
db.ItemCategories.Add(objcategory);
db.SaveChanges();
if (db.ItemCategories.Where(x => x.CategoryName.ToLower().Trim() == tempcategoryname.ToLower().Trim()).Count() > 0)
{
catid = db.ItemCategories.Where(x => x.CategoryName.ToLower().Trim() == tempcategoryname.ToLower().Trim()).FirstOrDefault().ID;
objproductmodel.CategoryID = catid;
}
}
tempsegmentname = ds.Tables[0].Rows[i][1].ToString();
if (db.Segments.Where(x => x.SegmentName.ToLower().Trim() == tempsegmentname.ToLower().Trim() && x.CategoryID == objproductmodel.CategoryID).Count() > 0)
{
segid = db.Segments.Where(x => x.SegmentName.ToLower().Trim() == tempsegmentname.ToLower().Trim() && x.CategoryID == objproductmodel.CategoryID).FirstOrDefault().ID;
objproductmodel.SegmentID = segid;
}
else
{
objSegment.SegmentName = tempsegmentname;
objSegment.CategoryID = objproductmodel.CategoryID;
objSegment.IsActive = true;
objSegment.CreatedBy = 523;
objSegment.CreatedDate = DateTime.UtcNow.AddHours(5);
db.Segments.Add(objSegment);
db.SaveChanges();
if (db.Segments.Where(x => x.SegmentName.ToLower().Trim() == tempsegmentname.ToLower().Trim() && x.CategoryID == objproductmodel.CategoryID).Count() > 0)
{
segid = db.Segments.Where(x => x.SegmentName.ToLower().Trim() == tempsegmentname.ToLower().Trim() && x.CategoryID == objproductmodel.CategoryID).FirstOrDefault().ID;
objproductmodel.SegmentID = segid;
}
}
tempclassificationname = ds.Tables[0].Rows[i][2].ToString();
if (db.Sizes.Where(x => x.SizeName.ToLower().Trim() == tempclassificationname.ToLower().Trim() && x.CategoryID == objproductmodel.CategoryID && x.SegmentID == objproductmodel.SegmentID).Count() > 0)
{
classificationId = db.Sizes.Where(x => x.SizeName.ToLower().Trim() == tempclassificationname.ToLower().Trim() && x.CategoryID == objproductmodel.CategoryID && x.SegmentID == objproductmodel.SegmentID).FirstOrDefault().ID;
objproductmodel.SizeID = classificationId;
}
}
}
transection.Commit();
ds.Clear();
return Json(new { MessageType = 1, Message = "Record successfully inserted." });
}
catch (Exception ex)
{
transection.Rollback();
return Json(new { MessageType = 0, Message = ex.Message });
}