mysql批量插入BulkCopy的实现

作者:大熊程序猿 时间:2024-01-28 14:53:12 

一、新建项目:SqlSugarDemo

<ItemGroup>
   <PackageReference Include="SqlSugarCore" Version="5.1.3.52" />
 </ItemGroup>

二、连接串未添加AllowLoadLocalInfile=true

mysql批量插入BulkCopy的实现

中文提示 : BulkCopy MySql连接字符串需要添加 AllowLoadLocalInfile=true; 添加后如果还不行Mysql数据库执行一下 SET GLOBAL local_infile=1 
English Message : connection string add : AllowLoadLocalInfile=true

show global variables like 'local_infile';
SET GLOBAL local_infile=1

 三、Startup.cs

using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
using SqlSugar;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;

namespace WebApplication3
{
   public class Startup
   {
       public Startup(IConfiguration configuration)
       {
           Configuration = configuration;
       }

public IConfiguration Configuration { get; }

// This method gets called by the runtime. Use this method to add services to the container.
       public void ConfigureServices(IServiceCollection services)
       {
           services.AddSingleton<ISqlSugarClient>(s =>
           {
               SqlSugarScope sqlSugar = new SqlSugarScope(new ConnectionConfig()
               {
                   DbType = SqlSugar.DbType.MySql,
                   ConnectionString = "Server=192.168.31.132;User ID=root;Password=123456;Database=sugar;port=3306;AllowLoadLocalInfile=true",
                   IsAutoCloseConnection = true,
               },
              db =>
              {
                  //单例参数配置,所有上下文生效
                  db.Aop.OnLogExecuting = (sql, pars) =>
                  {
                  };
              });
               return sqlSugar;
           });

services.AddControllersWithViews();
       }

// This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
       public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
       {
           if (env.IsDevelopment())
           {
               app.UseDeveloperExceptionPage();
           }
           else
           {
               app.UseExceptionHandler("/Home/Error");
           }
           app.UseStaticFiles();

app.UseRouting();

app.UseAuthorization();

app.UseEndpoints(endpoints =>
           {
               endpoints.MapControllerRoute(
                   name: "default",
                   pattern: "{controller=Home}/{action=Index}/{id?}");
           });
       }
   }
}

HomeController.cs

using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Logging;
using SqlSugar;
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Threading.Tasks;
using WebApplication3.Models;

namespace WebApplication3.Controllers
{
   public class HomeController : Controller
   {
       private readonly ILogger<HomeController> _logger;
       private readonly ISqlSugarClient _sqlSugarClient;
       public HomeController(ILogger<HomeController> logger, ISqlSugarClient sqlSugarClient)
       {
           _logger = logger;
           _sqlSugarClient = sqlSugarClient;
       }

public IActionResult Index()
       {
           _sqlSugarClient.Fastest<RealmAuctionDatum>().BulkCopy(GetList());
           return View();
       }
       public List<RealmAuctionDatum> GetList()
       {
           var datas = new List<RealmAuctionDatum>();
           for (int i = 0; i < 10000; i++)
           {
               datas.Add(new RealmAuctionDatum { Name = Guid.NewGuid().ToString("N") });
           }
           return datas;
       }
   }
}

mysql批量插入BulkCopy的实现

来源:https://blog.csdn.net/xiaoxionglove/article/details/129270945

标签:mysql,批量插入,BulkCopy
0
投稿

猜你喜欢

  • python百行代码自制电脑端网速悬浮窗的实现

    2023-02-03 21:52:57
  • SQL Server误区30日谈 第7天 一个实例多个镜像和日志传送延迟

    2024-01-16 19:01:01
  • 微信公众号接入ChatGPT机器人的方法

    2023-11-19 22:05:42
  • Webpack中的文件指纹的实现

    2024-04-10 11:00:17
  • SQL Server AlwaysOn读写分离配置图文教程

    2024-01-19 10:20:20
  • 让大家看看Object标签的强大功能---多用途

    2009-02-21 10:18:00
  • Python read函数按字节(字符)读取文件的实现

    2021-08-20 09:40:48
  • keras获得某一层或者某层权重的输出实例

    2023-04-06 10:54:27
  • python 列表推导式使用详解

    2021-10-13 01:53:28
  • Python中新式类与经典类的区别详析

    2022-02-04 05:11:16
  • Sublime中View in Browser功能不生效问题及解决

    2022-07-03 05:48:16
  • 基于golang uint8、int8与byte的区别说明

    2024-05-09 09:56:03
  • MySQL order by与group by查询优化实现详解

    2024-01-24 23:27:48
  • 使用python实现定时报天气的示例代码

    2021-12-27 14:56:10
  • Python 比较文本相似性的方法(difflib,Levenshtein)

    2022-01-29 00:19:17
  • 定义列表 dt dl

    2008-08-03 17:14:00
  • PHP加密函数 Javascript/Js 解密函数

    2023-06-15 18:03:03
  • vue.js中使用echarts实现数据动态刷新功能

    2024-04-22 12:53:19
  • Go语言sync.Cond基本使用及原理示例详解

    2023-06-28 07:09:01
  • 如何使用Python实现自动化水军评论

    2022-08-25 21:15:48
  • asp之家 网络编程 m.aspxhome.com