V4.0
This commit is contained in:
@@ -1,17 +1,12 @@
|
||||
using System;
|
||||
using ClosedXML.Excel;
|
||||
using System.Data;
|
||||
using System.Data.OleDb;
|
||||
using ClosedXML.Excel;
|
||||
|
||||
namespace AiQ_GUI.Microsoft
|
||||
namespace AiQ_GUI
|
||||
{
|
||||
internal class StatsExcel
|
||||
{
|
||||
private const string connString =
|
||||
@"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=G:\Shared drives\MAV Production GUI's\AiQ\GUI's\AiQ_Final_Test.accdb;Persist Security Info=False;OLE DB Services=-1;";
|
||||
|
||||
private readonly string exportPath =
|
||||
@"C:\Users\BradleyBorn\OneDrive - MAV Systems Ltd\Desktop\AiQ_Test_Stats.xlsx";
|
||||
private const string exportPath = @"G:\Shared drives\MAV Production GUI's\AiQ\GUI's\AiQ_Test_Stats.xlsx";
|
||||
|
||||
public void ExportDatabaseToExcel()
|
||||
{
|
||||
@@ -19,11 +14,10 @@ namespace AiQ_GUI.Microsoft
|
||||
{
|
||||
MainForm.Instance.AddToActionsList("=== ExportDatabaseToExcel START ===");
|
||||
|
||||
using (OleDbConnection conn = new(connString))
|
||||
using (OleDbConnection conn = new(Access.connString))
|
||||
{
|
||||
conn.Open();
|
||||
MainForm.Instance.AddToActionsList("Connected to Access database.");
|
||||
|
||||
DateTime now = DateTime.Now;
|
||||
|
||||
// ==================== MAIN STATS EXPORT ====================
|
||||
@@ -51,10 +45,7 @@ namespace AiQ_GUI.Microsoft
|
||||
[Visual Test Fail - Not All Rear Screws Fitted],
|
||||
[Visual Test Fail - Unit rattles]";
|
||||
|
||||
string query = $@"
|
||||
SELECT
|
||||
{selectColumns}
|
||||
FROM AiQ";
|
||||
string query = $@"SELECT {selectColumns} FROM AiQ";
|
||||
|
||||
OleDbDataAdapter adapter = new(query, conn);
|
||||
DataTable dataTable = new();
|
||||
@@ -72,7 +63,7 @@ namespace AiQ_GUI.Microsoft
|
||||
MainForm.Instance.AddToActionsList($"Adding sheet: {sheetName}");
|
||||
|
||||
XLWorkbook workbook;
|
||||
if (System.IO.File.Exists(exportPath))
|
||||
if (File.Exists(exportPath))
|
||||
{
|
||||
workbook = new XLWorkbook(exportPath);
|
||||
MainForm.Instance.AddToActionsList("Opened existing workbook.");
|
||||
@@ -89,7 +80,7 @@ namespace AiQ_GUI.Microsoft
|
||||
MainForm.Instance.AddToActionsList($"Deleted old sheet: {sheetName}");
|
||||
}
|
||||
|
||||
var ws = workbook.Worksheets.Add(sheetName);
|
||||
IXLWorksheet ws = workbook.Worksheets.Add(sheetName);
|
||||
ws.Cell(1, 1).InsertTable(dataTable, "AiQ_Stats", true);
|
||||
ws.Columns().AdjustToContents();
|
||||
|
||||
@@ -99,7 +90,7 @@ namespace AiQ_GUI.Microsoft
|
||||
// ==================== DIAGS STATS EXPORT (CURRENT MONTH ONLY) ====================
|
||||
MainForm.Instance.AddToActionsList("Exporting DiagsStats for current month...");
|
||||
|
||||
DateTime monthStart = new DateTime(now.Year, now.Month, 1);
|
||||
DateTime monthStart = new(now.Year, now.Month, 1);
|
||||
DateTime nextMonth = monthStart.AddMonths(1);
|
||||
|
||||
string diagsQuery = @"
|
||||
@@ -154,9 +145,10 @@ namespace AiQ_GUI.Microsoft
|
||||
{selectColumns}
|
||||
INTO [{backupTableName}]
|
||||
FROM AiQ";
|
||||
|
||||
try
|
||||
{
|
||||
using (var cmdBackup = new OleDbCommand(backupSql, conn))
|
||||
using (OleDbCommand cmdBackup = new(backupSql, conn))
|
||||
{
|
||||
cmdBackup.ExecuteNonQuery();
|
||||
}
|
||||
@@ -169,11 +161,10 @@ namespace AiQ_GUI.Microsoft
|
||||
return;
|
||||
}
|
||||
|
||||
using (var tx = conn.BeginTransaction())
|
||||
using OleDbTransaction tx = conn.BeginTransaction();
|
||||
try
|
||||
{
|
||||
try
|
||||
{
|
||||
string resetSql = @"
|
||||
string resetSql = @"
|
||||
UPDATE AiQ SET
|
||||
[Total Tests Run] = 0,
|
||||
[Pre Tests Passed] = 0,
|
||||
@@ -197,42 +188,41 @@ namespace AiQ_GUI.Microsoft
|
||||
[Visual Test Fail - Not All Rear Screws Fitted] = 0,
|
||||
[Visual Test Fail - Unit rattles] = 0;";
|
||||
|
||||
using (var cmdReset = new OleDbCommand(resetSql, conn, tx))
|
||||
{
|
||||
int affected = cmdReset.ExecuteNonQuery();
|
||||
MainForm.Instance.AddToActionsList($"Zeroed counters on AiQ rows: {affected} row(s).");
|
||||
}
|
||||
|
||||
int updatedRows;
|
||||
using (var cmdUpd = new OleDbCommand("UPDATE UniversalData SET LastStatsRun = ?", conn, tx))
|
||||
{
|
||||
cmdUpd.Parameters.Add(new OleDbParameter { OleDbType = OleDbType.Date, Value = now });
|
||||
updatedRows = cmdUpd.ExecuteNonQuery();
|
||||
}
|
||||
|
||||
if (updatedRows == 0)
|
||||
{
|
||||
using (var cmdIns = new OleDbCommand("INSERT INTO UniversalData (LastStatsRun) VALUES (?)", conn, tx))
|
||||
{
|
||||
cmdIns.Parameters.Add(new OleDbParameter { OleDbType = OleDbType.Date, Value = now });
|
||||
cmdIns.ExecuteNonQuery();
|
||||
MainForm.Instance.AddToActionsList("Inserted LastStatsRun row.");
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
MainForm.Instance.AddToActionsList("Updated LastStatsRun successfully.");
|
||||
}
|
||||
|
||||
tx.Commit();
|
||||
MainForm.Instance.AddToActionsList("Reset committed.");
|
||||
}
|
||||
catch (Exception resetEx)
|
||||
using (OleDbCommand cmdReset = new(resetSql, conn, tx))
|
||||
{
|
||||
tx.Rollback();
|
||||
MainForm.Instance.AddToActionsList($"ERROR during reset, rolled back. Details: {resetEx.Message}");
|
||||
int affected = cmdReset.ExecuteNonQuery();
|
||||
MainForm.Instance.AddToActionsList($"Zeroed counters on AiQ rows: {affected} row(s).");
|
||||
}
|
||||
|
||||
int updatedRows;
|
||||
using (OleDbCommand cmdUpd = new("UPDATE UniversalData SET LastStatsRun = ?", conn, tx))
|
||||
{
|
||||
cmdUpd.Parameters.Add(new OleDbParameter { OleDbType = OleDbType.Date, Value = now });
|
||||
updatedRows = cmdUpd.ExecuteNonQuery();
|
||||
}
|
||||
|
||||
if (updatedRows == 0)
|
||||
{
|
||||
using OleDbCommand cmdIns = new("INSERT INTO UniversalData (LastStatsRun) VALUES (?)", conn, tx);
|
||||
cmdIns.Parameters.Add(new OleDbParameter { OleDbType = OleDbType.Date, Value = now });
|
||||
cmdIns.ExecuteNonQuery();
|
||||
MainForm.Instance.AddToActionsList("Inserted LastStatsRun row.");
|
||||
}
|
||||
else
|
||||
{
|
||||
MainForm.Instance.AddToActionsList("Updated LastStatsRun successfully.");
|
||||
}
|
||||
|
||||
tx.Commit();
|
||||
MainForm.Instance.AddToActionsList("Reset committed.");
|
||||
}
|
||||
catch (Exception resetEx)
|
||||
{
|
||||
tx.Rollback();
|
||||
MainForm.Instance.AddToActionsList($"ERROR during reset, rolled back. Details: {resetEx.Message}");
|
||||
}
|
||||
|
||||
conn.Close();
|
||||
}
|
||||
|
||||
MainForm.Instance.AddToActionsList("=== ExportDatabaseToExcel END ===");
|
||||
|
||||
Reference in New Issue
Block a user