Msi installation database

Source: Internet
Author: User

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
  • Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.