Keyword MSI installation database original author's name Li
Introduction This article mainly shows how to automatically install the database automatically by adding custom actions.
The installation of the body database (as an example of SQL Server) typically has the following ways: Recover the database through the database backup, perform scripting installations by attaching the database file. In these ways, the first two ways typically require manual operation of the operator to add a database to the database system. This improves the requirements for software users and also has account privileges (only a series of accounts such as database administrators "have high privileges that have recovery, additional database capabilities). Therefore, install the database installation script installation database is a relatively flexible choice. In Visual Studio.NET, you provide us with an item template for installing and deploying applications through the Setup Project project template, you can create an MSI installation package. To make the MSI installation package with the function of installing the database, only using custom movements are completed. The steps are as follows: First, to create a class library. What is created to create an assembly that allows the MSI installation package. The class in this assembly needs to be derived from System.Configuration.install.Installer. This Installer class is the base class for all custom action classes. Second, write a database installation function in this project. Its main principle is to use the console tool of SQL Server Osql.exe. Trust to log in through the -e parameter, let Osql.exe will perform a database scripted file through the -i parameter. Since the location of the installation package is unqualified (the user can put the MSI file in any position of the hard disk). So, you should use the .NET reflection to temporarily get the location of the current installation package. The specific code is as follows: // Get the currently running assembly instance system.reflection.Assembly assembly = system.reflection.Assembly.GetexecutingAssembly (); // Get the path of the current assembly string path = askEMBLOCATION; // remove this The file name of the assembly path = path.replace ("installdb.dll", ""); // plus the file name of the script file string SQLPATH = PATH "DEMOSQL.SQL"; // Create a database installation process System.Diagnostics .Process p = new process (); // Setting process parameter P.StartInfo = New processstartInfo ("osql.exe", "-e -i" sqlpath); // Open process P.Start ();
annex:
Example engineering setupdemo.rar