Oracle:EFCore使用OracleBulkCopy实现批量导入数据|C/S开发框架
作者:csframework|C/S框架网  发布日期:2024/10/20 11:49:21

Oracle:EFCore使用OracleBulkCopy实现批量导入数据|C/S开发框架

Oracle:EFCore使用OracleBulkCopy实现批量导入数据

CSFramework.EF.OracleDatabase

BulkInsert 方法:

C# 全选
        public int BulkInsert<T>(List<T> entities, string tableName = null)
        {
            if (String.IsNullOrEmpty(tableName))
            {
                var tableAttribute = (TableAttribute)typeof(T).GetCustomAttributes(typeof(TableAttribute), false).First();
                tableName = $"\"{tableAttribute.Name}\"";
            }

            using (OracleBulkCopy bulkCopy = new OracleBulkCopy(ConnectionString))
            {
                bulkCopy.BulkCopyTimeout = 0;
                bulkCopy.BatchSize = entities.Count;
                bulkCopy.DestinationTableName = tableName;
                bulkCopy.BulkCopyOptions = OracleBulkCopyOptions.UseInternalTransaction | OracleBulkCopyOptions.Default;

                var table = DataTableHelper.ToDataTable<T>(entities);
                table.TableName = tableName;

                //重要:记录设置为新增状态
                foreach (DataRow r in table.Rows) r.SetAdded();

                bulkCopy.WriteToServer(table);

                return table.Rows.Count;
            }
        }

测试代码

C# 全选
            IDatabase db = DatabaseFactory.GetDatabase(GetDatabaseType(), GetConfig("Normal").ConnectionString);
            var watch = Stopwatch.StartNew();

            var list = new List<_Demo_Customer>();
            for (int i = 1; i <= 1000; i++)
            {
                list.Add(NewCustomer());
            }

            //调用IDatabase.BulkInsert方法
            int x = db.BulkInsert<_Demo_Customer>(list);

            watch.Stop();

            int count = db.GetQueryable<_Demo_Customer>().Count();
            var msg = $"成功:{x},总记录数:{count},用时:{watch.ElapsedMilliseconds}(ms)";

            ShowResult("测试BulkInsert方法 -> 结果:" + msg);

测试结果

测试BulkInsert方法 -> 结果:成功:1000,总记录数:8562,用时:115(ms)

----------

测试BulkInsert方法 -> 结果:成功:1000,总记录数:7562,用时:113(ms)

----------

测试BulkInsert方法 -> 结果:成功:1000,总记录数:6562,用时:248(ms)

----------

测试BulkInsert方法 -> 结果:成功:1000,总记录数:5562,用时:134(ms)

----------

测试BulkInsert方法 -> 结果:成功:1000,总记录数:4562,用时:1250(ms)

C/S框架网|原创精神.创造价值.打造精品


扫一扫加作者微信
C/S框架网作者微信 C/S框架网|原创作品.质量保障.竭诚为您服务
上一篇 下一篇