如何使用MySQL实现批量插入数据(Bulk Insert)?
作者:小确幸
时间:2023-04-26
来源:互联网
浏览:0
一、新建项目:SqlSugarDemo二、连接串未添加AllowLoadLocalInfile=true中文提示:BulkCopyMySql连接字符串需要添加AllowLoadLocalInfile=true;添加后如果还不行Mysql数据库执行一下SETGLOBALlocal_infile=1EnglishMessage:connectionstringadd:AllowLoadLocalInfile=trueshowglobalvariableslike'local_infile';S
一、新建项目:SqlSugarDemo
二、连接串未添加AllowLoadLocalInfile=true

中文提示 : 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(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 _logger;
private readonly ISqlSugarClient _sqlSugarClient;
public HomeController(ILogger logger, ISqlSugarClient sqlSugarClient)
{
_logger = logger;
_sqlSugarClient = sqlSugarClient;
}
public IActionResult Index()
{
_sqlSugarClient.Fastest().BulkCopy(GetList());
return View();
}
public List GetList()
{
var datas = new List();
for (int i = 0; i < 10000; i++)
{
datas.Add(new RealmAuctionDatum { Name = Guid.NewGuid().ToString("N") });
}
return datas;
}
}
} 
作者最新文章
Windows 10
2026-09-16 17:44
Python安装后怎么打开:使用IDLE或命令行启动解释器
2026-09-16 13:54
Windows系统Python安装教程:下载、勾选PATH及环境变量配置
2026-09-16 13:53
“等灯不计时”落地解析:算法善意如何转化为技术能力与生态协同
2026-09-08 18:03
英伟达推出NVHBM:定制HBM带宽提升30%并扩展NVLink Fusion生态
2026-09-08 17:31
热门文章
更多
精品专题
更多
Mac软件
更多
WINDOWS
更多
Windows 10
Windows
Windows 10 是一款微软推出的经典操作系统,拥有硬件兼容性与多任务处理能力。它更偏向把系统状态查看和常用调节动作放在一起,适合需要持续观察和微调设备状态的场景。
极度公式
Windows/macOS/Linux
极度公式是一款跨平台专业LaTeX公式识别编辑软件,支持OCR公式识别和多平台编辑。和使用说明,避免使用,享受完整功能与稳定支持。做扫描整理、文字提取和表格转换时,它能把识别后的处理步骤接得更顺,资料录入这类场景会省下不少时间。
















