public async Task CreateMany(IEnumerable <T> items, string tableName) { DbConnection db = new SqlConnection(_connectionString); db.Open(); var bulkOperation = new BulkOperation(db); bulkOperation.DestinationTableName = tableName; await bulkOperation.BulkInsertAsync(items); db.Close(); }
public async Task <List <Player> > CreateMany(List <Player> players) { DbConnection db = new SqlConnection(_connectionString); db.Open(); var bulkOperation = new BulkOperation(db); bulkOperation.DestinationTableName = "Players"; bulkOperation.ColumnMappings.Add(new ColumnMapping("Id", true)); bulkOperation.ColumnMappings.Add(new ColumnMapping("Name", "Name")); bulkOperation.ColumnMappings.Add(new ColumnMapping("Type", "Type")); bulkOperation.ColumnMappings.Add(new ColumnMapping("CreationDate", "CreationDate")); bulkOperation.ColumnMappings.Add("Id", ColumnMappingDirectionType.Output); await bulkOperation.BulkInsertAsync(players); db.Close(); return(players); }
public async Task CreateConfigIfNotExistsAsync2(int sqlConfigId, SqlConfigDetailDto[] listdata) { try { var mapperData = ObjectMapper.Map <SqlConfigDetail[]>(listdata); var oldConfig = GetColumnConfigBySqlId(sqlConfigId).ToArray(); var oldConfigMap = oldConfig.Select(x => x.Code); var newConfig = listdata.Select(x => x.Code); using (var conn = new SqlConnection(this.connectionString)) { if (conn.State == ConnectionState.Closed) { await conn.OpenAsync(); } if (oldConfig.Length > 0 && oldConfigMap != newConfig) { using (var bulk = new BulkOperation <SqlConfigDetail>(conn)) { bulk.DestinationTableName = "SqlConfigDetail"; bulk.InsertIfNotExists = true; bulk.ColumnPrimaryKeyExpression = c => c.Id; bulk.BatchSize = 10000; bulk.BatchTimeout = 10000; bulk.ColumnPrimaryKeyExpression = c => new { c.Code }; //bulk.AutoMapValueFactory = //bulk.ColumnInputExpression = c => new //{ // c.LastModificationTime, // c.LastModifierUserId, // c.IsDisplay, // c.IsSum, // c.GroupLevel, // c.Width, // c.Name, // c.GroupSort, // c.Type, // c.Format, // c.ColNum //}; await bulk.BulkInsertAsync(mapperData); } } else { using (var bulk = new BulkOperation <SqlConfigDetail>(conn)) { bulk.DestinationTableName = "SqlConfigDetail"; bulk.InsertIfNotExists = true; bulk.ColumnPrimaryKeyExpression = c => c.Id; bulk.BatchSize = 10000; bulk.BatchTimeout = 10000; //bulk.AutoMapValueFactory = bulk.ColumnInputExpression = c => new { c.LastModificationTime, c.LastModifierUserId, c.IsDisplay, c.IsSum, c.GroupLevel, c.Width, c.Name, c.GroupSort, c.Type, c.Format, c.ColNum }; await bulk.BulkUpdateAsync(mapperData); } } } } catch (Exception ex) { System.Diagnostics.Debug.WriteLine("Error: ", ex); } }