Click here to Skip to main content
15,888,019 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
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 ?

C#
<pre>public static int ExecuteNonQuery(string sql, DbParameter[] dbprmParameters = null)
    {
        return ExecuteNonQuery(sql, null, dbprmParameters);
    }
    public static int ExecuteNonQuery(string sql, IDbConnection dbConnection, DbParameter[] @params = null)
    {
        int RecordsCount = 0;

        lock (synObj)
        {
         
            if (cmd.CommandTimeout < 360)
                cmd.CommandTimeout = 360;
            if (sql == "") return 0;

            sql = AnalyizeBooleanFields(sql);
            cmd.CommandText = sql;

            cmd.Parameters.Clear();

            if (@params != null)
            {
                for (int i = 0; i < @params.Length; i++)
                {
                    cmd.Parameters.Add(@params[i]);
                }
            }
            if (dbConnection == null)
            {
                if (WithTransaction)
                    dbConnection = BeginTransaction();
                else
                    dbConnection = InitializeConnection();
            }
            if (dbConnection.State != ConnectionState.Open) dbConnection.Open();
            if (WithTransaction) cmd.Transaction = _transaction;
            cmd.Connection = dbConnection;
            RecordsCount = cmd.ExecuteNonQuery();
            if (!WithTransaction) dbConnection.Close();
        }
        return RecordsCount;
    }

public static string AnalyizeBooleanFields(string sql)
    {
        switch (DataAccess.Provider)
        {
            case Providers.Access2003:
            case Providers.Access2007:
                sql = sql.Replace("{{1}}", "True");
                sql = sql.Replace("{{0}}", "False");
                break;
            case Providers.SQLServer:
            case Providers.MySQL:
            case Providers.Oracle:
            default:
                sql = sql.Replace("{{1}}", "1");
                sql = sql.Replace("{{0}}", "0");
                break;
        }
        return sql;
    }

}

How to edit function above to allow multi threads ?

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


What I have tried:

How to make function ExecuteNonQuery to use multi thread to prevent computer hangs ?
Posted
Updated 13-Apr-18 15:21pm
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900