我在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
}
}
}
}
}
}
您似乎使用經典的RBAR方法。難怪它需要時間。爲什麼你不能在數據庫中進行驗證?然後你可以做一個批量插入 – Raj
200,000行應該可以在幾秒或更少的時間內插入。你能顯示你使用的代碼嗎? –
DB在做什麼樣的驗證?爲什麼它不能同時驗證所有200k行?您可以使用批量複製列映射有效地將它們插入到SQL Server:http://stackoverflow.com/a/20108861/2538939 –