| | |
Need Help with Windows Service
Please support our C# advertiser: Intel Parallel Studio Home
![]() |
•
•
Join Date: Dec 2008
Posts: 1
Reputation:
Solved Threads: 0
Hi There,
I am very very new to C#. I need to create a Windows Service, which should check my sql server database for every 30 seconds, and it should do some calculations on some table and insert them in some other table.
I have the C# code to do those calculations..but I am not understanding where to write this code.
should I write the code in On start Event or In timer1_Tick..?
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.Data.SqlClient;
namespace CalculatingService
{
public partial class MyService : System.ServiceProcess.ServiceBase
{
public MyService()
{
InitializeComponent();
}
protected override void OnStart(string[] args)
{
timer1.Enabled = true;
timer1.Start();
protected override void OnStop()
{
this.timer1.Enabled = false;
}
private void timer1_Tick(object sender, EventArgs e)
{
}
}
}
Here is My c# code to do the calculations
SqlConnection con = new SqlConnection();
con.ConnectionString = "Data Source=Corp-sqldv05\\Instance1;Initial Catalog=DW;User ID=admin;password=aha!";
con.Open();
string dealQry = "select *, month(BeginDate) as beginmonth, datediff(month,BeginDate,dateadd(month,term,BeginDate))as noofmonths, dateadd(month,term,BeginDate) as enddate from deal where needscalc = 1";
SqlDataAdapter adp = new SqlDataAdapter(dealQry, con);
DataSet ds = new DataSet();
adp.Fill(ds);
string cashquery;
SqlCommand cashdltComd = new SqlCommand();
SqlCommand cashIntComd = new SqlCommand();
cashdltComd.Connection = con;
cashIntComd.Connection = con;
double acc_401011, acc_402011, acc_1650011, acc_184019, acc_407011 = 0, acc_900009, acc_900003;
foreach (DataRow dr in ds.Tables[0].Rows)
{
cashquery = "Delete from cashflow where account not in ('401012','412015') and dealid = " + dr["dealid"].ToString();
cashdltComd.CommandText = cashquery;
cashdltComd.ExecuteNonQuery();
acc_401011 = (Convert.ToDouble(dr["BaseRent_PSF"].ToString()) * Convert.ToDouble(dr["SQFT"].ToString())) / 12;
acc_402011 = (Convert.ToDouble(dr["Abate_PSF"].ToString()) * Convert.ToDouble(dr["SQFT"].ToString())) / 12;
acc_1650011 = Convert.ToDouble(dr["TIOnly_PSF"].ToString()) * Convert.ToDouble(dr["SQFT"].ToString());
acc_184019 = Convert.ToDouble(dr["LC_PSF"].ToString()) * Convert.ToDouble(dr["SQFT"].ToString());
if (dr["GrossNet"].ToString().Trim().Equals("N"))
acc_407011 = (Convert.ToDouble(dr["OE_PSF"].ToString()) * Convert.ToDouble(dr["SQFT"].ToString())) / 12;
else if (dr["GrossNet"].ToString().Trim().Equals("G"))
acc_407011 = (Convert.ToDouble(dr["EI_PSF"].ToString()) * Convert.ToDouble(dr["SQFT"].ToString())) / 12;
acc_900009 = Convert.ToDouble(dr["SQFT"].ToString());
acc_900003 = Convert.ToDouble(dr["SQFT"].ToString());
DateTime dt2009 = new DateTime(2009, 01, 01);
DateTime dt2010 = new DateTime(2010, 01, 01);
DateTime begndate = Convert.ToDateTime(dr["BeginDate"].ToString());
DateTime enddate = Convert.ToDateTime(dr["enddate"].ToString());
int abtmonth = Convert.ToInt32(dr["AbateMos"].ToString());
DateTime abtdate;
if (abtmonth >= 1)
abtdate = begndate.AddMonths(abtmonth );
else
abtdate = new DateTime(2007, 01, 01);
int abtend, abtbgn;
if (begndate < dt2009)
{
if (enddate > dt2009)
{
if (enddate.Year == 2009)
{
abtbgn = 1;
if (abtdate.Year > 2009)
abtend = 12;
else if (abtdate.Year == 2009)
abtend = abtdate.Month;
else
{
abtend = 0;
abtbgn = 0;
}
cashIntComd.CommandText = CreateInstCmd(Convert.ToInt32(dr["dealid"].ToString().Trim()), 2, "401011", 1, enddate.Month, acc_401011);
cashIntComd.ExecuteNonQuery();
cashIntComd.CommandText = CreateInstCmd(Convert.ToInt32(dr["dealid"].ToString().Trim()), 2, "402011", abtbgn, abtend, acc_402011);
cashIntComd.ExecuteNonQuery();
cashIntComd.CommandText = CreateInstCmd(Convert.ToInt32(dr["dealid"].ToString().Trim()), 2, "1650011", begndate.Month, begndate.Month, acc_1650011);
cashIntComd.ExecuteNonQuery();
cashIntComd.CommandText = CreateInstCmd(Convert.ToInt32(dr["dealid"].ToString().Trim()), 2, "184019", begndate.Month, begndate.Month, acc_184019);
cashIntComd.ExecuteNonQuery();
cashIntComd.CommandText = CreateInstCmd(Convert.ToInt32(dr["dealid"].ToString().Trim()), 2, "407011", 1, enddate.Month, acc_407011);
cashIntComd.ExecuteNonQuery();
cashIntComd.CommandText = CreateInstCmd(Convert.ToInt32(dr["dealid"].ToString().Trim()), 2, "900009", 1, enddate.Month, acc_900009);
cashIntComd.ExecuteNonQuery();
cashIntComd.CommandText = CreateInstCmd(Convert.ToInt32(dr["dealid"].ToString().Trim()), 2, "900003", 1, enddate.Month, acc_900003);
cashIntComd.ExecuteNonQuery();
abtend = 0;
public String CreateInstCmd(int dealid, int year, string acct, int begn, int end, double amt)
{
string instQuery = "insert into cashflow(DealID, Account, year, January, february, march, april, may, june, july, august, september, october, november, december ) values (";
instQuery = instQuery + dealid.ToString() + "," + acct.ToString() + "," + year.ToString();
for (int i = 1; i <= 12; i++)
{
if (i >= begn && i <= end)
instQuery = instQuery + "," + amt.ToString();
else
instQuery = instQuery + ",0";
}
instQuery = instQuery + ")";
return instQuery;
}
Can Any One Please Help me..How to create windows Service. I am a beginer in windows Service. I dont have any Idea, what it is..It is very Urgent Please Help Me...
Thanks
I am very very new to C#. I need to create a Windows Service, which should check my sql server database for every 30 seconds, and it should do some calculations on some table and insert them in some other table.
I have the C# code to do those calculations..but I am not understanding where to write this code.
should I write the code in On start Event or In timer1_Tick..?
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.Data.SqlClient;
namespace CalculatingService
{
public partial class MyService : System.ServiceProcess.ServiceBase
{
public MyService()
{
InitializeComponent();
}
protected override void OnStart(string[] args)
{
timer1.Enabled = true;
timer1.Start();
protected override void OnStop()
{
this.timer1.Enabled = false;
}
private void timer1_Tick(object sender, EventArgs e)
{
}
}
}
Here is My c# code to do the calculations
SqlConnection con = new SqlConnection();
con.ConnectionString = "Data Source=Corp-sqldv05\\Instance1;Initial Catalog=DW;User ID=admin;password=aha!";
con.Open();
string dealQry = "select *, month(BeginDate) as beginmonth, datediff(month,BeginDate,dateadd(month,term,BeginDate))as noofmonths, dateadd(month,term,BeginDate) as enddate from deal where needscalc = 1";
SqlDataAdapter adp = new SqlDataAdapter(dealQry, con);
DataSet ds = new DataSet();
adp.Fill(ds);
string cashquery;
SqlCommand cashdltComd = new SqlCommand();
SqlCommand cashIntComd = new SqlCommand();
cashdltComd.Connection = con;
cashIntComd.Connection = con;
double acc_401011, acc_402011, acc_1650011, acc_184019, acc_407011 = 0, acc_900009, acc_900003;
foreach (DataRow dr in ds.Tables[0].Rows)
{
cashquery = "Delete from cashflow where account not in ('401012','412015') and dealid = " + dr["dealid"].ToString();
cashdltComd.CommandText = cashquery;
cashdltComd.ExecuteNonQuery();
acc_401011 = (Convert.ToDouble(dr["BaseRent_PSF"].ToString()) * Convert.ToDouble(dr["SQFT"].ToString())) / 12;
acc_402011 = (Convert.ToDouble(dr["Abate_PSF"].ToString()) * Convert.ToDouble(dr["SQFT"].ToString())) / 12;
acc_1650011 = Convert.ToDouble(dr["TIOnly_PSF"].ToString()) * Convert.ToDouble(dr["SQFT"].ToString());
acc_184019 = Convert.ToDouble(dr["LC_PSF"].ToString()) * Convert.ToDouble(dr["SQFT"].ToString());
if (dr["GrossNet"].ToString().Trim().Equals("N"))
acc_407011 = (Convert.ToDouble(dr["OE_PSF"].ToString()) * Convert.ToDouble(dr["SQFT"].ToString())) / 12;
else if (dr["GrossNet"].ToString().Trim().Equals("G"))
acc_407011 = (Convert.ToDouble(dr["EI_PSF"].ToString()) * Convert.ToDouble(dr["SQFT"].ToString())) / 12;
acc_900009 = Convert.ToDouble(dr["SQFT"].ToString());
acc_900003 = Convert.ToDouble(dr["SQFT"].ToString());
DateTime dt2009 = new DateTime(2009, 01, 01);
DateTime dt2010 = new DateTime(2010, 01, 01);
DateTime begndate = Convert.ToDateTime(dr["BeginDate"].ToString());
DateTime enddate = Convert.ToDateTime(dr["enddate"].ToString());
int abtmonth = Convert.ToInt32(dr["AbateMos"].ToString());
DateTime abtdate;
if (abtmonth >= 1)
abtdate = begndate.AddMonths(abtmonth );
else
abtdate = new DateTime(2007, 01, 01);
int abtend, abtbgn;
if (begndate < dt2009)
{
if (enddate > dt2009)
{
if (enddate.Year == 2009)
{
abtbgn = 1;
if (abtdate.Year > 2009)
abtend = 12;
else if (abtdate.Year == 2009)
abtend = abtdate.Month;
else
{
abtend = 0;
abtbgn = 0;
}
cashIntComd.CommandText = CreateInstCmd(Convert.ToInt32(dr["dealid"].ToString().Trim()), 2, "401011", 1, enddate.Month, acc_401011);
cashIntComd.ExecuteNonQuery();
cashIntComd.CommandText = CreateInstCmd(Convert.ToInt32(dr["dealid"].ToString().Trim()), 2, "402011", abtbgn, abtend, acc_402011);
cashIntComd.ExecuteNonQuery();
cashIntComd.CommandText = CreateInstCmd(Convert.ToInt32(dr["dealid"].ToString().Trim()), 2, "1650011", begndate.Month, begndate.Month, acc_1650011);
cashIntComd.ExecuteNonQuery();
cashIntComd.CommandText = CreateInstCmd(Convert.ToInt32(dr["dealid"].ToString().Trim()), 2, "184019", begndate.Month, begndate.Month, acc_184019);
cashIntComd.ExecuteNonQuery();
cashIntComd.CommandText = CreateInstCmd(Convert.ToInt32(dr["dealid"].ToString().Trim()), 2, "407011", 1, enddate.Month, acc_407011);
cashIntComd.ExecuteNonQuery();
cashIntComd.CommandText = CreateInstCmd(Convert.ToInt32(dr["dealid"].ToString().Trim()), 2, "900009", 1, enddate.Month, acc_900009);
cashIntComd.ExecuteNonQuery();
cashIntComd.CommandText = CreateInstCmd(Convert.ToInt32(dr["dealid"].ToString().Trim()), 2, "900003", 1, enddate.Month, acc_900003);
cashIntComd.ExecuteNonQuery();
abtend = 0;
public String CreateInstCmd(int dealid, int year, string acct, int begn, int end, double amt)
{
string instQuery = "insert into cashflow(DealID, Account, year, January, february, march, april, may, june, july, august, september, october, november, december ) values (";
instQuery = instQuery + dealid.ToString() + "," + acct.ToString() + "," + year.ToString();
for (int i = 1; i <= 12; i++)
{
if (i >= begn && i <= end)
instQuery = instQuery + "," + amt.ToString();
else
instQuery = instQuery + ",0";
}
instQuery = instQuery + ")";
return instQuery;
}
Can Any One Please Help me..How to create windows Service. I am a beginer in windows Service. I dont have any Idea, what it is..It is very Urgent Please Help Me...
Thanks
It's better if you want to begin developing new thing is to googlize it,
http://www.c-sharpcorner.com/UploadF...w_service.aspx
http://www.c-sharpcorner.com/UploadF...w_service.aspx
BI Developer | LINKdotNET
B.Sc Computer Science, Helwan University
Technical blog | http://ramymahrous.wordpress.com
LinkedIn | http://www.linkedin.com/in/ramymahrous
B.Sc Computer Science, Helwan University
Technical blog | http://ramymahrous.wordpress.com
LinkedIn | http://www.linkedin.com/in/ramymahrous
![]() |
Similar Threads
- Windows service and threads and sockets (C#)
- Exception uninstalling a windows service (Windows NT / 2000 / XP)
- How to create a windows service? (Visual Basic 4 / 5 / 6)
- How to add any task to windows service? (C#)
- Windows Service Problem (C#)
- Problem with Windows Service (C#)
- Windows XP Service Pack 2 (Windows NT / 2000 / XP)
- windows service pack 2 installation - unique problem (Windows NT / 2000 / XP)
Other Threads in the C# Forum
- Previous Thread: decimal to hexadecimal
- Next Thread: how to display the table data as combobox items
| Thread Tools | Search this Thread |
.net access algorithm array backup barchart bitmap box broadcast buttons c# check checkbox client clock combobox control conversion csharp custom database datagrid datagridview dataset datetime degrees developer development draganddrop drawing dynamiccreation encryption enum excel file form format forms function gdi+ hospitalmanagementsystems httpwebrequest image index input install interface java label list listbox mandelbrot math microsystems mouseclick mysql operator password path photoshop picturebox pixelinversion post priviallages. programming property radians regex remoting richtextbox running... serialization server sleep soap socket sql sqlserver stack statistics stream string table temperature text textbox thread time timer update usercontrol validation visualstudio webbrowser windows windowsformsapplication winforms wpf write xml






