private void Hoja1_Startup(object sender, System.EventArgs e) { IMSClasses.ConfigurationHelpper oCfg = Globals.ThisWorkbook.oCfg; IMSClasses.Jobs.Job oJob = Globals.ThisWorkbook.oJob; IMSClasses.DBHelper.db oDB = Globals.ThisWorkbook.oDb; try { //oTable = Helppers.importData(oCfg.ConnectionString, "SELECT * FROM " + oJob.SQLParameters.TableName.Replace(@"%identity%", "1"), this); oTable = Helppers.importData(oCfg.ConnectionString, "getTemplateData_GAFarmacia", this); } catch { this.oTable = null; Globals.ThisWorkbook.StatusMessage = "Error getting data in sheet 1"; Globals.ThisWorkbook.StatusCorrect = false; oJob.ReportStatus.Message = Globals.ThisWorkbook.StatusMessage; oJob.ReportStatus.Status = "ERRO"; oDB.updateJob(oJob.Serialize(), oJob.JOBID); } if (!Globals.ThisWorkbook.StatusCorrect || this.oTable == null) { this.oTable = null; Globals.ThisWorkbook.StatusMessage = "Error getting data in sheet 1"; Globals.ThisWorkbook.StatusCorrect = false; oJob.ReportStatus.Message = Globals.ThisWorkbook.StatusMessage; oJob.ReportStatus.Status = "ERRO"; oDB.updateJob(oJob.Serialize(), oJob.JOBID); } }
public bool importData(String sConnectionString, String sSqlQuery) { Boolean bDone = false; try { Excel.Range oRange = this.Range["A1"]; this.oTable = this.QueryTables.Add(sConnectionString, oRange); this.oTable.CommandType = Excel.XlCmdType.xlCmdSql; this.oTable.CommandText = sSqlQuery; this.oTable.Refresh(); bDone = true; for (int i = 1; i <= Globals.ThisWorkbook.Connections.Count; i++) Globals.ThisWorkbook.Connections[i].Delete(); } catch (Exception e) { //MessageBox.Show(e.Message); bDone = false; } return bDone; }