以更高效的方式将数据从excel导入数据库

时间:2017-08-17 15:05:49

标签: c# asp.net .net

我有一个包含7-8列和800-1200个的excel文件,需要进行验证并插入到数据库中,我所做的是将excel加载到OleDB Datareader并沿着行读取。检查数据并验证并加载到数据表,然后使用SqlBulkCopy将它们写入服务器。

string ConnectionStr = string.Empty;

if (extension.ToUpper() == ".XLS")
{
   ConnectionStr = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + Server.MapPath("~/Cookies/BatchFile/" + FileName) + ";Extended Properties=Excel 8.0";
}
else
{
   ConnectionStr = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" + Server.MapPath("~/Cookies/BatchFile/" + FileName) + ";Extended Properties=Excel 12.0";
}

DataTable Err = new DataTable();
dter.Columns.Add("MainCode", typeof(string));
dter.Columns.Add("Remarks", typeof(string));

DataTable DtASBAMaster = new DataTable();
DtASBAMaster.Columns.Add("ASBARef");
DtASBAMaster.Columns.Add("ClientCode");
DtASBAMaster.Columns.Add("BranchCode");
DtASBAMaster.Columns.Add("ShareHolderAc");
DtASBAMaster.Columns.Add("CyCode");
DtASBAMaster.Columns.Add("AcType");
DtASBAMaster.Columns.Add("DMATId");
DtASBAMaster.Columns.Add("ClientId");
DtASBAMaster.Columns.Add("BOId");
DtASBAMaster.Columns.Add("BankCode");
DtASBAMaster.Columns.Add("OpenDate");
DtASBAMaster.Columns.Add("EnteredBy");
DtASBAMaster.Columns.Add("ApprovedBy");
DtASBAMaster.Columns.Add("Name");
DtASBAMaster.Columns.Add("ASBAStatus");

DataTable Dtimport = new DataTable();
Dtimport.Columns.Add("BatchNo");
Dtimport.Columns.Add("BranchCode");
Dtimport.Columns.Add("AcType");
Dtimport.Columns.Add("CyCode");
Dtimport.Columns.Add("MainCode");
Dtimport.Columns.Add("TranDate");
Dtimport.Columns.Add("NoOfShare");
Dtimport.Columns.Add("Name");
Dtimport.Columns.Add("BankCode");
Dtimport.Columns.Add("ThisBranch");
Dtimport.Columns.Add("RegNo");
Dtimport.Columns.Add("DMATId");
Dtimport.Columns.Add("ClientId");
Dtimport.Columns.Add("TakeCharge");

