I am trying to edit a database using a service that I have created, that is set to run every minute using a timer. But I cannot for the life of me get it to work, if i run the code for updating the database as a console application then it works fine and updates the database. If i run the timer on its own and just get it to output a message to a text file every time it runs that works, but as soon as i try to get the timer to run the code that updates the database nothing happens. Please see below for the relevant bits of code:
Timer:
protected override void OnStart(string[] args)
{
timer1 = new Timer();
this.timer1.Interval = 10000;
this.timer1.Elapsed += new System.Timers.ElapsedEventHandler(this.timer1_Tick);
timer1.Enabled = true;
Library.WriteErrorLog("Service Started");
}
private void timer1_Tick(object sender, ElapsedEventArgs e)
{
Library.UpdateDatabase();
}
Library.UpdateDatabase()
public static void UpdateDatabase()
{
try
{
string outcome = "blah";
string reason = "testblah";
string company = "test";
string MyConnection2 = "datasource=***.***.***.***;port=3306;username=*********;password=**********";
string Query = "insert into CompanyBackups.Backups(outcome, reason, company) values('" + outcome + "','" + reason + "','" + company + "');";
MySqlConnection MyConn2 = new MySqlConnection(MyConnection2);
MySqlCommand MyCommand2 = new MySqlCommand(Query, MyConn2);
MySqlDataReader MyReader2;
MyConn2.Open();
MyReader2 = MyCommand2.ExecuteReader();
while (MyReader2.Read())
{
}
MyConn2.Close();
WriteErrorLog("Database has been updated");
}catch(Exception ex)
{
WriteErrorLog(ex.Message);
}
}
From what I can figure out the code isn't being run at all...