SIGN UP MEMBER LOGIN:    
ARTICLE

Database Installer For Production

Posted by Shantanu Articles | ADO.NET in C# July 15, 2011
This article explains how to install a database to production and build a tool to do so.
Reader Level:
Download Files:
 


Objective

Many times it is necessary to install a database to a production machine which is not accessible from the development environment.

This article explains how to install a database to production and build a tool do so.

Free source code of the tool is available for download.

Note : If your zip archiver is unable to open the download, please use 7 Zip (free) to do so.

Implementation

The tool I have made uses VSDBCMD.exe for actually deploying the database from a model of the database.

The first step is to create a Database Project in Visual Studio for the database. This project should contain all the SQL for the tables, stored procedures, post-deployment script etc.

Then build the database project. This creates model files in the sql\release folder of the project. The following image shows the files that are created by the build.

Build files
The tool (following image) uses these model files to install the database.

Model file


Use the browse button to get the path to the model files.

The connection string contains the Data Source, User ID and Password (or Integrated Security, if the database can be accessed by the logged in user account).

The database name is the name of the database created from the model.

After entering this information, when you click install, VSDBCMD.exe is invoked in a Process. This installs the database on the server.

    Process installProcess = new Process
    {
        StartInfo = new ProcessStartInfo
        {
            CreateNoWindow = true,
            WindowStyle = ProcessWindowStyle.Hidden,
            FileName = "VSDBCMD.exe",
            Arguments = @"/a:Deploy /dd /cs:""" + ConnStr + @""" /dsp:Sql /model:" + modelName + @".dbschema /p:TargetDatabase=" + DbName + @" /manifest:" + modelName +
@".deploymanifest"
        }
    };
    installProcess.Start();
    installProcess.WaitForExit();


For uninstalling (deleting) the database, just enter the connection string and the name of the database.

This drops the database.

    SqlConnection conn = new SqlConnection(ConnStr);
    SqlCommand command = new SqlCommand("DROP DATABASE " + DbName, conn);
    conn.Open();
    command.ExecuteNonQuery(); 
    conn.Close();


You will see the installed database on the server (as shown below).

sample database

So, you can copy the tool and the output of the database project (model) to the production machine and install the database using the tool.

There is also a Setup project which produces a Setup.exe. This can be used to install the tool on a computer.

Note : The tool needs .Net 4.0 Runtime to run. It can install models created in VS 2008 and VS 2010 database projects for SQL Server 2005/2008.

Login to add your contents and source code to this article
share this article :
post comment
 
Team Foundation Server Hosting
Become a Sponsor
PREMIUM SPONSORS
  • Finally – a virtual platform that delivers next-generation Windows Server 2008 Hyper-V virtualization technology from a managed hosting partner you can truly depend on. Visit www.maximumasp.com/max for a FREE 30 day trial. Hurry offer ends soon. Climb aboard the MaxV platform and take advantage of High Availability, Intelligent Monitoring, Recurrent Backups, and Scalability – with no hassle or hidden fees. As a managed hosting partner focused solely on Microsoft technologies since 2000, MaximumASP is uniquely qualified to provide the superior support that our business is built on. Unparalleled expertise with Microsoft technologies lead to working directly with Microsoft as first to offer IIS 7 and SQL 2008 betas in a hosted environment; partnering in the Go Live Program for Hyper-V; and product co-launches built on WS 2008 with Hyper-V technology.
    Get 2 Months Free of ASP.NET Hosting for Only $4.95/month! Receive FREE MS SQL and MySQL Databases Including ASP.NET 4/3.5, MVC 3.0, Silverlight 4, Windows 2008/IIS 7.0 Plus FREE IIS 7 Modules. Host UNLIMITED ASP.NET Web Sites - Click Here!
Nevron Gauge for SharePoint
Become a Sponsor