2015-06-26 69 views
0

我在c#Winforms gridview中有超过200,000条记录,需要大约一个小时才能插入到我的数据库中。我正在尝试改进此插入的性能。我期望在5到10分钟内插入所有记录。需要很长时间才能在SQL Server 2008 R2中插入行

我正在使用For循环来填充每一行,以便使用SQL事务插入到DB中,并且我不认为SqlBulkCopy会生效,因为在插入DB之前需要使用DB验证所有200,000条记录。

 Save-Function: 
    if (chkretailprice.Checked) 
     { 
        DataTable dt_grid = (DataTable)gcPromotion.DataSource; 
        dt_grid.AcceptChanges(); 

        for (int tt = 0; tt < gvPromotion.RowCount; tt++) 
        { 
         gvPromotion.FocusedRowHandle = tt; 
         double dRGridMinus = Convert.ToDouble(gvPromotion.GetRowCellValue(tt, gvPromotion.Columns["PromotionalRetailPrice"])); 
         string sItem = Convert.ToString(gvPromotion.GetRowCellValue(tt, gvPromotion.Columns["ItemName"]).ToString()); 
         string sPack = Convert.ToString(gvPromotion.GetRowCellValue(tt, gvPromotion.Columns["Package"]).ToString()); 

         if (dRGridMinus < 0) 
         { 
          gvPromotion.FocusedRowHandle = tt; 
          MessageBoxInfo("Promotional RetailPrice contains Negative Values for this ItemName-'" + sItem + "' & Package-'" + sPack + "'"); 
          gvPromotion.Focus(); 
          return; 
         } 
        } 
        int iReCount = dt_grid.Select("PromotionalRetailPrice='0.00'").Length; 

        if (iReCount != 0) 
        { 
         MessageBoxInfo("Promotional RetailPrice Must not be 0"); 
         gvPromotion.Focus(); 
         return; 
        } 
     } 
     if (rgPromotion.Checked) 
     { 
         for (int p = 0; p < gvPromotion.RowCount; p++) 
         { 
          string[] sbranchArr = sBranchIDs.Split(','); 
          for (int pp = 0; pp < sbranchArr.Length; pp++) 
          { 
           objProEntity.PromotionMasterId = objProEntity.PromotionMasterId; 
           objProEntity.BranchId = Convert.ToInt32(sbranchArr[pp]);//gvPromotion.GetRowCellValue(p, gvPromotion.Columns["BranchID"])); 
           objProEntity.ItemId = Convert.ToInt64(gvPromotion.GetRowCellValue(p, gvPromotion.Columns["ItemID"])); 
           objProEntity.PackId = Convert.ToInt32(gvPromotion.GetRowCellValue(p, gvPromotion.Columns["PackTypeID"])); 
           objProEntity.PromotionValueType = Convert.ToString(gvPromotion.GetRowCellValue(p, gvPromotion.Columns["PromotionValueType"])); 
           objProEntity.PromotionValue = Convert.ToString(gvPromotion.GetRowCellValue(p, gvPromotion.Columns["PromotionValue"])); 

           if (chkretailprice.Checked && chkwholesaleprice.Checked)// when both retailprice & wholesaleprice checkbox is checked 
           { 
            objProEntity.ActualRetailPrice = Convert.ToDecimal(gvPromotion.GetRowCellValue(p, gvPromotion.Columns["ActualRetailPrice"])); 
            objProEntity.PromoRetailPrice = Convert.ToDecimal(gvPromotion.GetRowCellValue(p, gvPromotion.Columns["PromotionalRetailPrice"])); 
            objProEntity.ActualWholeSalePrice = Convert.ToDecimal(gvPromotion.GetRowCellValue(p, gvPromotion.Columns["ActualWholeSalePrice"])); 
            objProEntity.PromoWholesalePrice = Convert.ToDecimal(gvPromotion.GetRowCellValue(p, gvPromotion.Columns["PromotionalWholeSalePrice"])); 
           } 
           else if (chkretailprice.Checked)// when retailprice checkbox is checked 
           { 

            objProEntity.ActualRetailPrice = Convert.ToDecimal(gvPromotion.GetRowCellValue(p, gvPromotion.Columns["ActualRetailPrice"])); 
            objProEntity.PromoRetailPrice = Convert.ToDecimal(gvPromotion.GetRowCellValue(p, gvPromotion.Columns["PromotionalRetailPrice"])); 
            objProEntity.ActualWholeSalePrice = Convert.ToDecimal(gvPromotion.GetRowCellValue(p, gvPromotion.Columns["ActualWholeSalePrice"])); 
            objProEntity.PromoWholesalePrice = Convert.ToDecimal(0); 
           } 
           else if (chkwholesaleprice.Checked)// when wholesaleprice checkbox is checked 
           { 
            objProEntity.ActualRetailPrice = Convert.ToDecimal(gvPromotion.GetRowCellValue(p, gvPromotion.Columns["ActualRetailPrice"])); 
            objProEntity.PromoRetailPrice = Convert.ToDecimal(0); 
            objProEntity.ActualWholeSalePrice = Convert.ToDecimal(gvPromotion.GetRowCellValue(p, gvPromotion.Columns["ActualWholeSalePrice"])); 
            objProEntity.PromoWholesalePrice = Convert.ToDecimal(gvPromotion.GetRowCellValue(p, gvPromotion.Columns["PromotionalWholeSalePrice"])); 
           } 
           objProEntity.DiscountAllowed = Convert.ToBoolean(gvPromotion.GetRowCellValue(p, gvPromotion.Columns["DiscountAllowed"])); 

           DataTable dt_Check = new DataTable(); 
           dt_Check = SalesPromotionData.IsCheckItemExists(objProEntity, SQLTrans);          
           if (dt_Check.Rows.Count == 0) 
           { 
            if (!IsEdit) 
            { 
             DataTable dt_child = SalesPromotionData.InsertChildData(objProEntity, SQLTrans); // Insert Child Details when isEdit=false 
            } 
            else 
            { 
             if (gvPromotion.Columns.Contains(gvPromotion.Columns["PromotionChildId"])) 
              if ((DBNull.Value.Equals(gvPromotion.GetRowCellValue(p, gvPromotion.Columns["PromotionChildId"]))) || (gvPromotion.GetRowCellValue(p, gvPromotion.Columns["PromotionChildId"]) == "") || Convert.ToString(gvPromotion.GetRowCellValue(p, gvPromotion.Columns["PromotionChildId"]).ToString()) == "0") 
              { 
               objProEntity.PromotionMasterId = masterid; 
               SalesPromotionData.InsertChildData(objProEntity, SQLTrans);// insert child details when isEdit=true 
              } 
              else 
              { 
               objProEntity.PromotionChildId = Convert.ToInt64(gvPromotion.GetRowCellValue(p, gvPromotion.Columns["PromotionChildId"]).ToString()); 
               SalesPromotionData.UpdateChildDetails(objProEntity, SQLTrans); // update child details when isEdit=true 
              } 
             else 
             { 
              objProEntity.PromotionMasterId = masterid; 
              SalesPromotionData.InsertChildData(objProEntity, SQLTrans);// insert child details when isEdit=true 
             } 
            } 
           } 
          } 
         } 
    } 
+1

您似乎使用经典的RBAR方法。难怪它需要时间。为什么你不能在数据库中进行验证?然后你可以做一个批量插入 – Raj

+1

200,000行应该可以在几秒或更少的时间内插入。你能显示你使用的代码吗? –

+0

DB在做什么样的验证?为什么它不能同时验证所有200k行?您可以使用批量复制列映射有效地将它们插入到SQL Server:http://stackoverflow.com/a/20108861/2538939 –

回答

1

通常情况下,你会通过主体镜台您的数据到数据库中其插入到[A]工作表(S),没有引用完整性或任何东西 - 只是原始数据加上任何[非唯一]你可能需要的指数。一旦你得到了它上演,您可以:

  • 验证数据在工作表(S)对你的数据库和
  • 它适用于有关“真正的”表。
+0

是的,这也是我的看法。在“原始表”中根本没有索引是很好的,因为每个索引都会减慢插入。另外,你在做什么,记录被拒绝。最好快速将所有内容加载到原始表中,然后在数据库引擎中进一步处理。 –

相关问题