using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Diagnostics; using System.Linq; using System.ServiceProcess; using System.Text; using System.Threading.Tasks; using System.Timers; using System.Data.SqlClient; namespace ProductionService { public partial class CordonelMetersFinalCheck : ServiceBase { private Timer timer; private SqlConnection sqlConnection; public CordonelMetersFinalCheck() { InitializeComponent(); } protected override void OnStart(string[] args) { timer = new Timer(); timer.Interval = 60 * 60 * 1000; // 1 hour in milliseconds timer.Elapsed += new ElapsedEventHandler(OnTimer); timer.Start(); string connectionString = "Server=myServerAddress;Database=myDataBase;User Id=myUsername;Password=myPassword;"; sqlConnection = new SqlConnection(connectionString); sqlConnection.Open(); } protected override void OnStop() { timer.Stop(); sqlConnection.Close(); } private void OnTimer(object sender, ElapsedEventArgs e) { SqlCommand command = new SqlCommand("SELECT COUNT(*) FROM MyTable", sqlConnection); int count = (int)command.ExecuteScalar(); // Do something with the count here } } }