I am looking for a simple approach to solve my current challenge. I have the following table:
Id Location DateFrom DateTo RateType Rate
1 LA 01/01/2017 01/01/2018 AverageRate 18
A few mysql commands in my code behind to select these values and display them to textboxes/datepickers:
public partial class MainWindow :
{
public MainWindow()
{
InitializeComponent();
string connectionString = "datasource= ;port=3306;username= ;password= ";
MySqlConnection connection = new MySqlConnection(connectionString);
MySqlCommand sAR = new MySqlCommand("Select Amount from Daily.Rates where RateType = 'Average Hourly Wages'", connection);
MySqlCommand sARF = new MySqlCommand("Select DateFrom from Daily.Rates where RateType = 'Average Hourly Wages'", connection);
MySqlCommand sART = new MySqlCommand("Select DateTo from Daily.Rates where RateType = 'AverageRate'", connection);
try
{
connection.Open();
txtAR.Text = sAHW.ExecuteScalar().ToString();
dfAR.Text = sAHWF.ExecuteScalar().ToString();
dtAR.Text = sAHWT.ExecuteScalar().ToString();
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
connection.Close();
}
What I am trying to achieve:
If I need to increase the rate to 20 I want to keep the old rate of 18, add a copy of that row with the updated values and change the DateTo field as follows: new DateFrom - 1. Basically if I change the rate today I want to end up with this:
Id Location DateFrom DateTo RateType Rate
1 LA 01/01/2017 10/11/2017(DateFrom-1) AverageRate 18
2 LA 10/12/2017 01/01/2018 AverageRate 20
I have quite a few textxboxes that might not change so if none of the values have changed, I don't want to insert a new row, just keep the old values. What would be the best approach to write this query? How can I efficiently combine an if statement with Insert, Update and Select and what I already have in the textboxes in order to achieve my goal? Thank you.