Windows Develop Bookmark and Share   
 index > Windows Forms Sample Applications > Seeking a windows service example that connects to database
 

Seeking a windows service example that connects to database

Is there a windows service application example that connects to a database. I am trying to write a dll or exe that polls a database, brings back a data set, translates it to XML and sends it on via FTP. It is the windows service bit that I am stuck on. There seems to be no example that uses a windows service with a database or any books describing it.

Richa333  Sunday, September 09, 2007 10:50 AM

It’s no hard to create a Windows Service application, you can check this workthrough for the steps
How to: Create Windows Services
and this project:
Simple Windows Service Sample
You can retrieve data from database in this way in the Service class
(You can use a timer to load data every a certain time if you need)

Code Snippet

partial class Service1 : ServiceBase

{

public Service1()

{

InitializeComponent();

}

protected override void OnStart(string[] args)

{

string connstr = "Persist Security Info=False;Integrated Security=SSPI;" +

"Initial Catalog=Northwind;server=localhost";

SqlConnection conn = new SqlConnection(connstr);

string sql = "SELECT CompanyName,Address From Customers";

SqlDataAdapter da = new SqlDataAdapter(sql, conn);

DataSet ds = new DataSet();

da.Fill(ds, "Customers");

//

System.Xml.XmlDataDocument doc = new System.Xml.XmlDataDocument(ds);

// your stuff with the xml

}

protected override void OnStop()

{

// TODO: Add code here to perform any tear-down necessary to stop your service.

}



However, you need to create a setup project to install the Services. The links I write above contains lots of information about how to do this.

Zhi-Xin Ye  Thursday, September 13, 2007 12:46 PM

It’s no hard to create a Windows Service application, you can check this workthrough for the steps
How to: Create Windows Services
and this project:
Simple Windows Service Sample
You can retrieve data from database in this way in the Service class
(You can use a timer to load data every a certain time if you need)

Code Snippet

partial class Service1 : ServiceBase

{

public Service1()

{

InitializeComponent();

}

protected override void OnStart(string[] args)

{

string connstr = "Persist Security Info=False;Integrated Security=SSPI;" +

"Initial Catalog=Northwind;server=localhost";

SqlConnection conn = new SqlConnection(connstr);

string sql = "SELECT CompanyName,Address From Customers";

SqlDataAdapter da = new SqlDataAdapter(sql, conn);

DataSet ds = new DataSet();

da.Fill(ds, "Customers");

//

System.Xml.XmlDataDocument doc = new System.Xml.XmlDataDocument(ds);

// your stuff with the xml

}

protected override void OnStop()

{

// TODO: Add code here to perform any tear-down necessary to stop your service.

}



However, you need to create a setup project to install the Services. The links I write above contains lots of information about how to do this.

Zhi-Xin Ye  Thursday, September 13, 2007 12:46 PM

You can use google to search for other answers

Custom Search

More Threads

• how to create those dataset class files in web services?
• Latest download of C# Movie Collection Starter Kit did not have online search enabled
• Remote access performance
• not able to download xml files
• Web Service location
• Diffgram questions
• Terrarium servers around the world?
• ParamArray
• How to serialize your target
• I'm new