>  기사  >  데이터 베이스  >  mysql 일괄 삽입 대량 복사 구현 방법

mysql 일괄 삽입 대량 복사 구현 방법

WBOY
WBOY앞으로
2023-06-02 10:37:051937검색

1. 새 프로젝트: SqlSugarDemo

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

2. AllowLoadLocalInfile=true가 연결 문자열에 추가되지 않습니다

mysql 일괄 삽입 대량 복사 구현 방법

중국어 팁: BulkCopy MySql 연결 문자열을 추가한 후에도 여전히 작동하지 않으면 Mysql 데이터베이스에서 SET GLOBAL local_infile 실행 =1
English 메시지: 연결 문자열 추가: AllowLoadLocalInfile=true

show global variables like &#39;local_infile&#39;;
SET GLOBAL local_infile=1

3. 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 일괄 삽입 대량 복사 구현 방법

위 내용은 mysql 일괄 삽입 대량 복사 구현 방법의 상세 내용입니다. 자세한 내용은 PHP 중국어 웹사이트의 기타 관련 기사를 참조하세요!

성명:
이 기사는 yisu.com에서 복제됩니다. 침해가 있는 경우 admin@php.cn으로 문의하시기 바랍니다. 삭제