// assumes DB is there, but might need to build it before we get here public bool Execute(string sSQL) { bool needDb = false; try { sql_con.Open(); sql_cmd = sql_con.CreateCommand(); sql_cmd.CommandText = sSQL; try { int i = sql_cmd.ExecuteNonQuery(); } catch (System.Data.Common.DbException dbx) { needDb = DBMain.DBExceptionHandler(dbx, sSQL); } } catch (Exception caught) { try { DBMain.AltLog(LogLevels.Warning, 70101, "Execute '" + caught.Message + "' " + sSQL); sql_con.Close(); return(false); } catch { } } if (sql_con != null) { sql_con.Close(); } if (needDb) { DBMain.Creation(sql_cmd); // create an new empty DB, pass the failed query string, retry it once after initial DB creation } return(true); }
public long ExecuteTransactionID(ArrayList sqlList) { //Execute Sequence of Sql Statements //Check Sql Type to determine if we have return value to replace in next statement DbTransaction Trans = null; string sRtn = ""; Int32 result = -1; // what to return here? try { sql_con.Open(); Trans = sql_con.BeginTransaction(); sql_cmd = sql_con.CreateCommand(); sql_cmd.Transaction = Trans; bool needDb = false; bool scalar = false; for (int i = 0; i < sqlList.Count; i++) { if (!sqlList[i].ToString().Equals("")) { sql_cmd.CommandText = sqlList[i].ToString(); //DBMain.AltLog(LogLevels.Info, 70115, "sql => " + sql_cmd.CommandText); try { if (sqlList[i].ToString().ToUpper().StartsWith("SELECT")) { //Expect back a string scalar = true; sRtn = sql_cmd.ExecuteScalar().ToString(); if (sRtn.Equals("")) { Trans.Rollback(); sql_con.Close(); return(-1); } } else { scalar = false; if (sql_cmd.CommandText.Contains("<Rtn>")) { //substitute in sRtn Value sql_cmd.CommandText = sql_cmd.CommandText.Replace("<Rtn>", sRtn); } //Expect back # rows modified if (sql_cmd.ExecuteNonQuery() < 0) { Trans.Rollback(); sql_con.Close(); return(-1); } } } catch (System.Data.Common.DbException dbx) { Trans.Rollback(); needDb = DBMain.DBExceptionHandler(dbx, sql_cmd.CommandText); sql_con.Close(); return(-1); } if (needDb) { DBMain.Creation(sql_cmd, scalar); // create a new empty DB, pass the failed query string, retry it once after initial DB creation } } } Trans.Commit(); sql_con.Close(); Int32.TryParse(sRtn, out result); return(result); } catch (Exception caught) { if (sql_cmd != null) { DBMain.AltLog(LogLevels.Warning, 70107, "ExecuteTransactionID '" + caught.Message + "' " + sql_cmd); } if (Trans != null) { Trans.Rollback(); } if (sql_con != null) { sql_con.Close(); } return(-1); } }