Recent Posts

Forcefully delete a service

October 24, 2010

I needed that when i saw that even after i had uninstalled SQL Server from my machine, the service was still showing in Windows services. This is a sign that SQL was not uninstalled correctly and some of the pieces are still lying around. In this case you have to manually delete the service and do the file system and registry cleanup.

Windows has a command line utility called “sc” (I assume it stands for Service Controller) that gives us the APIs to talk to Windows services. Following command deletes SQLExpress service from the computer, for example.

sc delete MSSQL$SQLExpress

To know the actual name of the service, you can go to the properties (Right click and select properties) of that service from Windows Services window (Start—>Run—>Services.msc) and check the service name.

Finding permutations

October 24, 2010

Here, I am going to present a very compact recursive c# code to compute all the permutations of a given string. Look at the following c# method:

public void PrintPermutations(string stringToPermute, string prefix)
{
   if (stringToPermute.Length <= 0)
   {
       Debug.WriteLine(prefix);
   }
   else
   {
       for(int i=0;i<stringToPermute.Length;i++)
           PrintPermutations(stringToPermute.Replace(stringToPermute[i].ToString(), ""), prefix + stringToPermute[i]);
   }
}

To find all the permutations of the string “ABC”, for example, you will call the method as follows: PrintPermutations(“ABC”, “”); Also note that, I assume that stringToPermute parameter is a string containing non-repeating characters.

Location of executing batch file

October 23, 2010

Often in the batch script we need to change to the directory from where the batch file is executing. Mainly when you are shipping the batch file with your application, you may want to launch some executable from the batch file, for example. So, if you put batch file where the executable is, following batch script will do the job for you: cd %~dp0MyApplication.exe First statement changes current directory to the location of batch file and then second statement launches the application.

Execute SQL statement from command prompt

October 23, 2010

Many a time we run into requirement of executing simple SQL scripts using command prompt. Microsoft gives us a utility called “sqlcmd” just for that. Following is a very simple query executed using this tool: sqlcmd -S .\SQLEXPRESS -E -Q “Drop Database SampleDB” Where SampleDB is the database name i want to drop, -S switch specifies the server instance, -E switch specifies that i want to use trusted connection and -Q switch specifies the SQL query i want to execute. This tool is quite powerful and there are several switches that you can use to meet different requirements. You can open command prompt and see help (sqlcmd /?) for all the different switches that it supports.