#region 拼接sql语句并使用Transaction public static bool ExecuteTransactionInsert(DataTable dt, int batchSize) { int count = dt.Rows.Count; StringBuilder sql = new StringBuilder(220); bool flag = false; SqlConnection cn = null; SqlCommand cmd = null; try { cn = new SqlConnection(connectionString); cmd = new SqlCommand(); cmd.Connection = cn; cn.Open(); for (int i = 0; i < count; i += batchSize) { sql.Append("begin try;begin tran;"); for (int j = i; j < i + batchSize && j < count; j++) { sql.AppendFormat("Insert into TestTable(Id, Name) Values({0}, '{1}');", dt.Rows[j]["Id"], dt.Rows[j]["Name"]); }