Hive Studios d.o.o. is a technology company specialized in software development, IT training and IT consulting for Microsoft based technologies.

How To Work With Database Installer

Installation

Please run setup.exe file and follow on-screen instructions to install Database Installer component. If you wish to use evaluation version leave “License Key” field blank. You will be able to upgrade to licensed version later.

Using Database Installer in Visual Studio

In order to use Database Installer successfully, please follow the steps below:

  1. Add a reference to HiveStudios.DeploymentTools.DBInstaller.dll to your project or add the component to Visual Studio toolbox. You can do that if you right click on the toolbox and select Choose Items. When “Choose Toolbox Items” dialog appears, click Browse and navigate to installation folder. Select HiveStudios.DeploymentTools.DBInstaller.dll and click OK.
  2. Add new Installer component to your project and open it in design mode. Make sure it has RunInstallerAttribute applied on class declaration and set to true.
  3. Drag Database Installer from the toolbox to the designer.
  4. Select the component on the form and set its properties.

You can use drop down menu or designer verbs to

  • Upgrade from trial to full version
  • Get support on www.hive-studios.com
  • Bulk add script files to install or uninstall script lists

Properties

Member

Description

BatchSeparator

Gets or sets string used for separation of batches in script file. The most common value is GO

Context

Gets or sets information about the current installation. The most common usage is in unit testing of installer components. (Inherited from Installer.)

DatabaseScriptsFolderName

Gets or sets the name of the database scripts folder. All Sql Scripts used by this istance of Database Installer must reside in this folder in order to be discoverable by installer utility in runtime.

DeleteScriptsOnSuccess

Gets or sets if installer should delete installation script files from file system after installation is completed

InitialCatalog

Gets or sets initial catalog for connection. Default value is 'master'

Installers

Gets the collection of installers that this installer contains. If you want to chain database installers you can use this collection to add them in particular order

(Inherited from Installer.)

InstallScriptList

Holds InstallScript list with locations of database script files used in install phase.

Parent

Gets or sets the installer containing the collection that this installer belongs to.

(Inherited from Installer.)

Password

Gets or sets password for Database Login. You can set password in design time if know which SQL Login will be used by installer. The more common option is to set it using Custom Action parameters in installation time.

SecurityOption

Gets or sets authentication method for SqlConnection. You can use both SQL Authentication and Windows Integrated

ServerName

Gets or sets FQDN, NetBIOS or IP address of Database Server. Default value is 'localhost'

UninstallScriptList

Holds InstallScript list with locations of database script files

UserID

Gets or sets User ID for Database Login. You can set it in design time if know which SQL Login will be used by installer. The more common option is to set it using Custom Action parameters in installation time. o:p>

Using Database Installer in Code

using System;

using System.Collections;

using System.Collections.Generic;

using System.ComponentModel;

using System.Configuration.Install;

using System.Linq;

using HiveStudios.DeploymentTools.Installers;

 

 

namespace MyClasslibrary

{

 

    /// <summary>

    /// This is the root installer of your project.

    /// </summary>

    [RunInstaller(true)]

    public partial class MainInstaller : Installer

    {

 

        /// <summary>

        /// Initializes a new instance of the <see cref="MainInstaller"/> class.

        /// </summary>

        public MainInstaller()

        {

 

            DatabaseInstaller databaseInstaller = new DatabaseInstaller();

 

            databaseInstaller.BatchSeparator = "GO";

            databaseInstaller.DatabaseScriptsFolderName = "MyScripts";

            databaseInstaller.SecurityOption = SqlServerSecurityOption.Integrated;

 

            //This scripts will be executed during installation

            databaseInstaller.InstallScriptList.Add(new InstallScript("CreateDatabase.sql"));

            databaseInstaller.InstallScriptList.Add(new InstallScript("CreateTables.sql"));

            databaseInstaller.InstallScriptList.Add(new InstallScript("CreateStoredProcedures.sql"));

 

            //This script will be executed during uninstallation

            databaseInstaller.UninstallScriptList.Add(new InstallScript("DropDatabase.sql"));

 

            //Add to parent installer

            this.Installers.Add(databaseInstaller);

 

 

        }

    }

}

 

Configuring Custom Actions and Passing Parameters to Installer