while (odr.Read())
{                       
   //Check values against next table to database -->Database trip 1
   SqlDataReader dr = DataAccess.ASBA.DA_Asba.GetMainCodeAsba(Session["ConnSTR"].ToString(), mainCode: MainCode);
   if (dr.Read())
   {
      //Perform Action if value are inseted in next table 
   }
   else
   {
      if (New == "T")
      {
         //Fetch Details from someother table in database to load in next table
         //add to datatable DtASBAMaster

        else {
                //Add to  datatable Err
                continue;
             }
      }

      //Calculation and stuffs 
      if (!GetBalance(MainCode, TotalAmount.ToString(), IsChargeASBA))
      {
         //Add to  datatable Err
      }
      else
      {
         if (!MainCodeProcessed.Add(MainCode))
         {
            dter.Rows.Add(MainCode, "Dublicate Maincode in the Batch");
            continue;
         }

         Dtimport.Rows.Add(BatchNo, BranchCode, AcType, CyCode, MainCode, SimpleHTTPModule.Today.ToString(), NoOfShare, Name, Session["BranchCodeSelected"].ToString(), Session["BranchCodeSelected"].ToString(), RegisterNo, Dmate, Clientid, IsChargeASBA);

    }

 }
 SqlTransaction myTrans = null;
 conn.Open();
 SqlCommand cmd = new SqlCommand();
 myTrans = conn.BeginTransaction();
 cmd.Connection = conn;
 cmd.Transaction = myTrans;
 try {
       successcount = Dtimport.Rows.Count;
       if (successcount > 0)
       {
          string Qrystring = @"INSERT INTO ASBABatApplyTable(BatchNo,BranchCode,BatchDetails,IsPrinted,IsApproved,EnteredBy,CompanyCode,IsuuedDate,NoOfTrans) values(@BatchNo,@BranchCode,@BatchDetails,@IsPrinted,@IsApproved,@EnteredBy,@CompanyCode,@IsuuedDate,@NoOfTrans)";
          ////Qrystring = Qrystring + " VALUES('" + BatchNo + "','" + BatchDetails + "','F','F','" + EnteredBy + "')";

          cmd.CommandText = Qrystring;
          cmd.Parameters.AddWithValue("@BatchNo", BatchNo);
          cmd.Parameters.AddWithValue("@BranchCode", Session["BranchCodeSelected"].ToString());//
          cmd.Parameters.AddWithValue("@BatchDetails", txtBatchDesc.Text);
          cmd.Parameters.AddWithValue("@IsPrinted", "F");
          cmd.Parameters.AddWithValue("@IsApproved", "F");
          cmd.Parameters.AddWithValue("@EnteredBy", EnteredBy);//
          cmd.Parameters.AddWithValue("@CompanyCode", txtCompanyCode.Text);
          cmd.Parameters.AddWithValue("@IsuuedDate", SimpleHTTPModule.Today.ToShortDateString());//
          cmd.Parameters.AddWithValue("@NoOfTrans", successcount);//
          cmd.ExecuteNonQuery();

          //bulk copy
          SqlBulkCopy tblcpyASBAMaster= new SqlBulkCopy(conn, SqlBulkCopyOptions.Default,myTrans);
          tblcpyASBAMaster.ColumnMappings.Add("ASBARef", "ASBARef");
          tblcpyASBAMaster.ColumnMappings.Add("ClientCode", "ClientCode");
          tblcpyASBAMaster.ColumnMappings.Add("BranchCode", "BranchCode");
          tblcpyASBAMaster.ColumnMappings.Add("ShareHolderAc", "ShareHolderAc");
          tblcpyASBAMaster.ColumnMappings.Add("CyCode", "CyCode");
          tblcpyASBAMaster.ColumnMappings.Add("AcType", "AcType");
          tblcpyASBAMaster.ColumnMappings.Add("DMATId", "DMATId");
          tblcpyASBAMaster.ColumnMappings.Add("ClientId", "ClientId");
          tblcpyASBAMaster.ColumnMappings.Add("BOId", "BOId");
          tblcpyASBAMaster.ColumnMappings.Add("BankCode", "BankCode");
          tblcpyASBAMaster.ColumnMappings.Add("OpenDate", "OpenDate");
          tblcpyASBAMaster.ColumnMappings.Add("EnteredBy", "EnteredBy");
          tblcpyASBAMaster.ColumnMappings.Add("ApprovedBy", "ApprovedBy");
          tblcpyASBAMaster.ColumnMappings.Add("Name", "Name");
          tblcpyASBAMaster.ColumnMappings.Add("ASBAStatus", "ASBAStatus");
          tblcpyASBAMaster.DestinationTableName = "ASBAMaster";
          tblcpyASBAMaster.WriteToServer(DtASBAMaster);

          SqlBulkCopy tblcpy = new SqlBulkCopy(conn,SqlBulkCopyOptions.Default, myTrans);
          tblcpy.ColumnMappings.Add("BatchNo", "BatchNo");
          tblcpy.ColumnMappings.Add("BranchCode", "BranchCode");
          tblcpy.ColumnMappings.Add("AcType", "AcType");
          tblcpy.ColumnMappings.Add("CyCode", "CyCode");
          tblcpy.ColumnMappings.Add("MainCode", "MainCode");
          tblcpy.ColumnMappings.Add("TranDate", "TranDate");
          tblcpy.ColumnMappings.Add("NoOfShare", "NoOfShare");
          tblcpy.ColumnMappings.Add("Name", "Name");
          tblcpy.ColumnMappings.Add("BankCode", "BankCode");
          tblcpy.ColumnMappings.Add("ThisBranch", "ThisBranch");
          tblcpy.ColumnMappings.Add("RegNo", "RegNo");
          tblcpy.ColumnMappings.Add("DMATId", "DMATId");
          tblcpy.ColumnMappings.Add("ClientId", "ClientId");
          tblcpy.ColumnMappings.Add("TakeCharge", "TakeCharge");
          tblcpy.DestinationTableName = "ASBABatApplyTran";
          tblcpy.WriteToServer(Dtimport);
          myTrans.Commit();
          Session["Imported"] = "IMPORTED :" + successcount + " Records";           
      }             
  }

目前,加载excel需要大约12-15分钟,而当数据库使用时需要更长时间。如何优化它以便更好地执行?

P.S随意使用其他/更好的方式来编写代码和练习,并建议我在哪里做错了。谢谢。

编辑: 所以经过代码分析后,我发现单个记录有4次数据库跳转并删除了那些不必要的数据库并将所有针对数据库值的验证提升到sql函数以将数据库跳转减少到1.之后导入速度相对较快

非常感谢所有人的挚爱。 :)

0 个答案:

没有答案