/// <summary> /// /// </summary> /// <param name="tabkeName"></param> /// <param name="columns"></param> /// <param name="dataReader"></param> /// <param name="option"></param> /// <returns></returns> public override long ExecuteBulkQuery(string tabkeName, IEnumerable <string> columns, IDataReader dataReader, DbBulkOption option) { long result = 0; OpenConnection(conn => { MySqlBulkLoader bulkCopy = new MySqlBulkLoader(conn); SetBulkOption(bulkCopy, tabkeName, option); foreach (var column in columns) { bulkCopy.Columns.Add(option.GetColumnMapping(column)); } BuildTempFile(columns, dataReader, option); FileInfo fi = new FileInfo(option.TempPath); FileAttributes oldAttr = fi.Attributes; try { fi.Attributes = fi.Attributes | FileAttributes.ReadOnly; result = bulkCopy.Load(); } finally { fi.Attributes = oldAttr; try { fi.Delete(); } catch { } } }); return(result); }
/// <summary> /// /// </summary> /// <param name="tabkeName"></param> /// <param name="dataTable"></param> /// <param name="option"></param> /// <returns></returns> public override long ExecuteBulkQuery(string tabkeName, DataTable dataTable, DbBulkOption option) { long result = 0; OpenConnection(conn => { var tran = conn.BeginTransaction(); using (SqlBulkCopy bulkCopy = new SqlBulkCopy(conn, option.CopyOptions, tran)) { SetBulkOption(bulkCopy, tabkeName, option); foreach (DataColumn col in dataTable.Columns) { bulkCopy.ColumnMappings.Add(col.ColumnName, option.GetColumnMapping(col.ColumnName)); } try { bulkCopy.WriteToServer(dataTable); tran.Commit(); result = bulkCopy.NotifyAfter; } catch (Exception ex) { tran.Rollback(); throw ex; } } }); return(result); }
/// <summary> /// /// </summary> /// <param name="tabkeName"></param> /// <param name="dataTable"></param> /// <param name="option"></param> /// <returns></returns> public override long ExecuteBulkQuery(string tabkeName, DataTable dataTable, DbBulkOption option) { long result = 0; OpenConnection(conn => { MySqlBulkLoader bulkCopy = new MySqlBulkLoader(conn); SetBulkOption(bulkCopy, tabkeName, option); var columns = new List <string>(); foreach (DataColumn col in dataTable.Columns) { string column = option.GetColumnMapping(col.ColumnName); columns.Add(column); bulkCopy.Columns.Add(column); } BuildTempFile(columns, dataTable, option); FileInfo fi = new FileInfo(option.TempPath); FileAttributes oldAttr = fi.Attributes; try { fi.Attributes = fi.Attributes | FileAttributes.ReadOnly; result = bulkCopy.Load(); } finally { fi.Attributes = oldAttr; try { fi.Delete(); } catch { } } }); return(result); }