Implementing a Custom ConnectionString Installer for Setup, Page 2
Reading the Connection String from MySettings
When you set a connection string in the Properties|Settings tab, you can read this value with My.MySettings.Default.settingsname. In the example, the settingsname part is the value provided, MyConnectionString. (Refer to Listing 1 for an example that demonstrates using the My feature and accessing connection strings.)
Connecting to a Database
The using statement is syntactic sugar for the try finally block. (Sugar in moderation is good.) The using clause is designed to invoke Dispose on classes that implement IDisposable. In the case of the SqlConnection class, the Dispose method closes an open connection. Refer to Listing 1 for an example of the using statement.
Implementing a Custom Action
A custom action is a class that inherits from the System.Configuration.Install.Installer class. Because an installer is just a class, you can define any behaviour your solution requires. In the example, you will run the Data Links dialog to help configure the connection string. You'll remove the provider clause from the connection string, and you'll use .NET's built-in support for encrypting a config section.
Creating an Installer Project
An installer is a template in Visual Studio. To create the installer, add a Class Library project to the solution. From the Project|Add New Item, select the Installer Class (see Figure 2). The installer is a component that includes a partial class. The generated partial class—in filename.designer.vb—contains the RunInstaller(True) attribute and the Inherits from System.Configuration.Install.Installer statement. All you have to do is override the Install method and add the desired behavior.
Figure 2: The Add New Item dialog includes an Installer template.
Before you begin adding the installer code, add a reference to Microsoft ActiveX Data Objects and Microsoft OLEDB Service Components. (Use Figures 3 and 4 to guide you here.) These references support displaying and using the Data Link Properties dialog in Figure 5. The code for the installer is shown in Listing 2.
Figure 3: Adding a reference to ADO.
Figure 4: Adding a reference to the OLE DB Service Components, which provides access to the Data Link Properties dialog (shown in Figure 5).