You are on page 1of 2

using System;

using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Data.SqlClient;
using System.Diagnostics;
using System.ServiceProcess;
using System.IO;
namespace Q2
{
public class Service1 : System.ServiceProcess.ServiceBase
{
private System.Timers.Timer timer = null;

public Service1()
{
double interval = 1000;
InitializeComponent();
timer = new System.Timers.Timer(interval);
timer.Elapsed += new ElapsedEventHandler(this.ServiceT
imer_Tick);
}

protected override void OnStart(string[] args)


{
timer.AutoReset = true;
timer.Enabled = true;
timer.Start();
}

protected override void OnStop()


{
timer.AutoReset = false;
timer.Enabled = false;
timer.Stop();
}
public void DoCheck()
{
string SqlQuery = @"SELECT ItemNo, Description, Quantity
FROM Item WHERE Quantity < ReOrderLevel";
string ConnString = @"server=RITCHA\SQLDB;database=Jeelz
;uid=sa;pwd=passwd";
SqlConnection MyConnection = new SqlConnection(ConnStrin
g);
SqlDataAdapter myAdapter = new SqlDataAdapter();
SqlCommand mySqlCommand = new SqlCommand(SqlQuery);
mySqlCommand.Connection=MyConnection;
myAdapter.SelectCommand = mySqlCommand;
DataSet ds = new DataSet("Items");
myAdapter.Fill(ds);
if(ds.Tables[0].Rows.Count!=0)
{
FileStream fs = new FileStream(@"c:\Items.txt",
FileMode.OpenOrCreate, FileAccess.Write);
StreamWriter sw = new StreamWriter(fs);
int cnt;
for(cnt=0;cnt<=(ds.Tables[0].Rows.Count-1);cnt++
)
{
string iNo = ds.Tables[0].Rows[cnt].Item
Array[0].ToString().Trim();
string iDesc = ds.Tables[0].Rows[cnt].It
emArray[1].ToString().Trim();
string iQuan = ds.Tables[0].Rows[cnt].It
emArray[2].ToString().Trim();
sw.WriteLine(iNo + ", " + iDesc + ", " +
iQuan);
}
sw.Flush();
sw.Close();
}
}
private void ServiceTimer_Tick(object sender, System.Timers.Ela
psedEventArgs e)
{
DoCheck();
}
}
}