public void BatchInsert(List <T> models, params string[] fields) { var sql = new StringBuilder(); var paramters = new List <List <object> >(); foreach (T t in models) { List <object> obj = new List <object>(); if (fields == null) { fields = type.GetProperties().Select(pi => pi.Name).ToArray(); } for (var i = 0; i < fields.Length; i++) { obj.Add(type.GetProperty(fields[i]).GetValue(t, null)); } paramters.Add(obj); } sql.Append("insert into ").Append(type.Name).Append("(").Append(fields[0]); var pam = new StringBuilder("@0"); for (var j = 1; j < fields.Length; j++) { sql.Append("," + fields[j] + ""); pam.Append(",@" + j); } sql.Append(") values (").Append(pam).Append(")"); DbHelperSQL.BatchExcuteNonquery(sql.ToString(), paramters.ToArray()); }
public void BatchDelete(List <T> models, params string[] fieldName) { StringBuilder sql = new StringBuilder(); sql.Append("delete from ").Append(type.Name).Append(" where 1=1 "); List <List <object> > paramters = new List <List <object> >(); foreach (T t in models) { var obj = new List <object>(); foreach (string name in fieldName) { obj.Add(type.GetProperty(name).GetValue(t, null)); } paramters.Add(obj); } var i = 0; foreach (string name in fieldName) { sql.Append(" and " + name + "=@" + i); i++; } DbHelperSQL.BatchExcuteNonquery(sql.ToString(), paramters.ToArray()); }