1 /// <summary> 2 /// 批量添加 3 /// </summary> 4 /// <param name="dt"></param> 5 public static bool AddExchangeBulkCopy(DataTable dt, string table_name) 6 { 7 bool ret = false; 8 9 using (SqlConnection Connection = new SqlConnection(CONN_STRING_NON_DTC)) 10 { 11 12 Connection.Open(); 13 14 SqlTransaction sqlbulkTransaction = Connection.BeginTransaction(); 15 16 using (SqlBulkCopy sqlBulkCopy = new SqlBulkCopy(Connection, SqlBulkCopyOptions.CheckConstraints, sqlbulkTransaction)) 17 { 18 19 //源列字段:A,目标数据源字段:B 20 //@o_id,ticket,@order_id,@product_id,@amount 21 22 sqlBulkCopy.ColumnMappings.Add(0, "return_orderid"); 23 24 sqlBulkCopy.ColumnMappings.Add(1, "phone"); 25 26 sqlBulkCopy.ColumnMappings.Add(2, "real_amount"); 27 28 sqlBulkCopy.ColumnMappings.Add(3, "finish_date"); 29 30 sqlBulkCopy.ColumnMappings.Add(4, "supplier"); 31 32 sqlBulkCopy.ColumnMappings.Add(5, "is_sign"); 33 34 sqlBulkCopy.ColumnMappings.Add(6, "msg_error"); 35 36 37 sqlBulkCopy.BatchSize = dt.Rows.Count; 38 39 sqlBulkCopy.BulkCopyTimeout = 60; 40 41 sqlBulkCopy.DestinationTableName = table_name; 42 43 if (dt != null && dt.Rows.Count != 0) 44 { 45 try 46 { 47 sqlBulkCopy.WriteToServer(dt); 48 49 sqlbulkTransaction.Commit(); 50 ret = true; 51 } 52 catch (Exception ex) 53 { 54 try 55 { 56 sqlbulkTransaction.Rollback(); 57 ret = false; 58 } 59 catch 60 { 61 ret = false; 62 } 63 throw ex; 64 } 65 finally 66 { 67 } 68 } 69 } 70 } 71 return ret; 72 }