/// <summary> /// 批量添加 /// </summary> /// <param name="dt"></param> /// <returns></returns> public bool BatchAdd(DataTable dt) { bool flag = false; try { using (SqlConnection cn = new SqlConnection(ConfigHelper.MssqlDBConnectionString)) { using (TransactionScope scope = new TransactionScope()) { cn.Open(); using (SqlBulkCopy sbc = new SqlBulkCopy(cn)) { //服务器上目标表的名称 sbc.DestinationTableName = "ZH_Building_RoomType"; sbc.BatchSize = 1000; sbc.BulkCopyTimeout = 60; for (int i = 0; i < dt.Columns.Count; i++) { //列映射定义数据源中的列和目标表中的列之间的关系 sbc.ColumnMappings.Add(dt.Columns[i].ColumnName, dt.Columns[i].ColumnName); } sbc.WriteToServer(dt); flag = true; scope.Complete();//有效的事务 } } } } catch (Exception ex) { //throw new Exception("[Provider批量添加问题]:" + ex.Message); return false; } return flag; }