Geeks With Blogs

Arthur Zubarev Compudicted

Today I needed to create some logic to execute database objects install script, although only once, upon a windows service installation. I recalled that I have already done in the past the auto-startup of a windows service using the Installer class, so I thought why not to just add some logic.

Step #1: I scripted the needed database objects into a file called Create_usp_MyStoredProcedure.sql

Step #2: I added this file to my VS2005 project, set its Build Action to content and then added it into the Setup/Install project's content folder.

Step #3, and the final is to add code (in my case it is the Committed event, but perhaps AfterInstall also makes sense) to the MyInstaller class derived from Installer:

private void MyInstaller_Committed(object sender, InstallEventArgs e)
{
  //Reference the currently executing process to get its location on disk
 Assembly asm = Assembly.GetExecutingAssembly();
 String asmLoc = asm.Location.Substring(0, asm.Location.LastIndexOf("\\"));
 
 //Create database object(s) executing sql script file by SQLCMD via DOS
System.Diagnostics.Process.Start("cmd", " /C SQLCMD -SMyServer -E -i \"" + asmLoc
+ "\\" + "Create_usp_MyStoredProcedure.sql \" -o \"" + asmLoc
+ "\\" + "Create_usp_MyStoredProcedure_Log.txt\"");                      
}
Potentially this command can do any kind of payload e.g. file operations or perhaps an email notifying on service installation. The key point here is to have the /C CMD switch, it can be replaced to /K in case something awry happens to the command so the DOS shell will remain open permitting you to examine errors.
Posted on Tuesday, September 23, 2008 1:35 PM | Back to top


Comments on this post: Way to execute a command upon Windows Service Installation

No comments posted yet.
Your comment:
 (will show your gravatar)


Copyright © Compudicted | Powered by: GeeksWithBlogs.net