用途說明:sql
公司要求作一個數據導入程序,要求將Excel數據,大批量的導入到數據庫中,儘可能少的訪問數據庫,高性能的對數據庫進行存儲。因而在網上進行查找,發現了一個比較好的解決方案,就是採用SqlBulkCopy來處理存儲數據。SqlBulkCopy存儲大批量的數據很是的高效,就像這個方法的名字同樣,能夠將內存中的數據表直接的一次性的存儲到數據庫中,而不須要一次一次的向數據庫Insert數據。初次實驗,百萬級別的數據表,也只需幾秒時間內就能夠徹底的存入數據庫中,其速度,比傳統的Insert方法不止快百倍千倍數據庫
#region 批量添加實名認證信息(萬級) /// <summary> /// 批量添加實名認證信息(萬級) /// </summary> /// <returns></returns> [Route("Test/BatchAddNciicUserInfo")] [HttpGet] public async Task<string> BatchAddNciicUserInfo() { DateTime regtime = DateTime.Parse("2019-06-24"); int i = 0; using (var db = new GPAppEntities()) { using (SqlConnection conn = db.Database.Connection as SqlConnection) { if (conn.State != ConnectionState.Open) { conn.Open(); } using (SqlTransaction tran = conn.BeginTransaction()) { db.Database.UseTransaction(tran); try { var userlist = db.AppUser.Where(a => a.RegisterTime > regtime).OrderBy(a=>a.RegisterTime).Select(s => new { s.ID,s.MobilePhone}).Take(51154).ToList(); StreamReader sr = new StreamReader(@"D:\Deploy\GPApp.Api\Content\Test\APP用戶數據.txt", Encoding.Default); String line; List<IDCardNo> list = new List<IDCardNo>(); while ((line = sr.ReadLine()) != null) { var model = new IDCardNo(); model.Number = line; list.Add(model); } using (SqlBulkCopy sqlBC = new SqlBulkCopy(conn, SqlBulkCopyOptions.Default, tran)) { DataTable dtSource = new DataTable(); dtSource.Columns.Add("ID", typeof(Guid)); dtSource.Columns.Add("UserId", typeof(Guid)); dtSource.Columns.Add("RealName", typeof(string)); dtSource.Columns.Add("Gender", typeof(string)); dtSource.Columns.Add("IDCardNo", typeof(string)); dtSource.Columns.Add("IDCardNoEncrypt", typeof(string)); dtSource.Columns.Add("IDCardAgency", typeof(string)); dtSource.Columns.Add("IDCardStartDate", typeof(DateTime)); dtSource.Columns.Add("IDCardEndDate", typeof(DateTime)); dtSource.Columns.Add("Portrait", typeof(string)); dtSource.Columns.Add("FIDCard", typeof(string)); dtSource.Columns.Add("BIDCard", typeof(string)); dtSource.Columns.Add("Status", typeof(string)); dtSource.Columns.Add("AppSystem", typeof(string)); dtSource.Columns.Add("AppDevice", typeof(string)); dtSource.Columns.Add("AppIP", typeof(string)); dtSource.Columns.Add("CreateDate", typeof(DateTime)); dtSource.Columns.Add("ModifyDate", typeof(DateTime)); dtSource.Columns.Add("Birthday", typeof(DateTime)); foreach (var item in userlist) { DataRow newdr = dtSource.NewRow(); newdr["ID"] = Guid.NewGuid(); newdr["UserId"] = item.ID; newdr["RealName"] = "葉長種"; newdr["Gender"] = "男"; newdr["IDCardNo"] = list[i].Number; newdr["IDCardNoEncrypt"] = SHA256Help.sha256(list[i].Number).ToLower(); newdr["IDCardAgency"] = "上海市公安局黃浦分局"; newdr["IDCardStartDate"] = DateTime.Parse("2010-01-01 00:00:00"); newdr["IDCardEndDate"] = DateTime.Parse("2020-01-01 00:00:00"); newdr["Portrait"] = ""; newdr["FIDCard"] = ""; newdr["BIDCard"] = ""; newdr["Status"] = "一致"; newdr["AppSystem"] = ""; newdr["AppDevice"] =""; newdr["AppIP"] = ""; newdr["CreateDate"] = DateTime.Now; newdr["ModifyDate"] = DateTime.Now; newdr["Birthday"] = DateTime.Parse("1989-01-28 00:00:00"); dtSource.Rows.Add(newdr); i = i + 1; } sqlBC.BatchSize = 10000; sqlBC.BulkCopyTimeout = 60; sqlBC.DestinationTableName = string.Format("dbo.NciicUserInfo"); sqlBC.ColumnMappings.Add("ID", "ID"); sqlBC.ColumnMappings.Add("UserId", "UserId"); sqlBC.ColumnMappings.Add("RealName", "RealName"); sqlBC.ColumnMappings.Add("Gender", "Gender"); sqlBC.ColumnMappings.Add("IDCardNo", "IDCardNo"); sqlBC.ColumnMappings.Add("IDCardNoEncrypt", "IDCardNoEncrypt"); sqlBC.ColumnMappings.Add("IDCardAgency", "IDCardAgency"); sqlBC.ColumnMappings.Add("IDCardStartDate", "IDCardStartDate"); sqlBC.ColumnMappings.Add("IDCardEndDate", "IDCardEndDate"); sqlBC.ColumnMappings.Add("Portrait", "Portrait"); sqlBC.ColumnMappings.Add("FIDCard", "FIDCard"); sqlBC.ColumnMappings.Add("BIDCard", "BIDCard"); sqlBC.ColumnMappings.Add("Status", "Status"); sqlBC.ColumnMappings.Add("AppSystem", "AppSystem"); sqlBC.ColumnMappings.Add("AppDevice", "AppDevice"); sqlBC.ColumnMappings.Add("AppIP", "AppIP"); sqlBC.ColumnMappings.Add("CreateDate", "CreateDate"); sqlBC.ColumnMappings.Add("ModifyDate", "ModifyDate"); sqlBC.ColumnMappings.Add("Birthday", "Birthday"); sqlBC.WriteToServer(dtSource); } tran.Commit(); } catch (Exception ex) { tran.Rollback(); throw ex; } } } } return "添加成功"; } public class IDCardNo { public string Number { get; set; } } #endregion