Introduction
This topic describes how to add a custom action to allow the MSI installation package to automatically install the database.
Body
Database (taking SQL Server as an example) Installation usually involves the following methods: recover the database through database backup, attach database files, and execute scripts for installation.
In these methods, the first two methods usually require manual operations by the operator to add a database to the database system. In this way, the requirements for software users and account permissions are improved (only a series of accounts with high permissions such as database administrators can have the ability to recover and attach databases ). Therefore, it is flexible to install the database by executing the database installation script.
Visual Studio. NET provides us with the installation and deployment of applications.ProgramYou can use the setup Project template to create an MSI installation package. To enable the MSI installation package to have the function of installing the database, you only need to use custom actions.
The procedure is as follows:
1. Create a class library project ). In order to create an assembly that can be called by the MSI installation package. Classes in this Assembly need to be derived from system. configuration. Install. installer. This installer class is the base class of all Custom Handler classes.
2. compile a database installation function in this project. The main principle is to use the SQL server console tool osql.exe. You can use the -e.exe method to gain trust and allow osql.exe to execute a database script file. The installation package is not in the correct location (you can place the MSI file in any location on the hard disk ). Therefore, you must use. Net reflection to temporarily obtain the location of the current installation package. DetailsCodeAs follows:
// Obtain the currently running Assembly instance
System. reflection. Assembly = system. reflection. Assembly. getexecutingassembly ();
// Obtain the path of the current Assembly
String Path = assembly. location;
// Remove the Assembly file name
Path = path. Replace ("installdb. dll ","");
// Add the script file name
String sqlpath = path + "demosql. SQL ";
// Create a database installation process
System. Diagnostics. PROCESS p = new process ();
// Set Process Parameters
P. startinfo = new processstartinfo ("osql.exe", "-e-I" + sqlpath );
// Open the process
P. Start ();
Text complete
Attachment:
Sample project setupdemo.rar