ahmed elbarbary

ahmed elbarbary

  • NA
  • 1.6k
  • 254.8k

How to make function ExecuteNonQuery to use multi thread to

Apr 13 2018 7:53 PM
How to make function ExecuteNonQuery to use multi thread to prevent computer hangs ?

I using function ExecuteNonQuery() for insert and update and delete using c# .

This function using to make insert and update more thousand of records may be 10000 records so that computer hangs

to solve this problem i need to modify function

below to allow multi threads

so that i need to modify function to allow multi threads

How i do that in code

  1. public static int ExecuteNonQuery(string sql, DbParameter[] dbprmParameters = null)  
  2.     {  
  3.         return ExecuteNonQuery(sql, null, dbprmParameters);  
  4.     }  
  5.     public static int ExecuteNonQuery(string sql, IDbConnection dbConnection, DbParameter[] @params = null)  
  6.     {  
  7.         int RecordsCount = 0;  
  8.   
  9.         lock (synObj)  
  10.         {  
  11.            
  12.             if (cmd.CommandTimeout < 360)  
  13.                 cmd.CommandTimeout = 360;  
  14.             if (sql == ""return 0;  
  15.   
  16.             sql = AnalyizeBooleanFields(sql);  
  17.             cmd.CommandText = sql;  
  18.   
  19.             cmd.Parameters.Clear();  
  20.   
  21.             if (@params != null)  
  22.             {  
  23.                 for (int i = 0; i < @params.Length; i++)  
  24.                 {  
  25.                     cmd.Parameters.Add(@params[i]);  
  26.                 }  
  27.             }  
  28.             if (dbConnection == null)  
  29.             {  
  30.                 if (WithTransaction)  
  31.                     dbConnection = BeginTransaction();  
  32.                 else  
  33.                     dbConnection = InitializeConnection();  
  34.             }  
  35.             if (dbConnection.State != ConnectionState.Open) dbConnection.Open();  
  36.             if (WithTransaction) cmd.Transaction = _transaction;  
  37.             cmd.Connection = dbConnection;  
  38.             RecordsCount = cmd.ExecuteNonQuery();  
  39.             if (!WithTransaction) dbConnection.Close();  
  40.         }  
  41.         return RecordsCount;  
  42.     } 
    1. public static string AnalyizeBooleanFields(string sql)  
    2.     {  
    3.         switch (DataAccess.Provider)  
    4.         {  
    5.             case Providers.Access2003:  
    6.             case Providers.Access2007:  
    7.                 sql = sql.Replace("{{1}}""True");  
    8.                 sql = sql.Replace("{{0}}""False");  
    9.                 break;  
    10.             case Providers.SQLServer:  
    11.             case Providers.MySQL:  
    12.             case Providers.Oracle:  
    13.             default:  
    14.                 sql = sql.Replace("{{1}}""1");  
    15.                 sql = sql.Replace("{{0}}""0");  
    16.                 break;  
    17.         }  
    18.         return sql;  
    19.     }  
    20.   
    21. }  
     
 

How to edit function above to allow multi threads ?

I work in visual studio 2010 windows form application with sql server 2012 .


Answers (1)