Click here to Skip to main content
Click here to Skip to main content

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

By , 10 Jan 2006
Rate this:
Please Sign up or sign in to vote.

Introduction

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.

Background

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(), "[" + 
    Application.StartupPath+@"\Organization_Data.mdf]");

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).

License

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

*****

Comments and Discussions

 
Questionalternative for sqldmo [modified] PinmemberPallavi_cis17-Dec-12 23:24 
QuestionHow to Do this with Oracle Database PinmemberPraveen212122-Nov-12 21:49 
GeneralMy vote of 1 PinmemberM.Ostadi5-May-12 18:57 
Questiondb Instal and uninstal' Pinmemberthawpeek14-Jul-11 15:06 
QuestionWhile Uninstalaation Pinmemberthawpeek14-Jul-11 15:00 
GeneralGood article PinmemberDonsw19-Dec-08 10:19 
GeneralNeed help on this article Pinmemberhighjo12-Dec-07 12:35 
GeneralAdding/Attach new user to sql server Pinmemberyachitha23-Apr-07 1:46 
GeneralSQL 2000 PinmemberNanaAM28-Feb-07 22:49 
GeneralError regarding the Exe Pinmemberjaganjay27-Feb-07 0:16 
GeneralNo offense ... PinmemberFrederick White27-Oct-06 19:33 
GeneralDeployment with Custom action and exe PinmemberBalaNet2-Jul-06 0:14 
General123 PinmemberThe_Myth21-Mar-06 22:43 
GeneralCancel the install process Pinmemberpauloafc28-Nov-05 6:48 
GeneralRe: Cancel the install process PinmemberBalaNet2-Jul-06 0:24 
QuestionUrgent - To include database in setupproject in asp.net PinmemberCardiana26-Nov-05 4:48 
AnswerRe: Urgent - To include database in setupproject in asp.net Pinmemberpauloafc28-Nov-05 6:57 
GeneralCaution PinmemberFrank Samjeske31-Aug-05 6:03 
GeneralRe: Caution PinmemberMukund Pujari3-Sep-05 4:31 
GeneralRe: Caution PinmemberFrank Samjeske5-Sep-05 0:56 
GeneralSQLDMO Question PinmemberPaul Brower31-Aug-05 3:32 
GeneralRe: SQLDMO Question PinmemberMukund Pujari2-Sep-05 20:45 
GeneralRe: SQLDMO Question PinmemberPaul Brower3-Sep-05 3:59 
GeneralRe: SQLDMO Question PinmemberMukund Pujari3-Sep-05 4:15 
GeneralRe: SQLDMO Question Pinmembermharr27-Oct-05 9:42 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    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 | Mobile
Web01 | 2.8.140415.2 | Last Updated 11 Jan 2006
Article Copyright 2005 by Mukund Pujari
Everything else Copyright © CodeProject, 1999-2014
Terms of Use
Layout: fixed | fluid