Now Place:2uzhan.com » Windows service using C#

Windows service using C#

Software Development @ March 17, 2008   Views:0

hi there,
I am trying to make my service to insert to a database when it starts as follows :

protected override void OnStart(string[] args)
            string query;
            id += 1;

            SqlCommand comm;
            conn = new SqlConnection("my connection string");
            query = "insert into myTable values('" + id + "'lamya'" + "')";
            comm = new SqlCommand(query, conn);


                    while (id >= 120)
                        query = "insert into myTable values('" + id + "'lamya'" + "')";
                        comm = new SqlCommand(query, conn);


                catch (Exception ex)
                    //throw ex;


after adding an installer and installing the service using the Installutil.exe

when I start the service from the services manager this msg shows up:

The ServiceOnTest service on your Local Computer started and then stopped. Some services stop automatically if they have no work to do, for example, the performance Logs and the Alters service.

where ServiceOnTest is the service name ,

just to know if it matters: the startup type is automatically and it run for the local System

what should I do to make it work, I need it to insert to the database and more but first I need to know what wrong I did?

Urgent help is needed , please


First step would be to log the error message to the event log so you know what went wrong.
Does this code work from a standard app ?

I guess I guess it may fail because SQL Service didn't start yet.. Follow Jerry advice for better error understading..

it may fail because SQL Service didn't start yet..

Is there a way to make the SQL service run before the windows log on ?, if yes, what should I do besides making the SQL service starts up Automatic,


I didn't work with services before I don't know if there's a way to check the service running or not! but I've dummy solution is to try connecting to sql server if it catch an error wait for period of time then try connecting if successed call the method which do some operations with SQL.

Does it insert the record once?

I'm attempting to simplify my fundraising page, and would like to show exactly how much each donation would help us out. I've listed the options below, but I would like ...

Write a java program that asks a person for his height (in meters) and weight (in kilograms) and
outputs one of the messages: underweight, normal, or obese, using the BMI ...

Before I get into the specific issues, I want to provide some other details:

  • Normally, I work on a live, paid server (even in dev); I realize that is ...
© 2018 2uzhan.com Contact