How to attach to a database during installation
The following article uses options that are available starting with the Enterprise edition and project type.
To attach to a database during installation, you can proceed as follow:
1) In the Files and Folders page you can add the database file as a regular file.
2) The next step is to establish the connection the server. In the Dialogs page you can add the SQLServerConnectionDlg dialog from the build-in list of dialogs. On this dialog, the user can enter the server details. Also, this dialog adds predefined functionaility for free, such as: database browse functionality, testing the SQL connection, choosing the authentication method (SQL Authentication or Windows Authentication).
When adding this dialog, a connection to the server is created in the SQL Databases page. The SQL server browse and database browse operations are added for free.
3) Add the SQL Script to attach to the database. It can be something like this:
CREATE DATABASE MyAdventureWorks ON (FILENAME = 'myDatabaseFilePath'), (FILENAME = 'myDatabaseFilePath') FOR ATTACH; USE DATABASE MyAdventureWorks
The myDatabaseFilePath is a property that will be set with the path of the database file at install time.
For details on how to attach to a database using Transact-SQL, please take a look on the Using Transact-SQL article.
4) In the Script Replacement tab you can create a SQL replace operation to set the property with the path of the database file, as below: