Click here to Skip to main content
11,932,103 members (55,673 online)
Click here to Skip to main content
Add your own
alternative version


132 bookmarked

Create database during installation of a .NET application - Version 2

, 10 Jan 2006
Rate this:
Please Sign up or sign in to vote.
This application can be used to create database, tables, and stored procedures during the installation of a .NET application.


Thanks for the good responses to my previous article. Though the method suggested in Version 1 of this article is useful for creating databases during installation, it proves tedious if there is a large number of tables with many columns, complex stored procedures, triggers, relationships, and data too. Here, I am going to explain to you a very simple method to achieve this. Believe me, you require less than a line of code to achieve this.


My client used to test the application with test data. Previously, he had to create a database and enter data every time I delivered him a module, even when there was only a small correction. So I developed this solution which installs a database with data in it.

Here too, we are going to use SQL-DMO, but we are not going to create any database, tables, or stored procedures. Rather, we are going to just attach the database files to the target server, but programmatically. So our task and time for writing the code for creating a database and its objects will be saved.

As all of us know, whenever we create a database on SQL Server, two files are created on the server viz., Data File (DBName_Data.mdf) and Log File (DBName_Log.ldf). If we want to move these files on to another server, just copying these files to the other computer will not work. To make this work, we have to attach these files to the server. (In your SQL Server Enterprise Manager, select Server, select Databases, right click on it, select All Tasks, and then select Attach Database…).

Similarly, to copy these files, either you have to stop the server, or detach the particular database from the server. (In your SQL Server Enterprise Manager, select Server, select Databases, select the particular database and right click on it, select All Tasks, and then select Detach Database…).

Using the Demo Application

The demo application is provided with the required tool tips which will help you to use the application.

Using the Source Code

Much of the code is same as in Version 1, so I will not be repeating it here. You can just ignore the code for creating the database, tables and stored procedures.

Attaching the Data Files to the Server

Use the code given below in the Install button's Click event:

/* The AttachDB method of server attaches your .mdf
 file to server  The syntax of AttachDB method is 
AttachDB(string DBName,string datafiles)where 'DBName'
 is name of your database and 'datafile' is nothing but
 your .mdf file including full path. You should be
 careful while mentioning the datafiles because SQL-DMO
 takes this parameter as multistring therefore if your file
 name has space then it will not read the file name.
 Hence put your file name in Square Brackets '[]' as below. */

srv.AttachDB(txtDBName.Text.Trim(), "[" + 

How to Use This Application in Setup Projects

  1. Don’t forget to copy your data files in your package. Copy them into your application folder. Follow the steps given below.
  2. Select the Setup project in the Solution Explorer. Right click on it.
  3. Select View--File System Editor.
  4. In File System Editor, select the application folder and right click on it.
  5. Then, select Add--File.
  6. Now, select your data files, and click OK.
  7. Rest of the steps to add your application to the setup project, using Custom Actions, are the same as in (Version 1).

Other Considerations

It is possible that the above code might give an error saying "QueryInterface for interface SQLDMO.NameList failed". If such exceptions occur, it means the server should be updated with the latest service pack (above SP 2).


This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here


About the Author


You may also be interested in...

Comments and Discussions

Questionalternative for sqldmo Pin
Pallavi_cis18-Dec-12 0:24
memberPallavi_cis18-Dec-12 0:24 
QuestionHow to Do this with Oracle Database Pin
Praveen212122-Nov-12 22:49
memberPraveen212122-Nov-12 22:49 
GeneralMy vote of 1 Pin
M.Ostadi5-May-12 19:57
memberM.Ostadi5-May-12 19:57 
Questiondb Instal and uninstal' Pin
thawpeek14-Jul-11 16:06
memberthawpeek14-Jul-11 16:06 
QuestionWhile Uninstalaation Pin
thawpeek14-Jul-11 16:00
memberthawpeek14-Jul-11 16:00 
GeneralGood article Pin
Donsw19-Dec-08 11:19
memberDonsw19-Dec-08 11:19 
GeneralNeed help on this article Pin
highjo12-Dec-07 13:35
memberhighjo12-Dec-07 13:35 
GeneralAdding/Attach new user to sql server Pin
yachitha23-Apr-07 2:46
memberyachitha23-Apr-07 2:46 
GeneralSQL 2000 Pin
NanaAM28-Feb-07 23:49
memberNanaAM28-Feb-07 23:49 
GeneralError regarding the Exe Pin
jaganjay27-Feb-07 1:16
memberjaganjay27-Feb-07 1:16 
GeneralNo offense ... Pin
Frederick White27-Oct-06 20:33
memberFrederick White27-Oct-06 20:33 
GeneralDeployment with Custom action and exe Pin
BalaNet2-Jul-06 1:14
memberBalaNet2-Jul-06 1:14 
General123 Pin
The_Myth21-Mar-06 23:43
memberThe_Myth21-Mar-06 23:43 
GeneralCancel the install process Pin
pauloafc28-Nov-05 7:48
memberpauloafc28-Nov-05 7:48 
GeneralRe: Cancel the install process Pin
BalaNet2-Jul-06 1:24
memberBalaNet2-Jul-06 1:24 
QuestionUrgent - To include database in setupproject in Pin
Cardiana26-Nov-05 5:48
memberCardiana26-Nov-05 5:48 
AnswerRe: Urgent - To include database in setupproject in Pin
pauloafc28-Nov-05 7:57
memberpauloafc28-Nov-05 7:57 
GeneralCaution Pin
Frank Samjeske31-Aug-05 7:03
memberFrank Samjeske31-Aug-05 7:03 
GeneralRe: Caution Pin
Mukund Pujari3-Sep-05 5:31
memberMukund Pujari3-Sep-05 5:31 
GeneralRe: Caution Pin
Frank Samjeske5-Sep-05 1:56
memberFrank Samjeske5-Sep-05 1:56 
GeneralSQLDMO Question Pin
Paul Brower31-Aug-05 4:32
memberPaul Brower31-Aug-05 4:32 
GeneralRe: SQLDMO Question Pin
Mukund Pujari2-Sep-05 21:45
memberMukund Pujari2-Sep-05 21:45 
GeneralRe: SQLDMO Question Pin
Paul Brower3-Sep-05 4:59
memberPaul Brower3-Sep-05 4:59 
GeneralRe: SQLDMO Question Pin
Mukund Pujari3-Sep-05 5:15
memberMukund Pujari3-Sep-05 5:15 
GeneralRe: SQLDMO Question Pin
mharr27-Oct-05 10:42
membermharr27-Oct-05 10:42 

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
Web04 | 2.8.151126.1 | Last Updated 11 Jan 2006
Article Copyright 2005 by Mukund Pujari
Everything else Copyright © CodeProject, 1999-2015
Layout: fixed | fluid