public static void ExecuteCommand(string file, OleDbCommand command, int colCount, List <object> dataList) { System.Data.OleDb.OleDbConnection MyConnection; //System.Data.OleDb.OleDbCommand myCommand = new System.Data.OleDb.OleDbCommand(); string connectionString = ExcelDbHelp.CreateExcelConnectionString(file); MyConnection = new OleDbConnection(connectionString); MyConnection.Open(); DbTransaction transaction = MyConnection.BeginTransaction(); command.Connection = MyConnection; command.Transaction = (OleDbTransaction)transaction; int count = 0; while (count < dataList.Count) { for (int i = 0; i < colCount; i++) { command.Parameters[i].Value = dataList[count]; count++; } command.ExecuteNonQuery(); } transaction.Commit(); MyConnection.Close(); }
public static void ExecuteCommand(string file, string commandText, List <string> headers, List <object> dataList) { System.Data.OleDb.OleDbConnection MyConnection; //System.Data.OleDb.OleDbCommand myCommand = new System.Data.OleDb.OleDbCommand(); string connectionString = ExcelDbHelp.CreateExcelConnectionString(file); MyConnection = new OleDbConnection(connectionString); MyConnection.Open(); DbTransaction transaction = MyConnection.BeginTransaction(); OleDbCommand command = new OleDbCommand(); command.Connection = MyConnection; command.CommandText = commandText; command.Transaction = (OleDbTransaction)transaction; foreach (string header in headers) { string s = "" + ""; command.Parameters.Add(new OleDbParameter() { ParameterName = "@" + header }); } int count = 0; while (count < dataList.Count) { for (int i = 0; i < headers.Count; i++) { command.Parameters[i].Value = dataList[count]; count++; } command.ExecuteNonQuery(); } transaction.Commit(); MyConnection.Close(); }