Click here to Skip to main content
15,881,757 members
Articles / Database Development / SQL Server
Article

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

Rate me:
Please Sign up or sign in to vote.
4.02/5 (31 votes)
10 Jan 20063 min read 179.3K   10.6K   133   26
This application can be used to create database, tables, and stored procedures during the installation of a .NET application.

Image 1

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:

C#
/* 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


Written By
India India
*****

Comments and Discussions

 
Questionalternative for sqldmo Pin
Pallavi_cis17-Dec-12 23:24
Pallavi_cis17-Dec-12 23:24 
QuestionHow to Do this with Oracle Database Pin
Praveen212122-Nov-12 21:49
Praveen212122-Nov-12 21:49 
GeneralMy vote of 1 Pin
M.Ostadi5-May-12 18:57
M.Ostadi5-May-12 18:57 
Questiondb Instal and uninstal' Pin
thawpeek14-Jul-11 15:06
thawpeek14-Jul-11 15:06 
QuestionWhile Uninstalaation Pin
thawpeek14-Jul-11 15:00
thawpeek14-Jul-11 15:00 
GeneralGood article Pin
Donsw19-Dec-08 10:19
Donsw19-Dec-08 10:19 
GeneralNeed help on this article Pin
highjo12-Dec-07 12:35
highjo12-Dec-07 12:35 
GeneralAdding/Attach new user to sql server Pin
yachitha23-Apr-07 1:46
yachitha23-Apr-07 1:46 
GeneralSQL 2000 Pin
NanaAM28-Feb-07 22:49
NanaAM28-Feb-07 22:49 
Does this work on SQL server 2000? or it needs only 2005?

Nana

GeneralError regarding the Exe Pin
jaganjay27-Feb-07 0:16
jaganjay27-Feb-07 0:16 
GeneralNo offense ... Pin
Frederick White27-Oct-06 19:33
Frederick White27-Oct-06 19:33 
GeneralDeployment with Custom action and exe Pin
BalaNet2-Jul-06 0:14
BalaNet2-Jul-06 0:14 
General123 Pin
AnasHashki21-Mar-06 22:43
AnasHashki21-Mar-06 22:43 
GeneralCancel the install process Pin
pauloafc28-Nov-05 6:48
pauloafc28-Nov-05 6:48 
GeneralRe: Cancel the install process Pin
BalaNet2-Jul-06 0:24
BalaNet2-Jul-06 0:24 
QuestionUrgent - To include database in setupproject in asp.net Pin
Cardiana26-Nov-05 4:48
Cardiana26-Nov-05 4:48 
AnswerRe: Urgent - To include database in setupproject in asp.net Pin
pauloafc28-Nov-05 6:57
pauloafc28-Nov-05 6:57 
GeneralCaution Pin
Frank Samjeske31-Aug-05 6:03
Frank Samjeske31-Aug-05 6:03 
GeneralRe: Caution Pin
Mukund Pujari3-Sep-05 4:31
Mukund Pujari3-Sep-05 4:31 
GeneralRe: Caution Pin
Frank Samjeske5-Sep-05 0:56
Frank Samjeske5-Sep-05 0:56 
GeneralSQLDMO Question Pin
Paul Brower31-Aug-05 3:32
Paul Brower31-Aug-05 3:32 
GeneralRe: SQLDMO Question Pin
Mukund Pujari2-Sep-05 20:45
Mukund Pujari2-Sep-05 20:45 
GeneralRe: SQLDMO Question Pin
Paul Brower3-Sep-05 3:59
Paul Brower3-Sep-05 3:59 
GeneralRe: SQLDMO Question Pin
Mukund Pujari3-Sep-05 4:15
Mukund Pujari3-Sep-05 4:15 
GeneralRe: SQLDMO Question Pin
mharr27-Oct-05 9:42
mharr27-Oct-05 9: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.