标签:
private void ExcuteListSQL(string tableName, List<List<SqlCondition>> conditionList) { string[] columns = conditionList[0].Select(t => t.ColumnName).ToList().ToArray(); string insertSql = DB.GetInsertSQL(tableName, columns); insertSql = insertSql.Replace("?", "@"); using (OleDbTransaction trans = conn.BeginTransaction()) { OleDbCommand cmd = new OleDbCommand(); cmd.Connection = conn; cmd.Transaction = trans; try { foreach (var one in conditionList) { cmd.CommandText = insertSql; one.ForEach(t => { cmd.Parameters.Add(new OleDbParameter("@" + t.ColumnName, t.Value)); }); cmd.ExecuteNonQuery(); cmd.Parameters.Clear(); } trans.Commit(); } catch (Exception ex) { trans.Rollback(); throw ex; } } } public class SqlCondition { public string ColumnName { get; set; } public object Value { get; set; } }
public void ExcuteTableSql(string tableName, DataTable dt) { List<string> columnList = new List<string>(); foreach (DataColumn one in dt.Columns) { columnList.Add(one.ColumnName); } OleDbDataAdapter adapter = new OleDbDataAdapter(); adapter.SelectCommand = new OleDbCommand("select * from " + tableName, conn); using (OleDbCommandBuilder builder = new OleDbCommandBuilder(adapter)) { adapter.InsertCommand = builder.GetInsertCommand(); foreach (string one in columnList) { adapter.InsertCommand.Parameters.Add(new OleDbParameter(one, dt.Columns[one].DataType)); } adapter.Update(dt); } }
代码下载地址:http://download.csdn.net/detail/lqqlike/9371349
标签:
原文地址:http://www.cnblogs.com/lqqlike/p/5065250.html