C# 大批量向数据库添加数据

       private static void SqlFastInsert(DbConnection dbConnection, DbTransaction trans, DataTable dt)
        {
            using (SqlBulkCopy sqlbulkcopy = new SqlBulkCopy(dbConnection as SqlConnection, SqlBulkCopyOptions.Default, trans as SqlTransaction))
            {
                sqlbulkcopy.DestinationTableName = dt.TableName;
                for (int i = 0; i < dt.Columns.Count; i++)
                {
                    sqlbulkcopy.ColumnMappings.Add(dt.Columns[i].ColumnName, dt.Columns[i].ColumnName);
                }
                sqlbulkcopy.BatchSize = 10000;
                sqlbulkcopy.WriteToServer(dt);
            }
        }
        private static void MySqlFastInsert(DbConnection dbConnection, DbTransaction trans, DataTable dt)
        {
            MySqlConnector.MySqlBulkCopy mySqlbulkcopy = new MySqlConnector.MySqlBulkCopy(dbConnection as MySqlConnector.MySqlConnection, trans as MySqlConnector.MySqlTransaction);
            {
                mySqlbulkcopy.DestinationTableName = dt.TableName;
                for (int i = 0; i < dt.Columns.Count; i++)
                {
                    mySqlbulkcopy.ColumnMappings.Add(new MySqlConnector.MySqlBulkCopyColumnMapping(i, dt.Columns[i].ColumnName));
                }
                mySqlbulkcopy.WriteToServer(dt);
            }
        }
        /// <summary>
        /// Oracle快速写入
        /// </summary>
        /// <param name="dbConnection"></param>
        /// <param name="trans"></param>
        /// <param name="dt"></param>
        private static void OracelFastInsert(DbConnection dbConnection, DbTransaction trans, DataTable dt)
        {
            Oracle.ManagedDataAccess.Client.OracleBulkCopy oraclebulkcopy = new Oracle.ManagedDataAccess.Client.OracleBulkCopy(dbConnection as Oracle.ManagedDataAccess.Client.OracleConnection, Oracle.ManagedDataAccess.Client.OracleBulkCopyOptions.Default);
            {

                oraclebulkcopy.DestinationTableName = dt.TableName;
                for (int i = 0; i < dt.Columns.Count; i++)
                {
                    oraclebulkcopy.ColumnMappings.Add(dt.Columns[i].ColumnName, dt.Columns[i].ColumnName);
                }
                oraclebulkcopy.BatchSize = 10000;
                oraclebulkcopy.WriteToServer(dt);
            }
        }

  

posted @ 2022-09-20 11:26  龙丶谈笑风声  阅读(611)  评论(0编辑  收藏  举报