windows service application interacting with SQL server database
I have a windows service application that is meant to interact with SQL server database (INSERT,开发者_Go百科 UPDATE, ETC). The windows service application is also multi-threaded.
I created an "App_Data
" folder to keep my database and used app.config
file for connection information, etc.
After installing and starting the service, nothing happens, the database doesnt get updated, etc.
Has anyone ever written a windows service application that interacts with a database? Kindly advice me on how to overcome this problem..
Thanks
From you've described you don't necessarily have a database problem. What you need is a way to debug your windows service. Particularly the OnStart.
Here's what I often put in the OnStart in a Windows Service written in C#
protected override void OnStart(string[] args)
{
foreach (string arg in args)
{
if (arg == "DEBUG_SERVICE")
DebugMode();
}
#if DEBUG
DebugMode();
#endif
timer.Interval = 1;
timer.Start();
}
private static void DebugMode()
{
Debugger.Break();
}
Now when you want to Debug the OnStart you can add the "DEBUG_SERVICE" command argument from the Service Control panel. Otherwise you'll have to try and attach the debugger manually which might not be in time.
Also note how the I start a timer. This allows a separate thread to do the actual work. This is important because you want the OnStart to finish in a timely fashion. A timer isn't required because some windows services respond to an event like a file watcher but more often then not, it seems polling at intervals is what people do in Windows Services.
As far as I know, the App_Data
folder and therefore connection strings pointing to it are only available in ASP.NET web apps and web sites - not in other types of Windows apps.
My recommendation: put your SQL Server database on a database server - can be your local machine and a SQL Server Express database - and connect to that server instance!
精彩评论