mysql批量插入BulkCopy的实现

一、新建项目:SqlSugarDemo

  1.      <ItemGroup>
  2.      <PackageReference Include=“SqlSugarCore” Version=“5.1.3.52” />
  3.      </ItemGroup>

二、连接串未添加AllowLoadLocalInfile=true

-1

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

  1. show global variables like ‘local_infile’;
  2. SET GLOBAL local_infile=1

 三、Startup.cs

  1. using Microsoft.ASPNetCore.Builder;
  2. using Microsoft.AspNetCore.Hosting;
  3. using Microsoft.Extensions.Configuration;
  4. using Microsoft.Extensions.DependencyInjection;
  5. using Microsoft.Extensions.Hosting;
  6. using SqlSugar;
  7. using System;
  8. using System.Collections.Generic;
  9. using System.Linq;
  10. using System.Threading.Tasks;
  11. namespace WebApplication3
  12. {
  13.      public class Startup
  14.      {
  15.          public Startup(IConfiguration configuration)
  16.          {
  17.              Configuration = configuration;
  18.          }
  19.          public IConfiguration Configuration { get; }
  20.          // This method gets called by the runtime. Use this method to add services to the container.
  21.          public void ConfigureServices(IServiceCollection services)
  22.          {
  23.              services.AddSingleton<ISqlSugarClient>(=>
  24.              {
  25.                  SqlSugarScope sqlSugar = new SqlSugarScope(new ConnectionConfig()
  26.                  {
  27.                      DbType = SqlSugar.DbType.MySql,
  28.                      ConnectionString = “Server=192.168.31.132;User ID=root;Password=123456;Database=sugar;port=3306;AllowLoadLocalInfile=true”,
  29.                      IsAutoCloseConnection = true,
  30.                  },
  31.                  db =>
  32.                  {
  33.                      //单例参数配置,所有上下文生效
  34.                      db.Aop.OnLogExecuting = (sql, pars) =>
  35.                      {
  36.                      };
  37.                  });
  38.                  return sqlSugar;
  39.              });
  40.              services.AddControllersWithViews();
  41.          }
  42.          // This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
  43.          public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
  44.          {
  45.              if (env.IsDevelopment())
  46.              {
  47.                  app.UseDeveloperExceptionPage();
  48.              }
  49.              else
  50.              {
  51.                  app.UseExceptionHandler(“/Home/Error”);
  52.              }
  53.              app.UseStaticFiles();
  54.              app.UseRouting();
  55.              app.UseAuthorization();
  56.              app.UseEndpoints(endpoints =>
  57.              {
  58.                  endpoints.MapControllerRoute(
  59.                      name: “default”,
  60.                      pattern: “{controller=Home}/{action=Index}/{id?}”);
  61.              });
  62.          }
  63.      }
  64. }

HomeController.cs

  1. using Microsoft.AspNetCore.Mvc;
  2. using Microsoft.Extensions.Logging;
  3. using SqlSugar;
  4. using System;
  5. using System.Collections.Generic;
  6. using System.Diagnostics;
  7. using System.Linq;
  8. using System.Threading.Tasks;
  9. using WebApplication3.Models;
  10. namespace WebApplication3.Controllers
  11. {
  12.      public class HomeController : Controller
  13.      {
  14.          private readonly ILogger<HomeController> _logger;
  15.          private readonly ISqlSugarClient _sqlSugarClient;
  16.          public HomeController(ILogger<HomeController> logger, ISqlSugarClient sqlSugarClient)
  17.          {
  18.              _logger = logger;
  19.              _sqlSugarClient = sqlSugarClient;
  20.          }
  21.          public IActionResult Index()
  22.          {
  23.              _sqlSugarClient.Fastest<RealmAuctionDatum>().BulkCopy(GetList());
  24.              return View();
  25.          }
  26.          public List<RealmAuctionDatum> GetList()
  27.          {
  28.              var datas = new List<RealmAuctionDatum>();
  29.              for (int i = 0; i < 10000; i++)
  30.              {
  31.                  datas.Add(new RealmAuctionDatum { Name = Guid.NewGuid().ToString(“N”) });
  32.              }
  33.              return datas;
  34.          }
  35.      }
  36. }

-2

到此这篇关于mysql批量插入BulkCopy的实现的文章就介绍到这了,更多相关mysql批量插入BulkCopy内容请搜索我们以前的文章或继续浏览下面的相关文章希望大家以后多多支持我们!

标签

发表评论