Note that there are some explanatory texts on larger screens.

plurals
  1. POUsing stored procedure to insert many records into database one by one, but it is too slow
    primarykey
    data
    text
    <p>I have a datagridview containing about 8000 records, and for each record, I call a SQL Server stored procedure to insert it into database, code like this:</p> <pre><code>for (int i = 0; i &lt; dgv_compare.Rows.Count; i++) { if (!DBCommands.sp_app_RatePlanDetail_Add( CarrierID, is_supplier == 1 ? 0 : 1, row.Prefix, row.RegionName, row.NewRate, row.FreeBlock, row.InitialBlock, row.RecycleBlock, row.ConnectionCharge, row.EnrollDate, row.ExpiryDate, row.isDisabled, row.TimeF1, row.TimeT1, row.Rate1, row.Block1Enabled, row.TimeF2, row.TimeT2, row.Rate2, row.Block2Enabled, row.TimeF3, row.TimeT3, row.Rate3, row.Block3Enabled, Operator, FlagShortOverLong, ref ErrCode, ref ErrMsg)) { //tb_log.Text += DBCommands.LastError + "\r\n"; MessageBox.Show(DBCommands.LastError); return; } } </code></pre> <p>the function <code>DBCommands.sp_app_RatePlanDetail_Add</code> is this form:</p> <pre><code>public static bool sp_app_RatePlanDetail_Add ( int CustID, int IsInBound, string Prefix, string RegionName, double RatePerMin, int FreeBlock, int InitialBlock, int RecycleBlock, double ConnectionCharge, DateTime EnrollDate, DateTime ExpiryDate, int isDisabled, TimeSpan TimeF1, TimeSpan TimeT1, double Rate1, int Block1Enabled, TimeSpan TimeF2, TimeSpan TimeT2, double Rate2, int Block2Enabled, TimeSpan TimeF3, TimeSpan TimeT3, double Rate3, int Block3Enabled, string Operator, int FlagShortOverLong, ref int ErrCode, ref string ErrMsg ) { SqlConnection conn = null; SqlCommand sqlCmd = null; try { try { conn = new SqlConnection(ConnectString); conn.Open(); if (conn.State != ConnectionState.Open) { LastErrorCode = -1; LastError = "Connect database error."; return false; } } catch (Exception ex) { LastErrorCode = -1; LastError = "Connect database error. " + ex.ToString(); return false; } try { sqlCmd = new SqlCommand("sp_app_RatePlanDetail_Add_0909", conn); sqlCmd.CommandTimeout = ExecuteTimeout; sqlCmd.CommandType = CommandType.StoredProcedure; AddParam(sqlCmd, "@CustID", SqlDbType.Int, 4, 10, 0, ParameterDirection.Input, CustID); AddParam(sqlCmd, "@IsInBound", SqlDbType.Int, 4, 10, 0, ParameterDirection.Input, IsInBound); AddParam(sqlCmd, "@Prefix", SqlDbType.VarChar, 60, 30, 0, ParameterDirection.Input, Prefix); AddParam(sqlCmd, "@RegionName", SqlDbType.VarChar, 400, 200, 0, ParameterDirection.Input, RegionName); AddParam(sqlCmd, "@RatePerMin", SqlDbType.Decimal, 13, 20, 10, ParameterDirection.Input, RatePerMin); AddParam(sqlCmd, "@FreeBlock", SqlDbType.Int, 4, 10, 0, ParameterDirection.Input, FreeBlock); AddParam(sqlCmd, "@InitialBlock", SqlDbType.Int, 4, 10, 0, ParameterDirection.Input, InitialBlock); AddParam(sqlCmd, "@RecycleBlock", SqlDbType.Int, 4, 10, 0, ParameterDirection.Input, RecycleBlock); AddParam(sqlCmd, "@ConnectionCharge", SqlDbType.Decimal, 9, 18, 8, ParameterDirection.Input, ConnectionCharge); AddParam(sqlCmd, "@EnrollDate", SqlDbType.DateTime, 8, 23, 3, ParameterDirection.Input, EnrollDate); AddParam(sqlCmd, "@ExpiryDate", SqlDbType.DateTime, 8, 23, 3, ParameterDirection.Input, ExpiryDate); AddParam(sqlCmd, "@isDisabled", SqlDbType.Int, 4, 10, 0, ParameterDirection.Input, isDisabled); AddParam(sqlCmd, "@TimeF1", SqlDbType.Time, 5, 16, 7, ParameterDirection.Input, TimeF1); AddParam(sqlCmd, "@TimeT1", SqlDbType.Time, 5, 16, 7, ParameterDirection.Input, TimeT1); AddParam(sqlCmd, "@Rate1", SqlDbType.Decimal, 13, 20, 10, ParameterDirection.Input, Rate1); AddParam(sqlCmd, "@Block1Enabled", SqlDbType.Int, 4, 10, 0, ParameterDirection.Input, Block1Enabled); AddParam(sqlCmd, "@TimeF2", SqlDbType.Time, 5, 16, 7, ParameterDirection.Input, TimeF2); AddParam(sqlCmd, "@TimeT2", SqlDbType.Time, 5, 16, 7, ParameterDirection.Input, TimeT2); AddParam(sqlCmd, "@Rate2", SqlDbType.Decimal, 13, 20, 10, ParameterDirection.Input, Rate2); AddParam(sqlCmd, "@Block2Enabled", SqlDbType.Int, 4, 10, 0, ParameterDirection.Input, Block2Enabled); AddParam(sqlCmd, "@TimeF3", SqlDbType.Time, 5, 16, 7, ParameterDirection.Input, TimeF3); AddParam(sqlCmd, "@TimeT3", SqlDbType.Time, 5, 16, 7, ParameterDirection.Input, TimeT3); AddParam(sqlCmd, "@Rate3", SqlDbType.Decimal, 13, 20, 10, ParameterDirection.Input, Rate3); AddParam(sqlCmd, "@Block3Enabled", SqlDbType.Int, 4, 10, 0, ParameterDirection.Input, Block3Enabled); AddParam(sqlCmd, "@Operator", SqlDbType.VarChar, 60, 30, 0, ParameterDirection.Input, Operator); AddParam(sqlCmd, "@FlagShortOverLong", SqlDbType.Int, 4, 10, 0, ParameterDirection.Input, FlagShortOverLong); AddParam(sqlCmd, "@ErrCode", SqlDbType.Int, 4, 10, 0, ParameterDirection.Output, null); AddParam(sqlCmd, "@ErrMsg", SqlDbType.VarChar, 100, 50, 0, ParameterDirection.Output, null); sqlCmd.Parameters.Add("@RETURN_VALUE", SqlDbType.Int, 4); sqlCmd.Parameters["@RETURN_VALUE"].Direction = ParameterDirection.ReturnValue; sqlCmd.ExecuteNonQuery(); ErrCode = get_int_value(sqlCmd.Parameters["@ErrCode"].Value); ErrMsg = get_string_value(sqlCmd.Parameters["@ErrMsg"].Value); int _temp = Convert.ToInt32(sqlCmd.Parameters["@RETURN_VALUE"].Value.ToString()); return true; } catch (Exception ex) { LastErrorCode = -1; LastError = "Execute sp_app_RatePlanDetail_Add error. " + ex.ToString(); return false; } } finally { if (sqlCmd != null) sqlCmd.Dispose(); if (conn != null) { conn.Close(); conn.Dispose(); } } } </code></pre> <p>because I need do some check and compare, so I can not save the datagridview to csv and insert the total set of data at once.</p> <p>In this procedure <code>sp_app_RatePlanDetail_Add_0909</code>, I do some update and delete, insert operator, although it can work, but it spend too much time</p> <p>I thought this maybe because the unoptimize update or delete, so I try to change the procedure <code>sp_app_RatePlanDetail_Add_0909</code>, let it only contain one statement, like this :</p> <pre><code> select @a=1 </code></pre> <p>but it is very strange, the speed of whole procedure seems does not change at all, it still spend much time, can anybody tell me what is the reason? thanks.</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload