Click here to Skip to main content
12,547,501 members (67,267 online)
Click here to Skip to main content
Add your own
alternative version


41 bookmarked

Restore Database during Application Deployment

, 15 Oct 2008 CPOL
Rate this:
Please Sign up or sign in to vote.
Restore database during application deployment using Windows Installer and Visual Studio Setup Project


In applications developed in .NET for data storage is often used MS SQL Server. During distribution of the application, one of the frequently asked questions is Database installation method on the Target Machine (the PC or workstation your project will be installed on).

The method described in this paper allows execute operation Restore Database at installation stage of application.

Application and Database Development

Start with creating an application that works with database.
Create a sample database with name “SampleDatabase”.
Create a sample application that connects to the created database and retrieves data.
(“SampleApplication” is in the file attached to the paper.)

Create Installer

Now let’s create an installer.

  1. First of all, we need to create a Backup for the Database in “SQL Server Management Studio Express” by selecting required base and clicking “Back Up…”


    1. Then select path and backup copy filename. In this case it is “SampleDatabase.bak


  2. Next we create Custom Action.
    1. Although standard actions are sufficient to execute an installation in most cases, custom actions enable the author of an installation package to extend the capabilities of standard actions by including executables, dynamic-link libraries, and script.
    2. Create a new project using the Class Library template, then in the Name box, type “SampleInstallLib”, in the Solution Name box, type “SampleInstallApp”, click OK.


      The project is added to Solution Explorer.

    3. On the Project menu, choose Add Class, and then in the Add New Item dialog box, select Installer Class. Accept the default name of Installer1.cs. Click Add.


    4. Delete the default Class1.cs object from the SampleInstallLib project by expanding the Solution Explorer, right click on the Class1.cs object and select the Delete option.
    5. Then open Installer1.cs for editing.

      Adding references to:


      You will need the following namespaces for the code above to work:

      using System.Data.SqlClient;
      using System.IO;
      using System.Security.AccessControl;
      using System.Windows.Forms;
      using Microsoft.SqlServer.Management.Common;
      using Microsoft.SqlServer.Management.Smo;

      Add the next code:

      public void RestoreDatabase(String databaseName, String filePath, 
      	String serverName, String userName, String password,
      String dataFilePath, String logFilePath)
          Restore sqlRestore = new Restore();
          BackupDeviceItem deviceItem = new BackupDeviceItem
      			(filePath, DeviceType.File);
          sqlRestore.Database = databaseName;
          ServerConnection connection;
          // for Windows Authentication
          if(userName == "")
              SqlConnection sqlCon = new SqlConnection
      	(@"Data Source="+serverName+@"; Integrated Security=True;");
              connection = new ServerConnection(sqlCon);    
          // for Server Authentication
          connection = new ServerConnection(serverName, userName, password);
          Server sqlServer = new Server(connection);
          Database db = sqlServer.Databases[databaseName];
          sqlRestore.Action = RestoreActionType.Database;
          String dataFileLocation = dataFilePath + databaseName + ".mdf";
          String logFileLocation = logFilePath + databaseName + "_Log.ldf";
          db = sqlServer.Databases[databaseName];
          RelocateFile rf = new RelocateFile(databaseName, dataFileLocation);
          sqlRestore.RelocateFiles.Add(new RelocateFile
      			(databaseName, dataFileLocation));
          sqlRestore.RelocateFiles.Add(new RelocateFile
      		(databaseName + "_log", logFileLocation));
          sqlRestore.ReplaceDatabase = true;
          sqlRestore.Complete += new ServerMessageEventHandler(sqlRestore_Complete);
          sqlRestore.PercentCompleteNotification = 10;
          sqlRestore.PercentComplete += new PercentCompleteEventHandler
          catch (Exception ex)
          db = sqlServer.Databases[databaseName];

      Function RestoreDatabase is an open connection to a SQL Server and restores database from backup.

      As a parameter function gets:

      • databaseName – Name of database for which restore operations will be executed
      • filePath – a pass to a file
      • serverName – name of server
      • userName – name of user
      • password – user’s password
      • dataFilePath – a path that database file will have
      • logFilePath – a path that log file will have

      In case Windows Authentication is needed instead of user name, a blank line should be input, a password should be ignored in this case.

      Add next code for the function Commit:

      public override void Commit(System.Collections.IDictionary savedState)
          // The code below changes the TARGETDIR permission 
          // for a Windows Services running under the 
          // NT AUTHORITY\NETWORK SERVICE account.
              DirectorySecurity dirSec = Directory.GetAccessControl
              FileSystemAccessRule fsar = new FileSystemAccessRule
                                            , FileSystemRights.FullControl
                                            , InheritanceFlags.ContainerInherit | 
                                            , PropagationFlags.None
                                            , AccessControlType.Allow);
              Directory.SetAccessControl(Context.Parameters["TargetDir"], dirSec);
          catch (Exception ex)
      Context.Parameters["password"].ToString(), Context.Parameters

      This function code determines full access to directory setup (“TargetDir”) and all attached folders for user group “NT AUTHORITY\NETWORK SERVICE”. This will allow SQL Server to run Restore for our Database. In case of “NT AUTHORITY\NETWORK SERVICE” access is denied while decompression database the function SqlRestore cause the next exception:

      Restored failed for server '.\sqlexpress' (Microsoft.SqlServer.Express.Smo)
      Additional Information:
      System.Data.SqlClient.SqlError: The operating system returned the error '5
      	(Access is denied)' while attempting
      'RestoreContainer::ValidateTargetForCreation' on '<path />\SampleDatabase_Log.ldf' 
    6. Build SampleInstallLib.
  3. Start with Setup Project creation.
    1. In SampleInstallApp execute File -> Add -> New Project
      Select Other Projects -> Setup and Deployment, using the Setup Project template.
    2. Set application name “SampleInstall” and click Ok.


    3. In Solution Explorer select newly created project, right click and select Properties. In appeared window click Prerequisites. In Prerequisites window, select checks at Windows Installer 3.1 and SQL Server 2005 Express Edition, and fix “Download prerequisites from the same location as my application” position. This will allow enable Windows Installer and SQL Server in installation pack, if there are no applications on target machine it will install them and then our application will be installed.
    4. In Solution Explorer, click “File system editor”. Appeared folder displays file system of the target machine. Let’s make copies of our project file “SampleApplication”, library file “SampleInstallLib.dll”, and backup of our database SampleDatabase.bak. Essential assembly will be automatically included in our project. Now create folder “Database”, the files of our database will be unpacked in it. To create the folder, right click “Application folder” and select Add-> Folder, name it. In the window properties, select AlwaysCreate in true.


    5. Next create Project output. For that, choose Application Folder and right click select Add-> Project Output. In the window, Add Project Output Group select in Project: SampleInstallLib, then select Primary output and click OK.


      Created Primary output will appear in file list.

    6. Then press a button in Solution Explorer and open Custom Actions Editor. And for all four actions, choose Primary output from SamplesInstallLib(Active). For that, right click for the necessary Action and select Add Custom Action.


      As a result, we get the following:


      The most recent move we pass parameter/argument list for action Commit. At installation stage, the variable IDictionary savedState (Commit function) gets transmission parameters at the moment of activation event Commit.

      public override void Commit(System.Collections.IDictionary savedState) 

      The parameters are shown in CustomActionData property. Parameters transfer syntax is paramName=”value”.

      For our task, the line is:

      /TargetDir="[TARGETDIR]\" /databaseName="SampleDatabase" 
      /filePath="[TARGETDIR]SampleDatabase.bak" /serverName=".\SQLEXPRESS"
       /userName="" /password="" /dataFilePath="[TARGETDIR]Database\\" 
    7. Build the project SampleInstall, now we can execute installation, right click the project in Solution Explorer and select Install. Windows Installer will copy files in the specified folder. If it is copied successfully, the full access will be given to user’s group “NT AUTHORITY\NETWORK SERVICE” to installation directory and database will be unpacked in the Database directory.



