温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

mysql批量插入BulkCopy如何实现

发布时间:2023-03-30 13:56:29 来源:亿速云 阅读:126 作者:iii 栏目:开发技术

这篇文章主要介绍了mysql批量插入BulkCopy如何实现的相关知识,内容详细易懂,操作简单快捷,具有一定借鉴价值,相信大家阅读完这篇mysql批量插入BulkCopy如何实现文章都会有所收获,下面我们一起来看看吧。

一、新建项目: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如何实现

关于“mysql批量插入BulkCopy如何实现”这篇文章的内容就介绍到这里,感谢各位的阅读!相信大家对“mysql批量插入BulkCopy如何实现”知识都有一定的了解,大家如果还想学习更多知识,欢迎关注亿速云行业资讯频道。

向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI