Created a container object to house messages, operation status and the rows added for the methods that add new items and update existing items in the database.

This commit is contained in:
2016-11-10 00:02:15 -06:00
parent 8e140a7131
commit 5d387caecb
12 changed files with 183 additions and 29 deletions
+35 -14
View File
@@ -1,8 +1,6 @@
using System.Collections.Generic;
using System.Data;
using System.Data.OleDb;
using System.Transactions;
using System.Windows.Forms;
namespace AdvertsingProfitControl
{
@@ -14,8 +12,16 @@ namespace AdvertsingProfitControl
{
_oleDbConnection.ConnectionString = connectionString;
}
#region New Code
public OleDbCommand GetOleDbCommand(string connectionString)
{
var oleDbCommand = new OleDbCommand();
return oleDbCommand;
}
/// <summary>
/// Inserts new records into the specified sales table.
/// Supports rolling back the database to prevent corruption.
@@ -23,10 +29,10 @@ namespace AdvertsingProfitControl
/// <param name="salesTable">The data table that contains the data to be inserted.</param>
/// <param name="connectionString"></param>
/// <returns></returns>
public Dictionary<int, int> InsertIntoSalesTable(DataTable salesTable, string connectionString)
public DbWriterStatus InsertIntoSalesTable(DataTable salesTable, string connectionString)
{
var rowIndex = 0;
var rows = new Dictionary<int, int>();
var writerStatus = new DbWriterStatus();
var oleDbConnection = new OleDbConnection(connectionString);
//IDs from the database can not be zero (0), so this will be the default value (no group).
var oleDbCommand = new OleDbCommand
@@ -69,21 +75,29 @@ namespace AdvertsingProfitControl
if (int.Parse(row[8].ToString()) != 0)
{
//Account for the ad special row.
rows.Add(int.Parse(row[9].ToString()) + 1, rowId);
//Index 8 of the row indicates that this row is an ad special member row
//so we must add one to the row position to offset the fact that a row
//is "missing" from this DataTable.
//Row[9] is its row position, which is not zero index based.
writerStatus.AddRowId(int.Parse(row[9].ToString()) + 1, rowId);
}
else
{
rows.Add(int.Parse(row[9].ToString()), rowId);
}
//The ad special row doesn't exist so no need to offset it.
writerStatus.AddRowId(int.Parse(row[9].ToString()), rowId);
}
}
writerStatus.SetStatus(WritingOperationStatus.InsertionSuccessful);
}
catch (OleDbException ex)
{
writerStatus.SetStatus(WritingOperationStatus.Failed);
_logConsole.WriteToLog(FrmLogConsole.Level.Error, "Failed to write to database: " + ex.Message);
if (rowIndex <= 0)
{
_logConsole.WriteToLog(FrmLogConsole.Level.Error,
"Failed to begin parsing data rows, var dump of erroneous row unavailable.");
writerStatus.SetErrorMessage("Failed to begin parsing rows.");
}
else
{
@@ -98,7 +112,8 @@ namespace AdvertsingProfitControl
"Cost: \"" + salesTable.Rows[rowIndex][3] + "\" Profit Return: \"" + salesTable.Rows[rowIndex][4] + "\"");
_logConsole.WriteToLog(FrmLogConsole.Level.Error,
"Total Profit Return: \"" +
salesTable.Rows[rowIndex][5] + "\" Ad Special Group: \"" + salesTable.Rows[rowIndex][8] + "\"");
salesTable.Rows[rowIndex][5] + "\" Ad Special Group: \"" + salesTable.Rows[rowIndex][8] + "\"");
writerStatus.SetErrorMessage("Failed to write to the database on row " + (rowIndex + 1) + ".");
}
oleDbTransaction?.Rollback();
_logConsole.WriteToLog(FrmLogConsole.Level.Critical, "Rollback completed successfully, " + salesTable.TableName+ " table failed on insertion.");
@@ -107,12 +122,12 @@ namespace AdvertsingProfitControl
{
_oleDbConnection.Close();
}
return rows;
return writerStatus;
}
public bool UpdateSalesTable(DataTable salesTable, string connectionString)
public DbWriterStatus UpdateSalesTable(DataTable salesTable, string connectionString)
{
var wasSuccessful = false;
var updateStatus = new DbWriterStatus();
var rowIndex = 0;
var oleDbConnection = new OleDbConnection(connectionString);
//IDs from the database can not be zero (0), so this will be the default value (no group).
@@ -129,7 +144,7 @@ namespace AdvertsingProfitControl
for (var i = 0; i < salesTable.Rows.Count; i++)
{
oleDbCommand.CommandText =
"UPDATE " + salesTable.TableName + " SET Sold = ?, SalePrice = ?, TotalSales = ?, Cost = ?, ProfitReturn = ?, TotalProfitReturn = ?, RowAttribute = ?, FK_AdSpecialGroupName = ?, RowPosition = ? WHERE ID = ?";
"UPDATE " + salesTable.TableName + " SET Sold = ?, SalePrice = ?, TotalSales = ?, Cost = ?, ProfitReturn = ?, TotalProfitReturn = ?, FK_AdItemID = ?, RowAttribute = ?, FK_AdSpecialGroupName = ?, RowPosition = ? WHERE ID = ?";
//Update Sales Table Layout (Based on the Database's Physical Layout)
//0:ID 1:Sold 2:SalePrice 3:TotalSales 4:cost 5:ProfitReturn 6:TotalProfitReturn
//7:FK_AdItemID 8:RowAttribute 9:FK_AdSpecialGroupName (ID) 10:RowPosition (not index based)
@@ -140,24 +155,28 @@ namespace AdvertsingProfitControl
oleDbCommand.Parameters.AddWithValue("Cost", salesTable.Rows[i][4]);
oleDbCommand.Parameters.AddWithValue("ProfitReturn", salesTable.Rows[i][5]);
oleDbCommand.Parameters.AddWithValue("TotalProfitReturn", salesTable.Rows[i][6]);
oleDbCommand.Parameters.AddWithValue("FK_AdItemID", salesTable.Rows[i][7]);
oleDbCommand.Parameters.AddWithValue("RowAttribute", salesTable.Rows[i][8]);
oleDbCommand.Parameters.AddWithValue("adSpecialID", salesTable.Rows[i][9]);
oleDbCommand.Parameters.AddWithValue("RowPosition", salesTable.Rows[i][10]);
oleDbCommand.Parameters.AddWithValue("ID", salesTable.Rows[i][0]);
oleDbCommand.ExecuteNonQuery();
oleDbCommand.Parameters.Clear();
//Row index is only used to keep track of what row failed to update.
rowIndex++;
}
oleDbTransaction.Commit();
wasSuccessful = true;
updateStatus.SetStatus(WritingOperationStatus.UpdateSuccessful);
}
catch (OleDbException ex)
{
updateStatus.SetStatus(WritingOperationStatus.Failed);
_logConsole.WriteToLog(FrmLogConsole.Level.Error, "Failed to update the database for " + salesTable.TableName + ": " + ex.Message);
if (rowIndex <= 0)
{
_logConsole.WriteToLog(FrmLogConsole.Level.Error,
"Failed to begin parsing data rows, var dump of erroneous row unavailable.");
updateStatus.SetErrorMessage("Failed to begin parsing rows.");
}
else
{
@@ -173,6 +192,7 @@ namespace AdvertsingProfitControl
_logConsole.WriteToLog(FrmLogConsole.Level.Error,
"Total Profit Return: \"" +
salesTable.Rows[rowIndex][6] + "\" Ad Special Group: \"" + salesTable.Rows[rowIndex][9] + "\"");
updateStatus.SetErrorMessage("Failed to write to the database on row " + (rowIndex + 1) + ".");
}
oleDbTransaction?.Rollback();
_logConsole.WriteToLog(FrmLogConsole.Level.Critical, "Rollback completed successfully, " + salesTable.TableName + " table failed on update.");
@@ -182,7 +202,7 @@ namespace AdvertsingProfitControl
_oleDbConnection.Close();
}
return wasSuccessful;
return updateStatus;
}
/// <summary>
@@ -315,6 +335,7 @@ namespace AdvertsingProfitControl
_oleDbConnection.Close();
return id;
}
#endregion
//10 to 1 ratio of tries to code