Database Installer supports four properties that can be set by installer during installation or uninstallation of your package

Custom action parameter

Description

 

SERVER

Database server name. Corresponds to ServerName property.

UID

User ID used to access Sql Server using Sql autentification. Corresponds to UserID property.

PWD

Password used to access Sql Server using Sql autentification. Corresponds to Password property.

INTEGRATEDSECURITY

Allowed values are true or false. If you set this option to true UID and PSD will be ignored and Integrated security will be used to connect to Sql Server. Corresponds to SecurityOption property. If you want to use Sql Authentication omit this parameter.

Using installutil.exe

  1. Run Visual Studio Command Promt
  2. Navigate to the folder that contains your binaries
  3. Run installutil.exe

Installutil YourBinaries.dll /SERVER=myservername /UID=habahaba /PWD=s3cr3tpazz

If you want to use Windows Integrated security to authenticate agains SQL Server use INTEGRATEDSECURITY parameter

Installutil YourBinaries.exe /SERVER=myservername /INTEGRATEDSECURITY=true

Using Windows Installer

Please see detailed walktrough on MSDN (http://msdn2.microsoft.com/en-us/library/9cdb5eda(VS.80).aspx)

Unit Testing Database Installer

Unit testing is a procedure used to validate that individual units of source code are working properly. Unit testing installers might be tricky. The basic approach is to simulate activity of Windows Installer and Installutil in your code.

  1. Add unit test project to your application if you do not have one.
  2. Add reference to project that contains code you want to test. Add reference to System.Configuration.Installer .NET library
  3. Add folder(s) to the test project  and fill it with your scripts
  4. Open LocalTestRun.testrunconfig, go to Deply Items tab and add scripts to the deployments. It is important to note that MSTest will copy these files to the specific location where all test are executed.
  5. LocalTestRun.testrunconfig file using XML or text editor and add outputDirectory to each DeploymentItem that represent your scripts

    <DeploymentItem filename="TestProject1\Scripts\DropDatabase.sql" outputDirectory="Scripts"  />


  6. Create new TestMethod that will test your installation code
  7. In the Test method, create one instance of the installer you want to test, one instance of InstallContext and one collection that implements ICollection interface.

    DatabaseInstaller parent = new DatabaseInstaller();

    InstallContext context = new InstallContext();

    IDictionary state = new Hashtable();

  8. Add parameters to the context installer

    context.Parameters.Add("SERVER", Environment.MachineName);

    context.Parameters.Add("INTEGRATEDSECURITY", "true");

  9. Fill InstallScriptList and UnInstallScripts, set any other properties and run installer

    parent.InstallScriptList.Add(new InstallScript(@"CreateDatabase.sql"));

    parent.InstallScriptList.Add(new InstallScript(@"CreateDatabaseObjects.sql"));

    parent.UninstallScriptList.Add(new InstallScript(@"DropDatabase.sql"));

     

    parent.Install(state);

    parent.Commit(state);

    parent.Uninstall(state);


  10. You can add additional code after calls to Install(), Commit() and Uninstall() to check are scripts performs as expected
  11. On the end, test method should look similar to this:

    [TestMethod]

    public void TestMethod1()

    {

     

        MainInstaller installer = new MainInstaller();

        InstallContext context = new InstallContext();

        IDictionary state = new Hashtable();

     

     

        context.Parameters.Add("SERVER", Environment.MachineName);

        context.Parameters.Add("INTEGRATEDSECURITY", "true");

     

        try

        {

            parent.InstallScriptList.Add(new InstallScript(@"CreateDatabase.sql"));

            parent.InstallScriptList.Add(new InstallScript(@"CreateDatabaseObjects.sql"));

    newue;">new InstallScript(@"DropDatabase.sql"));

            parent.Install(state);

            parent.Commit(state);

            parent.Uninstall(state);

     

        }

        catch (InstallException ex)

        {

            parent.Rollback(state);

            Assert.Fail(ex.ToString());

        }

        catch (Exception ex)

        {

            parent.Rollback(state);

            Assert.Fail(ex.ToString());

        }

     

    }

AddThis Social Bookmark Button
Services | Case Studies | Products | About Us | News | Privacy | Downloads | Copyright
Copyright © HiveStudios 2003-2009.