First version of the Inventory insertion and update methods, untested.

This commit is contained in:
2016-11-10 13:35:47 -06:00
parent 5d387caecb
commit d0c6ce5e42
3 changed files with 182 additions and 12 deletions
Binary file not shown.
+177 -5
View File
@@ -27,14 +27,13 @@ namespace AdvertsingProfitControl
/// Supports rolling back the database to prevent corruption.
/// </summary>
/// <param name="salesTable">The data table that contains the data to be inserted.</param>
/// <param name="connectionString"></param>
/// <returns></returns>
/// <param name="connectionString">A string pointing to the database.</param>
/// <returns>A DbWriterStatus object that contains a status, rows added and an error message if necessary.</returns>
public DbWriterStatus InsertIntoSalesTable(DataTable salesTable, string connectionString)
{
var rowIndex = 0;
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
{
Connection = oleDbConnection
@@ -124,13 +123,19 @@ namespace AdvertsingProfitControl
}
return writerStatus;
}
/// <summary>
/// Updates existing records in the database for either the Projections
/// table or the Actual Sales table.
/// Supports database rollback.
/// </summary>
/// <param name="salesTable">The data table that contains the data to be inserted.</param>
/// <param name="connectionString">A string pointing to the database.</param>
/// <returns>A DbWriterStatus object that contains a status, rows added and an error message if necessary.</returns>
public DbWriterStatus UpdateSalesTable(DataTable salesTable, string connectionString)
{
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).
var oleDbCommand = new OleDbCommand
{
Connection = oleDbConnection
@@ -204,6 +209,173 @@ namespace AdvertsingProfitControl
return updateStatus;
}
/// <summary>
/// Inserts new records into the Inventory table.
/// Supports database rollback.
/// </summary>
/// <param name="table">The data table that contains the data to be inserted.</param>
/// <param name="connectionString">A string pointing to the database.</param>
/// <returns>A DbWriterStatus object that contains a status, rows added and an error message if necessary.</returns>
public DbWriterStatus InsertIntoInventoryTable(DataTable table, string connectionString)
{
var status = new DbWriterStatus();
var rowIndex = 0;
var oleDbConnection = new OleDbConnection(connectionString);
var oleDbCommand = new OleDbCommand
{
Connection = oleDbConnection
};
OleDbTransaction oleDbTransaction = null;
try
{
oleDbTransaction = oleDbConnection.BeginTransaction();
_oleDbConnection.Open();
oleDbCommand.Transaction = oleDbTransaction;
for (var i = 0; i < table.Rows.Count; i++)
{
oleDbCommand.CommandText =
"INSERT INTO " + table.TableName + " (BeginningInventory, Received, TotalInventory, EndingInventory, FK_AdItemID, RowAttribute, FK_AdSpecialGroupName, RowPosition, FK_DateID) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?)";
//New inventory Table Layout (Based on the Database's Physical Layout)
//0:Beginning Inventory 1:Received 2:Total Inventory 3:Ending Inventory
//4:AdItemID 5:RowAttribute 6:GroupID 7:RowPosition 8:DateID
oleDbCommand.Parameters.AddWithValue("BegnningInventory", table.Rows[i][0]);
oleDbCommand.Parameters.AddWithValue("Received", table.Rows[i][1]);
oleDbCommand.Parameters.AddWithValue("TotalInventory", table.Rows[i][2]);
oleDbCommand.Parameters.AddWithValue("EndingInventory", table.Rows[i][3]);
oleDbCommand.Parameters.AddWithValue("AdItemID", table.Rows[i][4]);
oleDbCommand.Parameters.AddWithValue("RowAttribute", table.Rows[i][5]);
oleDbCommand.Parameters.AddWithValue("FK_AdSpecialGroup", table.Rows[i][6]);
oleDbCommand.Parameters.AddWithValue("RowPosition", table.Rows[i][7]);
oleDbCommand.Parameters.AddWithValue("DateID", table.Rows[i][8]);
oleDbCommand.ExecuteNonQuery();
oleDbCommand.Parameters.Clear();
//Row index is only used to keep track of what row failed to update.
rowIndex++;
}
oleDbTransaction.Commit();
foreach (DataRow row in table.Rows)
{
var rowId = RetrieveRowId(table.TableName, int.Parse(row[4].ToString()), int.Parse(row[8].ToString()));
if (int.Parse(row[6].ToString()) != 0)
{
//Account for the ad special row.
//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[7] is its row position, which is not zero index based.
status.AddRowId(int.Parse(row[7].ToString()) + 1, rowId);
}
else
{
//The ad special row doesn't exist so no need to offset it.
status.AddRowId(int.Parse(row[7].ToString()), rowId);
}
}
status.SetStatus(WritingOperationStatus.InsertionSuccessful);
}
catch (OleDbException e)
{
status.SetStatus(WritingOperationStatus.Failed);
_logConsole.WriteToLog(FrmLogConsole.Level.Error, "Failed to update the database for " + table.TableName + ": " + e.Message);
if (rowIndex <= 0)
{
_logConsole.WriteToLog(FrmLogConsole.Level.Error,
"Failed to begin parsing data rows, var dump of erroneous row unavailable.");
status.SetErrorMessage("Failed to begin parsing rows.");
}
else
{
_logConsole.WriteToLog(FrmLogConsole.Level.Error, "Failed on row " + (rowIndex + 1) + " due to the above error. Dumping contents of row " + (rowIndex + 1) + " from " + table.TableName + ".");
_logConsole.WriteToLog(FrmLogConsole.Level.Error, "Ad Item ID: \"" + table.Rows[rowIndex][4] + "\" Beginning Inventory: \"" + table.Rows[rowIndex][0]);
_logConsole.WriteToLog(FrmLogConsole.Level.Error, "Received: \"" + table.Rows[rowIndex][1] + "\" Total Inventory: \"" + table.Rows[rowIndex][2] + "\" Ending Inventory: \"" + table.Rows[rowIndex][3] + "\"");
_logConsole.WriteToLog(FrmLogConsole.Level.Error, "Row Attribute: \"" + table.Rows[rowIndex][5] + "\" Ad Special ID: \"" + table.Rows[rowIndex][7] + "\"");
_logConsole.WriteToLog(FrmLogConsole.Level.Error, "Date ID: \"" + table.Rows[rowIndex][8] + "\"");
status.SetErrorMessage("Failed to write to the database on row " + (rowIndex + 1) + ".");
}
oleDbTransaction?.Rollback();
_logConsole.WriteToLog(FrmLogConsole.Level.Critical, "Rollback completed successfully, " + table.TableName + " table failed on update.");
}
finally
{
_oleDbConnection.Close();
}
return status;
}
/// <summary>
/// Updates the Inventory table using the supplied DataTable
/// Supports rolling back the database.
/// </summary>
/// <param name="table">The table with the changes to be made.</param>
/// <param name="connectionString">A string pointing to the database.</param>
/// <returns>A DbWriterStatus object that contains a status, rows added and an error message if necessary.</returns>
public DbWriterStatus UpdateInventoryTable(DataTable table, string connectionString)
{
var status = new DbWriterStatus();
var rowIndex = 0;
var oleDbConnection = new OleDbConnection(connectionString);
var oleDbCommand = new OleDbCommand
{
Connection = oleDbConnection
};
OleDbTransaction oleDbTransaction = null;
try
{
oleDbTransaction = oleDbConnection.BeginTransaction();
_oleDbConnection.Open();
oleDbCommand.Transaction = oleDbTransaction;
for (var i = 0; i < table.Rows.Count; i++)
{
oleDbCommand.CommandText =
"UPDATE " + table.TableName + " SET BeginningInventory = ?, Received =? , TotalInventory = ?, EndingInventory = ?, FK_AdItemID = ?, RowAttribute = ?, FK_AdSpecialGroupName = ?, RowPosition = ? WHERE ID = ?";
//0:ID 1:BeginningInventory 2:Received 3:TotalInventory 4:EndingInventory
//5:AdItemID 6:RowAttribute 7:GroupID 8:RowPosition 9:DateID
oleDbCommand.Parameters.AddWithValue("BegnningInventory", table.Rows[i][1]);
oleDbCommand.Parameters.AddWithValue("Received", table.Rows[i][2]);
oleDbCommand.Parameters.AddWithValue("TotalInventory", table.Rows[i][3]);
oleDbCommand.Parameters.AddWithValue("EndingInventory", table.Rows[i][4]);
oleDbCommand.Parameters.AddWithValue("AdItemID", table.Rows[i][5]);
oleDbCommand.Parameters.AddWithValue("RowAttribute", table.Rows[i][6]);
oleDbCommand.Parameters.AddWithValue("FK_AdSpecialGroup", table.Rows[i][7]);
oleDbCommand.Parameters.AddWithValue("RowPosition", table.Rows[i][8]);
oleDbCommand.Parameters.AddWithValue("ID", table.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();
status.SetStatus(WritingOperationStatus.InsertionSuccessful);
}
catch (OleDbException e)
{
status.SetStatus(WritingOperationStatus.Failed);
_logConsole.WriteToLog(FrmLogConsole.Level.Error, "Failed to update the database for " + table.TableName + ": " + e.Message);
if (rowIndex <= 0)
{
_logConsole.WriteToLog(FrmLogConsole.Level.Error,
"Failed to begin parsing data rows, var dump of erroneous row unavailable.");
status.SetErrorMessage("Failed to begin parsing rows.");
}
else
{
_logConsole.WriteToLog(FrmLogConsole.Level.Error, "Failed on row " + (rowIndex + 1) + " due to the above error. Dumping contents of row " + (rowIndex + 1) + " from " + table.TableName + ".");
_logConsole.WriteToLog(FrmLogConsole.Level.Error, "Ad Item ID: \"" + table.Rows[rowIndex][4] + "\" Beginning Inventory: \"" + table.Rows[rowIndex][0]);
_logConsole.WriteToLog(FrmLogConsole.Level.Error, "Received: \"" + table.Rows[rowIndex][1] + "\" Total Inventory: \"" + table.Rows[rowIndex][2] + "\" Ending Inventory: \"" + table.Rows[rowIndex][3] + "\"");
_logConsole.WriteToLog(FrmLogConsole.Level.Error, "Row Attribute: \"" + table.Rows[rowIndex][5] + "\" Ad Special ID: \"" + table.Rows[rowIndex][7] + "\"");
_logConsole.WriteToLog(FrmLogConsole.Level.Error, "Date ID: \"" + table.Rows[rowIndex][8] + "\"");
status.SetErrorMessage("Failed to write to the database on row " + (rowIndex + 1) + ".");
}
oleDbTransaction?.Rollback();
_logConsole.WriteToLog(FrmLogConsole.Level.Critical, "Rollback completed successfully, " + table.TableName + " table failed on update.");
}
finally
{
_oleDbConnection.Close();
}
return status;
}
/// <summary>
/// Inserts a new ad item into the database and returns the new item's ID number.
+5 -7
View File
@@ -2143,15 +2143,13 @@ namespace AdvertsingProfitControl
{
//Create two DataTables one for the new items to be added to the database
//and one for items that have to be updated.
//New Sales Table Layout (Based on the Database's Physical Layout)
//0:Sold 1:SalePrice 2:TotalSales 3:cost 4:ProfitReturn 5:TotalProfitReturn
//6:FK_AdItemID 7:RowAttribute 8:FK_AdSpecialGroupName (ID) 9:RowPosition (not index based)
//10:FK_DateID
//New inventory Table Layout (Based on the Database's Physical Layout)
//0:Beginning Inventory 1:Received 2:Total Inventory 3:Ending Inventory
//4:AdItemID 5:RowAttribute 6:GroupID 7:RowPosition 8:DateID
insertNewTable = new DataTable("Projections");
//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)
//11:FK_DateID
//0:ID 1:BeginningInventory 2:Received 3:TotalInventory 4:EndingInventory
//5:AdItemID 6:RowAttribute 7:GroupID 8:RowPosition 9:DateID
updateExistingTable = new DataTable("Projections");
//Construct a list of column names for the projections/actual sales DataGridViews and the inventory DataGirdView.
string[] saleColumnNames =