In a recent installation project, I had the need to check if a SQL Server database already existed. This was in a Basic MSI project where I decided to use an InstallScript custom action.
Now, if you read this blog a lot, you know I write a lot of InstallScript custom actions. The main reason is I’m lazy and if I can do what I need to do in InstallShield, I will. However, this might have been a little easier in a Managed Code Custom Action written in C# or Visual Basic.NET. It’s debatable.
Here is the actual InstallScript code that I used. It’s nothing revolutionary. The reason I am sharing it is that I want you to notice how I am building the connection string. SQL Server connection strings are different in 32-bit than they are in 64-bit. I’m not a SQL Server expert, but I have learned that.
Here’s the code.
Using an InstallScript custom action to check if a database exists
You will notice in the code that there are several MessageBox calls. These are just for debugging purposes.
Also notice the WsGetProperty calls. This is just my version of the MsiGetProperty function. InstallShield changed how that function works in the 2012 version, so I created my own function to account for that.
Hope this helps.