EF結合SqlBulkCopy實現高效的批量數據插入(轉載)

批量插入 (17597條數據批量插入耗時1.7秒)

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;

namespace MvcApplication1.Controllers
{
    using MvcApplication1.Models;
    using EntityFramework.Extensions;
    using System.ComponentModel;
    using System.Data;
    using System.Data.SqlClient;
    using System.Diagnostics;
    public class HomeController : Controller
    {
        public ActionResult Index()
        {
            Stopwatch sw = new Stopwatch(); //計時器
            sw.Start();//開始計時

            using (var db = new salesEntities())
            {

                List<location> entitys = db.location.ToList(); //構建集合,到時候會將這個集合數據批量插入到

                if (db.Database.Connection.State != ConnectionState.Open)
                {
                    db.Database.Connection.Open(); //打開Connection鏈接
                }

                //調用BulkInsert方法,將entitys集合數據批量插入到數據庫的tolocation表中
                BulkInsert<location>((SqlConnection)db.Database.Connection, "tolocation", entitys);

                if (db.Database.Connection.State != ConnectionState.Closed)
                {
                    db.Database.Connection.Close(); //關閉Connection鏈接
                }
            }

            sw.Stop(); //結束計時
            string aa = sw.Elapsed.ToString();//批量插入了17597條數據。耗時1.7秒
            return View();
        }


        /// <summary>
        /// 批量插入
        /// </summary>
        /// <typeparam name="T">泛型集合的類型</typeparam>
        /// <param name="conn">鏈接對象</param>
        /// <param name="tableName">將泛型集合插入到本地數據庫表的表名</param>
        /// <param name="list">要插入大泛型集合</param>
        public static void BulkInsert<T>(SqlConnection conn, string tableName, IList<T> list)
        {
            using (var bulkCopy = new SqlBulkCopy(conn))
            {
                bulkCopy.BatchSize = list.Count;
                bulkCopy.DestinationTableName = tableName;

                var table = new DataTable();
                var props = TypeDescriptor.GetProperties(typeof(T))

                    .Cast<PropertyDescriptor>()
                    .Where(propertyInfo => propertyInfo.PropertyType.Namespace.Equals("System"))
                    .ToArray();

                foreach (var propertyInfo in props)
                {
                    bulkCopy.ColumnMappings.Add(propertyInfo.Name, propertyInfo.Name);
                    table.Columns.Add(propertyInfo.Name, Nullable.GetUnderlyingType(propertyInfo.PropertyType) ?? propertyInfo.PropertyType);
                }

                var values = new object[props.Length];
                foreach (var item in list)
                {
                    for (var i = 0; i < values.Length; i++)
                    {
                        values[i] = props[i].GetValue(item);
                    }

                    table.Rows.Add(values);
                }

                bulkCopy.WriteToServer(table);
            }
        }

    }
}
View Code

使用EF擴展EntityFramework.Extended 對數據進行批量更新,和批量刪除

首先去nuget上下載EntityFramework.Extended插件(搜索:EntityFramework.Extended) 安裝後,在項目中引入using EntityFramework.Extensions; 名稱空間

批量更新(17597條數據,批量更新耗時1.69秒)

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;

namespace MvcApplication1.Controllers
{
    using MvcApplication1.Models;
    using EntityFramework.Extensions; //使用EF的EntityFramework.Extended插件須要引入此名稱空間
    using System.Diagnostics;

    public class HomeController : Controller
    {
        public ActionResult Index()
        {
            salesEntities db = new salesEntities();

            Stopwatch sw = new Stopwatch(); //計時器
            sw.Start();

            //調用插件的Update方法進行批量更新(不須要咱們手動的db.SaveChanges()了)
            //db.location.Update(r => new location { version = 123 });//批量將location表裏version字段數據更新爲123

            db.tolocation.Where(r => r.locId < 100).Update(c => new tolocation { version = 236 }); //也能夠帶條件批量修改


            sw.Stop();
            string aa = sw.Elapsed.ToString();//批量更新了17597條數據。耗時1.69秒
            return View();
        }
    }
}
View Code

批量刪除(17597條數據,批量刪除耗時1.76秒)

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;

namespace MvcApplication1.Controllers
{
    using MvcApplication1.Models;
    using EntityFramework.Extensions; //使用EF的EntityFramework.Extended插件須要引入此名稱空間
    using System.Diagnostics;

    public class HomeController : Controller
    {
        public ActionResult Index()
        {
            salesEntities db = new salesEntities();

            Stopwatch sw = new Stopwatch(); //計時器
            sw.Start();

            //調用插件的Delete方法進行批量刪除(不須要咱們手動的db.SaveChanges()了)
            //db.location.Delete(r => r.locId < 100000);

            db.location.Where(r => r.locId < 10000).Delete(); //固然我也能夠這樣寫

            
            sw.Stop();
            string aa = sw.Elapsed.ToString();//批量刪除了17597條數據。耗時1.76秒
            return View();
        }
    }
}
View Code

原文地址: https://blog.csdn.net/Fanbin168/article/details/51485969數據庫

相關文章
相關標籤/搜索