Implementing a Custom ConnectionString Installer for Setup
MSBuild is supposed to be powerful. However, its integration into Visual Studio does not seem intuitive. Sometimes, changing the simplest things—like the text that shows up on the opening screen of the install—seems to be challenging.
MSBuild has a lot of potential, and the integration in .NET is confusing, so I think. However, adding custom actions lets you do just about anything you want to do. One of the things that you might want to do is let the user define the database connection, and if you are security conscious—and we all should be—you might want to encrypt that connection string. In this article, you will define a setup project with a custom action that lets the user define the connection string using the Data Links dialog and encrypts that connection string using RSA encryption.
Creating a Simple Database Project
The database project is used in this case to test the correct functioning of the setup project. Listing 1 contains a very simple console application that consumes the encrypted connection string of the setup project. The database console application reads the connection string, opens the database, and checks the state of the connection. Assuming the setup performed correctly, the database state should be open. Here is the sample database application.
Listing 1: A sample application that tests the correct deployment of the encrypted connection string.
Imports System.Data Imports System.Data.SqlClient Imports System.Configuration Module Module1 Sub Main() Using connection As _ New SqlConnection(My.MySettings.Default.MyConnectionString) connection.Open() Console.WriteLine(connection.ConnectionString) Console.WriteLine(connection.State = ConnectionState.Open) Console.ReadLine() End Using End Sub End Module
Adding an Application Connection String
You can use the Visual Studio properties editor to define the connection string referred to (in Listing 1 as My.MySettings.Default.MyConnectionString). The connection string property referred to in the sample application is the same value you want to write in the setup project. To add a connection string in the settings editor (shown in Figure 1), follow the steps listed next:
- Click the Project|appname Properties menu item.
- Click the Settings tab (see Figure 1).
- In the Name field type "MyConnectionString".
- In the Type column select "(Connection string)".
- In the Scope column, select Application.
- In the Value column click the button with the ellipses and use the Connection Properties dialog (see Figure 2) to define the connection string
Figure 1: Use the Settings tab to define a connection string.
Figure 2: The connections property dialog supports defining a connection string.