When you create your own installation packet, you can ignore step 2 and use Custom action instead that is in the archive attached with this article.


  • 15th October, 2008: Initial post


This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


About the Author

United States United States
No Biography provided

You may also be interested in...


Comments and Discussions

QuestionBackUP Database during Application Deployment Pin
JamesB2816-Jan-13 14:31
memberJamesB2816-Jan-13 14:31 
Hi FreeDev ,, Thanks for This Excellent Article , I have a big problem with the same case,, I'm developing an application with in witch I use one Database and then I will deploy it later . My Client would like save many copies of this database whenever he updates it , SO the question is : how to do it in the same philosophy u exposed the RESTORE task of the database ?. I have many questions in this field ( In the Ado.NET we deal only with one database ,, even in the connectionstring we should mention the name of the database , SO I need some code or ideas at least for the whole task of dealing with database files : creating new empty databases with the same structure that my application is designed for SO maybe many users will use this application then each one will create his 'session' or his ' folder ' and then exploit his own database witch is similar in term of STRUCTURE ( a copy of the structure not of the data inserted in tables ). In fact I already had an Answer ; its using : Server Management Objects SMO From 'David' But I still have ambiguities SO I hope its clear , else I will express my self more enough to get what I'm talking about .. thanks a lot!
Questionerror Pin
Sinosar30-Oct-12 20:39
memberSinosar30-Oct-12 20:39 
Questiongreat tutorial Pin
sersheed17-Jul-12 1:40
membersersheed17-Jul-12 1:40 
GeneralIt's a nice tutorial. Pin
Tae-Sung27-Nov-09 12:17
memberTae-Sung27-Nov-09 12:17 
GeneralError-please help me Pin
araavind0071-Nov-09 23:09
memberaraavind0071-Nov-09 23:09 
GeneralRe: Error-please help me Pin
Tae-Sung27-Nov-09 11:55
memberTae-Sung27-Nov-09 11:55 
GeneralError Pin
araavind0071-Nov-09 23:01
memberaraavind0071-Nov-09 23:01 
GeneralRe: Error Pin
suren841729-Jun-10 23:59
membersuren841729-Jun-10 23:59 
Questionquestion.. can you help me?? Pin
Member 472692925-Sep-09 4:35
memberMember 472692925-Sep-09 4:35 
AnswerRe: question.. can you help me?? Pin
Tae-Sung27-Nov-09 11:59
memberTae-Sung27-Nov-09 11:59 
QuestionHave a Prolem Pin
ngo kim oanh2-Jun-09 15:43
memberngo kim oanh2-Jun-09 15:43 
AnswerRe: Have a Prolem Pin
FREEDEV30-Jun-09 21:51
memberFREEDEV30-Jun-09 21:51 
GeneralError [modified] Pin
asder437-Mar-09 2:07
memberasder437-Mar-09 2:07 
GeneralRe: Error Pin
asder437-Mar-09 2:58
memberasder437-Mar-09 2:58 
GeneralRe: Error Pin
FREEDEV2-Apr-09 5:17
memberFREEDEV2-Apr-09 5:17 
GeneralRe: Error Pin
777vladislav7779-Jun-11 4:38
member777vladislav7779-Jun-11 4:38 
GeneralThanks Pin
narendra_thadani23-Feb-09 10:10
membernarendra_thadani23-Feb-09 10:10 
Questionhi. I have a prolem. Pin
TolgaKaradayi27-Oct-08 3:21
memberTolgaKaradayi27-Oct-08 3:21 
AnswerRe: hi. I have a prolem. Pin
FREEDEV5-Nov-08 22:33
memberFREEDEV5-Nov-08 22:33 
Generalvery good! Pin
9701lb20-Oct-08 21:29
member9701lb20-Oct-08 21:29 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

| Advertise | Privacy | Terms of Use | Mobile
Web01 | 2.8.161018.1 | Last Updated 15 Oct 2008
Article Copyright 2008 by FREEDEV
Everything else Copyright © CodeProject, 1999-2016
Layout: fixed | fluid