C# 处理批量插入数据(事物) mysql oracle sqlserver三个版本

来源:互联网 发布:今日头条数据运营待遇 编辑:程序博客网 时间:2024/05/29 10:09

 网上有人对于批处理进行了优化处理,500条一次事物提交,在处理数据量很大是确实很快,

但今天发现有个问题,最后一次的数据始终无法入库,最后发现是最后一次事物没有提交的原因。

希望大家在提交代码的时候,能够自己验证通过后再提交。


以下是项目中实际正在用的操作语句。提供SQL Server版本、MySQL版本以及Oracle版本事物处理。

数据的删除/插入/更新等语句。
入参如:

string sql="insert into t_test(testid,testname) values('1','大家好')";string sql2="insert into t_test(testid,testname) values('2','大家好,欢迎访问CSDN,http://my.csdn.net/wzcool273509239')";SQLStringList.Add(sql);SQLStringList.Add(sql2);



SQL Server版本

 /// <summary>          /// 执行多条SQL语句,实现数据库事务。          /// </summary>Sql数据库          /// <param name="SQLStringList">多条SQL语句</param>          public static int ExecuteSqlTran(List<string> SQLStringList)        {            using (SqlConnection conn = new SqlConnection(m_connectionString))            {                int result = 0;                conn.Open();                SqlCommand cmd = new SqlCommand();                cmd.Connection = conn;                SqlTransaction tx = conn.BeginTransaction();                cmd.Transaction = tx;                try                {                    for (int n = 0; n < SQLStringList.Count; n++)                    {                        string strsql = SQLStringList[n].ToString();                        if (strsql.Trim().Length > 1)                        {                            cmd.CommandText = strsql;                            result += cmd.ExecuteNonQuery();                        }                        //后来加上的                          if (n > 0 && (n % 500 == 0 || n == SQLStringList.Count - 1))                        {                            tx.Commit();                            //二次事务处理                            tx = conn.BeginTransaction();                            cmd.Transaction = tx;                        }                    }                    //最后一次提交(网上提供的这句话是被注释掉的,大爷的,错了。该句必须有,不然最后一个循环的数据无法提交)                    tx.Commit();                  }                catch (System.Data.SqlClient.SqlException E)                {                    DoBest.Common.LogHelper.WriteErrorLog(typeof(SQLHelper), E);                    result = -1;                    tx.Rollback();                    throw new Exception(E.Message);                }                catch (Exception ex)                {                    DoBest.Common.LogHelper.WriteErrorLog(typeof(SQLHelper), ex);                    result = -1;                    tx.Rollback();                    throw new Exception(ex.Message);                }                return result;            }        }

MySQL版本

   /// <summary>          /// 执行多条SQL语句,实现数据库事务。          /// </summary>mysql数据库          /// <param name="SQLStringList">多条SQL语句</param>          public static int ExecuteSqlTran(List<string> SQLStringList)        {            using (MySqlConnection conn = new MySqlConnection(m_connectionString))            {                int result = 0;                conn.Open();                MySqlCommand cmd = new MySqlCommand();                cmd.Connection = conn;                MySqlTransaction tx = conn.BeginTransaction();                cmd.Transaction = tx;                try                {                    for (int n = 0; n < SQLStringList.Count; n++)                    {                        string strsql = SQLStringList[n].ToString();                        if (strsql.Trim().Length > 1)                        {                            cmd.CommandText = strsql;                            result += cmd.ExecuteNonQuery();                        }                        //后来加上的,防止数据量过大,事务卡死现象                        if (n > 0 && (n % 500 == 0 || n == SQLStringList.Count - 1))                        {                            tx.Commit();                            //二次事务处理                              tx = conn.BeginTransaction();                            cmd.Transaction = tx;                        }                    }                    //最后一次提交(网上提供的这句话是被注释掉的,大爷的,错了。该句必须有,不然最后一个循环的数据无法提交)                    tx.Commit();                   }                catch (System.Data.SqlClient.SqlException E)                {                    result = -1;                    tx.Rollback();                    throw new Exception(E.Message);                }                catch (Exception ex)                {                    result = -1;                    tx.Rollback();                    throw new Exception(ex.Message);                }                return result;            }        }

Oracle版本

        /// <summary>          /// 执行多条SQL语句,实现数据库事务。          /// </summary>        /// <param name="SQLStringList">多条SQL语句</param>          public int ExecuteSqlTran(List<string> SQLStringList)        {            try            {                using (OracleConnection conn = new OracleConnection(m_ConnectString))                {                    int result = 0;                    conn.Open();                    OracleCommand cmd = new OracleCommand();                    cmd.Connection = conn;                    OracleTransaction tx = conn.BeginTransaction();                    cmd.Transaction = tx;                    try                    {                        for (int n = 0; n < SQLStringList.Count; n++)                        {                            string strsql = SQLStringList[n].ToString();                            if (strsql.Trim().Length > 1)                            {                                cmd.CommandText = strsql;                                result += cmd.ExecuteNonQuery();                            }                            //后来加上的  ,500条提交一次事务,防止数据量过大,程序卡死                            if (n > 0 && (n % 500 == 0 || n == SQLStringList.Count - 1))                            {                                tx.Commit();                                tx = conn.BeginTransaction();                            }                        }                        //最后一次提交(网上提供的这句话是被注释掉的,大爷的,错了。该句必须有,不然最后一个循环的数据无法提交)                        tx.Commit();                      }                    catch (System.Data.SqlClient.SqlException E)                    {                        LogLib.LogHelper.WriteErrorLog(typeof(OracleHelper), E);                        result = -1;                        tx.Rollback();                    }                    catch (Exception ex)                    {                        LogLib.LogHelper.WriteErrorLog(typeof(OracleHelper), ex);                        result = -1;                        tx.Rollback();                    }                    return result;                }            }            catch (Exception ex)            {                LogLib.LogHelper.WriteErrorLog(typeof(OracleHelper), ex);                return -1;            }        }














1 0
原创粉丝点击