Marked as 'AdvertsingProfitControl 0.9.5.2' by its folder. I believe this version is where most of the main interface for the user was complete and I was toying with the idea of allowing the user to change the shrink on the main form.

This commit is contained in:
2021-01-28 20:03:54 -06:00
parent 676a9dd890
commit 687e37da94
21 changed files with 2755 additions and 292 deletions
+375 -69
View File
@@ -1,18 +1,321 @@
using System.Collections.Generic;
using System.Data;
using System.Data.OleDb;
using System.Transactions;
using System.Windows.Forms;
namespace AdvertsingProfitControl
{
class DatabaseWriter
internal class DatabaseWriter
{
private readonly OleDbConnection _connectionobject = new OleDbConnection();
private readonly OleDbConnection _oleDbConnection = new OleDbConnection();
private readonly FrmLogConsole _logConsole = FrmLogConsole.GetStaticInstance;
public DatabaseWriter(string connectionString)
{
_connectionobject.ConnectionString = connectionString;
_oleDbConnection.ConnectionString = connectionString;
}
#region New Code
/// <summary>
/// Inserts new records into the specified sales table.
/// 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>
public Dictionary<int, int> InsertIntoSalesTable(DataTable salesTable, string connectionString)
{
var rowIndex = 0;
var rows = new Dictionary<int, int>();
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
};
OleDbTransaction oleDbTransaction = null;
try
{
oleDbConnection.Open();
oleDbTransaction = oleDbConnection.BeginTransaction();
oleDbCommand.Transaction = oleDbTransaction;
for (var i = 0; i < salesTable.Rows.Count; i++)
{
oleDbCommand.CommandText =
"INSERT INTO " + salesTable.TableName + " (Sold, SalePrice, TotalSales, Cost, ProfitReturn, TotalProfitReturn, FK_AdItemID, RowAttribute, FK_AdSpecialGroupName, RowPosition, FK_DateID) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)";
//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
oleDbCommand.Parameters.AddWithValue("Sold", salesTable.Rows[i][0]);
oleDbCommand.Parameters.AddWithValue("SalesPrice", salesTable.Rows[i][1]);
oleDbCommand.Parameters.AddWithValue("TotalSales", salesTable.Rows[i][2]);
oleDbCommand.Parameters.AddWithValue("Cost", salesTable.Rows[i][3]);
oleDbCommand.Parameters.AddWithValue("ProfitReturn", salesTable.Rows[i][4]);
oleDbCommand.Parameters.AddWithValue("TotalProfitReturn", salesTable.Rows[i][5]);
oleDbCommand.Parameters.AddWithValue("adItemID", int.Parse(salesTable.Rows[i][6].ToString()));
oleDbCommand.Parameters.AddWithValue("RowAttribute", int.Parse(salesTable.Rows[i][7].ToString()));
oleDbCommand.Parameters.AddWithValue("adSpecialID", int.Parse(salesTable.Rows[i][8].ToString()));
oleDbCommand.Parameters.AddWithValue("RowPosition", int.Parse(salesTable.Rows[i][9].ToString()));
oleDbCommand.Parameters.AddWithValue("dateID", salesTable.Rows[i][10]);
oleDbCommand.ExecuteNonQuery();
oleDbCommand.Parameters.Clear();
rowIndex++;
}
oleDbTransaction.Commit();
foreach (DataRow row in salesTable.Rows)
{
var rowId = RetrieveRowId(salesTable.TableName, int.Parse(row[6].ToString()), int.Parse(row[10].ToString()));
if (int.Parse(row[8].ToString()) != 0)
{
//Account for the ad special row.
rows.Add(int.Parse(row[9].ToString()) + 1, rowId);
}
else
{
rows.Add(int.Parse(row[9].ToString()), rowId);
}
}
}
catch (OleDbException ex)
{
_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.");
}
else
{
_logConsole.WriteToLog(FrmLogConsole.Level.Error,
"Failed on row " + (rowIndex + 1) + " due to the above error. Dumping contents of row " + (rowIndex + 1) + " from " + salesTable.TableName + ".");
_logConsole.WriteToLog(FrmLogConsole.Level.Error,
"Ad Item ID: \"" +
salesTable.Rows[rowIndex][6] + "\" Sold: \"" + salesTable.Rows[rowIndex][0] + "\"");
_logConsole.WriteToLog(FrmLogConsole.Level.Error,
"Sale Price: \"" + salesTable.Rows[rowIndex][1] + "\" Total Sales: \"" + salesTable.Rows[rowIndex][2] + "\"");
_logConsole.WriteToLog(FrmLogConsole.Level.Error,
"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] + "\"");
}
oleDbTransaction?.Rollback();
_logConsole.WriteToLog(FrmLogConsole.Level.Critical, "Rollback completed successfully, " + salesTable.TableName+ " table failed on insertion.");
}
finally
{
_oleDbConnection.Close();
}
return rows;
}
public bool UpdateSalesTable(DataTable salesTable, string connectionString)
{
var wasSuccessful = false;
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
};
OleDbTransaction oleDbTransaction = null;
try
{
oleDbTransaction = oleDbConnection.BeginTransaction();
_oleDbConnection.Open();
oleDbCommand.Transaction = oleDbTransaction;
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 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
oleDbCommand.Parameters.AddWithValue("Sold", salesTable.Rows[i][1]);
oleDbCommand.Parameters.AddWithValue("SalesPrice", salesTable.Rows[i][2]);
oleDbCommand.Parameters.AddWithValue("TotalSales", salesTable.Rows[i][3]);
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("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();
rowIndex++;
}
oleDbTransaction.Commit();
wasSuccessful = true;
}
catch (OleDbException ex)
{
_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.");
}
else
{
_logConsole.WriteToLog(FrmLogConsole.Level.Error,
"Failed on row " + (rowIndex + 1) + " due to the above error. Dumping contents of row " + (rowIndex + 1) + " from " + salesTable.TableName + ".");
_logConsole.WriteToLog(FrmLogConsole.Level.Error,
"Ad Item ID: \"" +
salesTable.Rows[rowIndex][7] + "\" Sold: \"" + salesTable.Rows[rowIndex][1] + "\"");
_logConsole.WriteToLog(FrmLogConsole.Level.Error,
"Sale Price: \"" + salesTable.Rows[rowIndex][2] + "\" Total Sales: \"" + salesTable.Rows[rowIndex][3] + "\"");
_logConsole.WriteToLog(FrmLogConsole.Level.Error,
"Cost: \"" + salesTable.Rows[rowIndex][4] + "\" Profit Return: \"" + salesTable.Rows[rowIndex][5] + "\"");
_logConsole.WriteToLog(FrmLogConsole.Level.Error,
"Total Profit Return: \"" +
salesTable.Rows[rowIndex][6] + "\" Ad Special Group: \"" + salesTable.Rows[rowIndex][9] + "\"");
}
oleDbTransaction?.Rollback();
_logConsole.WriteToLog(FrmLogConsole.Level.Critical, "Rollback completed successfully, " + salesTable.TableName + " table failed on update.");
}
finally
{
_oleDbConnection.Close();
}
return wasSuccessful;
}
/// <summary>
/// Inserts a new ad item into the database and returns the new item's ID number.
/// Supports rolling back as to not corrupt the database.
/// </summary>
/// <param name="adItemName">The ad item's name that is to be added.</param>
/// <param name="oleDbCommand">A reference to a command that already has a transaction active.</param>
/// <returns>The ID number of the ad item that was just added.</returns>
public int InsertNewAdItem(string adItemName, OleDbCommand oleDbCommand = null)
{
var adItemId = 0;
var needsTransaction = false;
if (oleDbCommand == null)
{
oleDbCommand = new OleDbCommand
{
Connection = _oleDbConnection
};
needsTransaction = true;
}
oleDbCommand.CommandText = "INSERT INTO AdItem (AdItem) VALUES (?)";
oleDbCommand.Parameters.Clear();
oleDbCommand.Parameters.AddWithValue("AdItem", adItemName);
OleDbTransaction oleDbTransaction = null;
try
{
if (needsTransaction)
{
//If the OleDbCommand object is not supplied, then we'll need to create and assign the transaction object.
oleDbCommand.Connection = _oleDbConnection;
_oleDbConnection.Open();
oleDbTransaction = _oleDbConnection.BeginTransaction();
oleDbCommand.Transaction = oleDbTransaction;
}
if (oleDbCommand.ExecuteNonQuery() == 1)
{
oleDbCommand.CommandText = "SELECT ID FROM AdItem WHERE AdItem.AdItem = ?";
oleDbCommand.Parameters.Clear();
oleDbCommand.Parameters.AddWithValue("AdItem", adItemName);
var reader = oleDbCommand.ExecuteReader();
//Get the ID of the new ad item.
while (reader != null && reader.Read())
{
int.TryParse(reader[0].ToString(), out adItemId);
}
reader?.Close();
if (adItemId != 0)
{
//All was successful.
oleDbTransaction?.Commit();
}
else
{
//
_logConsole.WriteToLog(FrmLogConsole.Level.Error,
"Ad item \"" + adItemName + "\" was not found in the database; rolling back changes.");
oleDbTransaction?.Rollback();
}
}
else
{
//Something failed but wasn't caught.
_logConsole.WriteToLog(FrmLogConsole.Level.Error,
"Failed to add the ad item \"" + adItemName + "\" into the database.");
oleDbTransaction?.Rollback();
}
}
catch (OleDbException e)
{
_logConsole.WriteToLog(FrmLogConsole.Level.Error, e.Message);
_logConsole.WriteToLog(FrmLogConsole.Level.Error,
"Failed to insert ad item " + adItemName + " into the database.");
oleDbTransaction?.Rollback();
}
finally
{
if (needsTransaction)
{
_oleDbConnection.Close();
}
}
return adItemId;
}
/// <summary>
/// Infrastructure for the DatabaseWriter class, not meant to be used with external code.
/// Grabs the ID of the specified row.
/// </summary>
/// <param name="tableName">The name of the table to check for the row in.</param>
/// <param name="adItemId">The ID of the ad item in the row.</param>
/// <param name="dateId">The ID of the date in the row.</param>
/// <returns>The ID of the row, zero(0) if no rows are found.</returns>
private int RetrieveRowId(string tableName, int adItemId, int dateId)
{
var id = 0;
var rowsEffected = 0;
var oleDbCommand = new OleDbCommand
{
CommandText = "SELECT ID FROM " + tableName + " WHERE FK_AdItemID = ? AND FK_DateID = ?",
Connection = _oleDbConnection
};
oleDbCommand.Parameters.AddWithValue("AdItemID", adItemId);
oleDbCommand.Parameters.AddWithValue("DateID", dateId);
try
{
_oleDbConnection.Open();
var reader = oleDbCommand.ExecuteReader();
while (reader != null && reader.Read())
{
rowsEffected++;
int.TryParse(reader[0].ToString(), out id);
}
reader?.Close();
if (rowsEffected > 1)
{
_logConsole.WriteToLog(FrmLogConsole.Level.Error,
"Redundancy found row with the ad item ID " + adItemId + " with the date ID " + dateId + ".");
}
}
catch (OleDbException e)
{
_logConsole.WriteToLog(FrmLogConsole.Level.Error, "Failed to look up the existence of a row with the ad item ID " + adItemId + " with the date ID " + dateId + ".");
_logConsole.WriteToLog(FrmLogConsole.Level.Error, e.Message);
}
_oleDbConnection.Close();
return id;
}
#endregion
//10 to 1 ratio of tries to code
//http://codebetter.com/karlseguin/2006/04/05/understanding-and-using-exceptions/nfrastructure
@@ -27,7 +330,7 @@ namespace AdvertsingProfitControl
CommandText = "INSERT INTO WeekEnding(EndOfWeekDate) VALUES ( dateString )"
};
oleDbCommand.Parameters.AddWithValue("dateString", dateString);
oleDbCommand.Connection = _connectionobject;
oleDbCommand.Connection = _oleDbConnection;
//Try to execute the query
try
{
@@ -35,8 +338,8 @@ namespace AdvertsingProfitControl
//Check for the existence of the date string that was provided.
oleDbCommand.CommandText = "SELECT ID FROM WeekEnding WHERE EndOfWeekDate = @dateString";
oleDbCommand.Parameters.AddWithValue("@dateString", dateString);
oleDbCommand.Connection = _connectionobject;
_connectionobject.Open();
oleDbCommand.Connection = _oleDbConnection;
_oleDbConnection.Open();
//Execute the SELECT statement and retrieve the row(s) effected.
var reader = oleDbCommand.ExecuteReader();
var recordIndex = 0;
@@ -48,7 +351,7 @@ namespace AdvertsingProfitControl
}
recordIndex++;
}
if (reader != null) reader.Close();
reader?.Close();
//IF one row was affected, then return with "True", since the value is already there.
if (numberOfRowsEffected == 1)
{
@@ -103,7 +406,7 @@ namespace AdvertsingProfitControl
finally
{
//Make certain that the connection is closed before returning.
_connectionobject.Close();
_oleDbConnection.Close();
}
return insertWasSuccessful;
@@ -115,7 +418,7 @@ namespace AdvertsingProfitControl
var insertWasSuccessful = false;
var storedComment = "";
//Set up the command object, SQL command string and parameters.
var oleDbCommand = new OleDbCommand {Connection = _connectionobject};
var oleDbCommand = new OleDbCommand {Connection = _oleDbConnection};
//Try to execute the query
try
{
@@ -127,7 +430,7 @@ namespace AdvertsingProfitControl
oleDbCommand.CommandText = "SELECT Comment FROM Comment WHERE FK_DateID = dateID";
oleDbCommand.Parameters.Clear();
oleDbCommand.Parameters.AddWithValue("dateID", dateIdString);
_connectionobject.Open();
_oleDbConnection.Open();
//OpenConnection();
var reader = oleDbCommand.ExecuteReader();
var recordIndex = 0;
@@ -146,7 +449,7 @@ namespace AdvertsingProfitControl
recordIndex++;
}
}
if (reader != null) reader.Close();
reader?.Close();
//IF there is a comment that has the same date then check to see if it is the same as the one entered.
if (recordIndex == 1)
{
@@ -235,7 +538,7 @@ namespace AdvertsingProfitControl
finally
{
//Make certain that the connection is closed before returning.
_connectionobject.Close();
_oleDbConnection.Close();
}
return insertWasSuccessful;
@@ -257,7 +560,7 @@ namespace AdvertsingProfitControl
var noRowsAdded = new List<int> {0};
return noRowsAdded;
}
_connectionobject.Open();
_oleDbConnection.Open();
var rowsAdded = new List<int>();
var rowNumber = 0;
//DataTable's structure will reflect the APC database table structure.
@@ -309,7 +612,7 @@ namespace AdvertsingProfitControl
oleDbCommand.Parameters.AddWithValue("AdItemID", adItemId);
oleDbCommand.Parameters.AddWithValue("GroupID", row[19]);
oleDbCommand.Parameters.AddWithValue("dateID", row[20]);
oleDbCommand.Connection = _connectionobject;
oleDbCommand.Connection = _oleDbConnection;
try
{
@@ -328,7 +631,7 @@ namespace AdvertsingProfitControl
{
_logConsole.WriteToLog(FrmLogConsole.Level.Error, "An error occurred trying to write a record to the APC table.");
_logConsole.WriteToLog(FrmLogConsole.Level.Debug, e.Message);
_connectionobject.Close();
_oleDbConnection.Close();
//Add an error code at the end of array of rows that were added to show an error occurred.
rowsAdded.Add(-1);
return rowsAdded;
@@ -343,7 +646,7 @@ namespace AdvertsingProfitControl
rowsAdded.Add(0);
_logConsole.WriteToLog(FrmLogConsole.Level.Error, "An error occurred trying to update a record APC table.");
_logConsole.WriteToLog(FrmLogConsole.Level.Debug, "An error occurred trying to update ad item ID " + adItemId + " and dateID " + dateId + ".");
_connectionobject.Close();
_oleDbConnection.Close();
return rowsAdded;
}
}
@@ -351,7 +654,7 @@ namespace AdvertsingProfitControl
}
//Close the database connection before returning.
_connectionobject.Close();
_oleDbConnection.Close();
return rowsAdded;
}
@@ -370,15 +673,16 @@ namespace AdvertsingProfitControl
oleDbCommand.CommandText = "SELECT ID FROM APC WHERE FK_AdItemID = adItemID AND FK_DateID = dateID";
oleDbCommand.Parameters.AddWithValue("adItemID", adItemId);
oleDbCommand.Parameters.AddWithValue("dateID", dateId);
oleDbCommand.Connection = _connectionobject;
oleDbCommand.Connection = _oleDbConnection;
try
{
oleDbCommand.Transaction = _oleDbConnection.BeginTransaction();
var reader = oleDbCommand.ExecuteReader();
while (reader != null && reader.Read())
{
rowsEffected++;
}
if (reader != null) reader.Close();
reader?.Close();
}
catch (OleDbException ex)
{
@@ -424,10 +728,12 @@ namespace AdvertsingProfitControl
}
//Record the number of rows affected when the statement is executed, again assume nothing was affected.
var adItemId = "0";
var oleDbCommand = new OleDbCommand();
oleDbCommand.CommandText = "INSERT INTO AdItem (AdItem) VALUES (?)";
var oleDbCommand = new OleDbCommand
{
CommandText = "INSERT INTO AdItem (AdItem) VALUES (?)",
Connection = _oleDbConnection
};
oleDbCommand.Parameters.AddWithValue("adItem", adItemName);
oleDbCommand.Connection = _connectionobject;
//Attempt to execute the INSERT SQL statement.
try
{
@@ -443,7 +749,7 @@ namespace AdvertsingProfitControl
{
adItemId = reader[0].ToString();
}
if (reader != null) reader.Close();
reader?.Close();
}
//ELSE IF more then one (1) row was affected...
else if (rowsEffected > 1)
@@ -487,7 +793,7 @@ namespace AdvertsingProfitControl
}
var oleDbCommand = new OleDbCommand {CommandText = "SELECT ID FROM AdItem WHERE UCASE(AdItem.AdItem) = UCASE(adItemName)"};
oleDbCommand.Parameters.AddWithValue("adItemName", adItemName);
oleDbCommand.Connection = _connectionobject;
oleDbCommand.Connection = _oleDbConnection;
try
{
@@ -557,7 +863,7 @@ namespace AdvertsingProfitControl
oleDbCommand.Parameters.AddWithValue("groupID", parameters[19]);
oleDbCommand.Parameters.AddWithValue("dateID", parameters[20]);
oleDbCommand.Parameters.AddWithValue("adItemID", adItemId);
oleDbCommand.Connection = _connectionobject;
oleDbCommand.Connection = _oleDbConnection;
try
{
@@ -597,22 +903,22 @@ namespace AdvertsingProfitControl
var supplierQueryCommand = new OleDbCommand
{
CommandText = "SELECT ID FROM Supplier WHERE SupplierName = ?",
Connection = _connectionobject
Connection = _oleDbConnection
};
//Grabbing the ID of the invoice by the invoice number and the supplier's ID.
var invoiceCheckCommand = new OleDbCommand
{
CommandText = "SELECT ID FROM Invoice WHERE InvoiceNumber = ? AND FK_Supplier = ?",
Connection = _connectionobject
Connection = _oleDbConnection
};
//Inserting a new record into the Invoice table.
var insertNewInvoice = new OleDbCommand
{
CommandText =
"INSERT INTO Invoice (InvoiceDate, InvoiceNumber, InvoiceNetAmountAtCost, InvoiceNetAmount, InvoiceNote, FK_Supplier, FK_DateID) VALUES (?, ?, ?, ?, ?, ?, ?)",
Connection = _connectionobject
Connection = _oleDbConnection
};
_connectionobject.Open();
_oleDbConnection.Open();
foreach (DataRow row in parameters.Rows)
{
var itemCount = 0;
@@ -736,12 +1042,12 @@ namespace AdvertsingProfitControl
{
_logConsole.WriteToLog(FrmLogConsole.Level.Error, "Failed to write supplier " + row[1] + " into the invoice table.");
_logConsole.WriteToLog(FrmLogConsole.Level.Debug, e.Message);
_connectionobject.Close();
_oleDbConnection.Close();
break;
}
}
_connectionobject.Close();
_oleDbConnection.Close();
return rowsAdded;
}
/// <summary>
@@ -767,7 +1073,7 @@ namespace AdvertsingProfitControl
var supplierNameId = "0";
var oleDbCommand = new OleDbCommand {CommandText = "INSERT INTO Supplier (SupplierName) VALUES (?)"};
oleDbCommand.Parameters.AddWithValue("SupplierName", supplierName);
oleDbCommand.Connection = _connectionobject;
oleDbCommand.Connection = _oleDbConnection;
try
{
@@ -825,7 +1131,7 @@ namespace AdvertsingProfitControl
oleDbCommand.Parameters.AddWithValue("InvoiceNumber", invoiceField[2]);
oleDbCommand.Parameters.AddWithValue("SupplierID", supplierId);
oleDbCommand.Parameters.AddWithValue("DateID", invoiceField[6]);
oleDbCommand.Connection = _connectionobject;
oleDbCommand.Connection = _oleDbConnection;
try
{
@@ -863,7 +1169,7 @@ namespace AdvertsingProfitControl
CommandText = "SELECT ID FROM WeeklySales WHERE FK_DateID = ?"
};
weekEndingDateCheck.Parameters.AddWithValue("DateID", parameters[8]);
weekEndingDateCheck.Connection = _connectionobject;
weekEndingDateCheck.Connection = _oleDbConnection;
//Object to update a field with the date supplied, assuming an entry was found with the previous object.
var updateWeeklySales = new OleDbCommand
{
@@ -879,7 +1185,7 @@ namespace AdvertsingProfitControl
updateWeeklySales.Parameters.AddWithValue("Saturday", parameters[6]);
updateWeeklySales.Parameters.AddWithValue("TotalSales", parameters[7]);
updateWeeklySales.Parameters.AddWithValue("DateID", parameters[8]);
updateWeeklySales.Connection = _connectionobject;
updateWeeklySales.Connection = _oleDbConnection;
//Object to insert a new field into the database if no entry is present.
var insertWeeklySales = new OleDbCommand
{
@@ -895,10 +1201,10 @@ namespace AdvertsingProfitControl
insertWeeklySales.Parameters.AddWithValue("Saturday", parameters[6]);
insertWeeklySales.Parameters.AddWithValue("TotalSales", parameters[7]);
insertWeeklySales.Parameters.AddWithValue("DateID", parameters[8]);
insertWeeklySales.Connection = _connectionobject;
insertWeeklySales.Connection = _oleDbConnection;
try
{
_connectionobject.Open();
_oleDbConnection.Open();
//Execute the select statement to check for the presence of an entry with the supplied date (parameters[8]).
var reader = weekEndingDateCheck.ExecuteReader();
@@ -931,7 +1237,7 @@ namespace AdvertsingProfitControl
}
finally
{
_connectionobject.Close();
_oleDbConnection.Close();
}
return rowsEffected;
@@ -947,18 +1253,18 @@ namespace AdvertsingProfitControl
CommandText = "SELECT ID FROM GroupCategory WHERE GroupDescription = ?"
};
categoryDescCheckQueryCommand.Parameters.AddWithValue("GroupName", groupName);
categoryDescCheckQueryCommand.Connection = _connectionobject;
categoryDescCheckQueryCommand.Connection = _oleDbConnection;
//Setting up an object to insert a new ad special keyword.
var insertNewCategoryDesc = new OleDbCommand
{
CommandText = "INSERT INTO GroupCategory (GroupDescription) VALUES (?)"
};
insertNewCategoryDesc.Parameters.AddWithValue("GroupName", groupName);
insertNewCategoryDesc.Connection = _connectionobject;
insertNewCategoryDesc.Connection = _oleDbConnection;
try
{
_connectionobject.Open();
_oleDbConnection.Open();
var reader = categoryDescCheckQueryCommand.ExecuteReader();
while (reader != null && reader.Read())
@@ -985,7 +1291,7 @@ namespace AdvertsingProfitControl
}
finally
{
_connectionobject.Close();
_oleDbConnection.Close();
}
return rowsEffected;
@@ -999,11 +1305,11 @@ namespace AdvertsingProfitControl
CommandText = "DELETE FROM GroupCategory WHERE GroupDescription = ?"
};
oleDbCommand.Parameters.AddWithValue("AdSpecialName", adSpecialName);
oleDbCommand.Connection = _connectionobject;
oleDbCommand.Connection = _oleDbConnection;
try
{
_connectionobject.Open();
_oleDbConnection.Open();
rowsEffected = oleDbCommand.ExecuteNonQuery();
}
catch (OleDbException e)
@@ -1014,7 +1320,7 @@ namespace AdvertsingProfitControl
}
finally
{
_connectionobject.Close();
_oleDbConnection.Close();
}
return rowsEffected;
@@ -1029,11 +1335,11 @@ namespace AdvertsingProfitControl
};
deleteCommand.Parameters.AddWithValue("AdItemID", adItemId);
deleteCommand.Parameters.AddWithValue("DateID", dateId);
deleteCommand.Connection = _connectionobject;
deleteCommand.Connection = _oleDbConnection;
try
{
_connectionobject.Open();
_oleDbConnection.Open();
recordsDeleted = deleteCommand.ExecuteNonQuery();
}
catch (OleDbException e)
@@ -1043,7 +1349,7 @@ namespace AdvertsingProfitControl
}
finally
{
_connectionobject.Close();
_oleDbConnection.Close();
}
return recordsDeleted;
@@ -1058,11 +1364,11 @@ namespace AdvertsingProfitControl
};
oleDbCommand.Parameters.AddWithValue("InvoiceNumber", invoiceNumber);
oleDbCommand.Parameters.AddWithValue("DateID", dateId);
oleDbCommand.Connection = _connectionobject;
oleDbCommand.Connection = _oleDbConnection;
try
{
_connectionobject.Open();
_oleDbConnection.Open();
recordsAffected = oleDbCommand.ExecuteNonQuery();
}
catch (OleDbException e)
@@ -1072,7 +1378,7 @@ namespace AdvertsingProfitControl
}
finally
{
_connectionobject.Close();
_oleDbConnection.Close();
}
return recordsAffected;
@@ -1093,39 +1399,39 @@ namespace AdvertsingProfitControl
CommandText = "DELETE FROM APC WHERE FK_DateID = ?"
};
clearApCommand.Parameters.AddWithValue("DateID", dateId);
clearApCommand.Connection = _connectionobject;
clearApCommand.Connection = _oleDbConnection;
//Object to clear comments associated with the date ID.
var clearCommentsCommand = new OleDbCommand()
{
CommandText = "DELETE FROM Comment WHERE FK_DateID = ?"
};
clearCommentsCommand.Parameters.AddWithValue("DateID", dateId);
clearCommentsCommand.Connection = _connectionobject;
clearCommentsCommand.Connection = _oleDbConnection;
//Object to clear the invoices associated with the date ID.
var clearInvoicesCommand = new OleDbCommand()
{
CommandText = "DELETE FROM Invoice WHERE FK_DateID = ?"
};
clearInvoicesCommand.Parameters.AddWithValue("DateID", dateId);
clearInvoicesCommand.Connection = _connectionobject;
clearInvoicesCommand.Connection = _oleDbConnection;
//Object to clear the weekly sales associated with the date ID.
var clearWeeklySalesCommand = new OleDbCommand()
{
CommandText = "DELETE FROM WeeklySales WHERE FK_DateID = ?"
};
clearWeeklySalesCommand.Parameters.AddWithValue("DateID", dateId);
clearWeeklySalesCommand.Connection = _connectionobject;
clearWeeklySalesCommand.Connection = _oleDbConnection;
//Object to clear the date associated with the ID passed in.
var clearEndOfWeekDateCommand = new OleDbCommand()
{
CommandText = "DELETE FROM WeekEnding WHERE ID = ?"
};
clearEndOfWeekDateCommand.Parameters.AddWithValue("DateID", dateId);
clearEndOfWeekDateCommand.Connection = _connectionobject;
clearEndOfWeekDateCommand.Connection = _oleDbConnection;
try
{
_connectionobject.Open();
_oleDbConnection.Open();
recordsRemoved += clearApCommand.ExecuteNonQuery();
recordsRemoved += clearCommentsCommand.ExecuteNonQuery();
recordsRemoved += clearInvoicesCommand.ExecuteNonQuery();
@@ -1139,7 +1445,7 @@ namespace AdvertsingProfitControl
}
finally
{
_connectionobject.Close();
_oleDbConnection.Close();
}
return recordsRemoved;
}
@@ -1161,12 +1467,12 @@ namespace AdvertsingProfitControl
};
oleDbCommand.Parameters.AddWithValue("Comment", comment);
oleDbCommand.Parameters.AddWithValue("DateID", dateId);
oleDbCommand.Connection = _connectionobject;
oleDbCommand.Connection = _oleDbConnection;
try
{
//IF the delete command deletes more then one row it returns 0.
_connectionobject.Open();
_oleDbConnection.Open();
var recordsEffected = oleDbCommand.ExecuteNonQuery();
if (recordsEffected == 1)
{
@@ -1180,7 +1486,7 @@ namespace AdvertsingProfitControl
}
finally
{
_connectionobject.Close();
_oleDbConnection.Close();
}
return wasSuccessful;
@@ -1194,11 +1500,11 @@ namespace AdvertsingProfitControl
CommandText = "DELETE FROM AdItem WHERE AdItem = ?"
};
oleDbCommand.Parameters.AddWithValue("AdItemName", adItemName);
oleDbCommand.Connection = _connectionobject;
oleDbCommand.Connection = _oleDbConnection;
try
{
_connectionobject.Open();
_oleDbConnection.Open();
var rowsEffected = oleDbCommand.ExecuteNonQuery();
if(rowsEffected == 1)
@@ -1222,7 +1528,7 @@ namespace AdvertsingProfitControl
}
finally
{
_connectionobject.Close();
_oleDbConnection.Close();
}
return success;
@@ -1236,17 +1542,17 @@ namespace AdvertsingProfitControl
CommandText = "INSERT INTO AdItem (AdItem) VALUES (?)"
};
oleDbCommand.Parameters.AddWithValue("AdItemName", adItemName);
oleDbCommand.Connection = _connectionobject;
oleDbCommand.Connection = _oleDbConnection;
var redundantancyCheck = new OleDbCommand
{
CommandText = "SELECT AdItem FROM AdItem WHERE AdItem = ?"
};
redundantancyCheck.Parameters.AddWithValue("AdItemName", adItemName);
redundantancyCheck.Connection = _connectionobject;
redundantancyCheck.Connection = _oleDbConnection;
try
{
_connectionobject.Open();
_oleDbConnection.Open();
using(var reader = redundantancyCheck.ExecuteReader())
{
var rows = 0;
@@ -1280,7 +1586,7 @@ namespace AdvertsingProfitControl
}
finally
{
_connectionobject.Close();
_oleDbConnection.Close();
}
return success;