|
| |
Most of the web applications that we
create are not simple web pages but manipulate databases as well. When we deploy
our applications we manually create a virtual directory, deploy the project and
then we create the database by running a sql script against the database. To
automate the installation of the web application we can create a setup package
which is explained here (http://www.dotnetforce.com/Content.aspx?t=a&n=214).In
this article we will see how to create a database and tables in the database at
the time of setup of the application.
We will first create a
dll which will contain the code to create the database in the database
server.This code in the dll will be called automatically at the time when the
application is installed. - Open VS.NET and add a class library to the
solution.
- Using the add reference option; add a reference to the
“C:\Program Files\Common Files\System\Ole DB\oledb32.dll”
dll. MSDASC and OleDbError will be added to the reference tree nodes. Also add a
reference to adodb from the add reference dialog box, .net components
tab.
- Remove the Class1.cs file from the solution and add an
Installer class files using the “add new item” option.
- Add a overridden method with following signature to the
installer class
publicoverridevoid
Install(IDictionary
stateSaver) { //
TODO: Add Installer1.Install
implementation base.Install
(stateSaver); createDatabase();
}
|
- Add the following code to the createDatabase
method
privatevoid
CreateDatabase() {
MSDASC.DataLinks
dataLinks =
new
MSDASC.DataLinksClass(); ADODB.Connection
cs=(ADODB.Connection)dataLinks.PromptNew(); cs.Open("","","",0); object
missing =
null; string
database
="SetupTestDatabase"; cs.Execute("create
database " +
database,out
missing,0); string
comma = "CREATE TABLE [dbo].[authors] ("
+ "[au_id] [int] NOT NULL ," +
"[au_lname] [varchar] (40) COLLATE
SQL_Latin1_General_CP1_CI_AS NOT NULL ," +
"[au_fname] [varchar] (20) COLLATE
SQL_Latin1_General_CP1_CI_AS NOT NULL ," +
"[phone] [char] (12) COLLATE
SQL_Latin1_General_CP1_CI_AS NOT NULL ," +
"[address] [varchar] (40) COLLATE
SQL_Latin1_General_CP1_CI_AS NULL ," +
"[city] [varchar] (20) COLLATE
SQL_Latin1_General_CP1_CI_AS NULL ," +
"[state] [char] (2) COLLATE
SQL_Latin1_General_CP1_CI_AS NULL ,"
+ "[zip] [char] (5) COLLATE
SQL_Latin1_General_CP1_CI_AS NULL ," +
"[contract] [bit] NOT NULL" +
") ON
[PRIMARY]"; cs.DefaultDatabase =
database; cs.Execute
(comma, out
missing, 0);
}
|
- Now add a setup project to the same solution. Click the
custom action button and add a custom action during the install
process.
- Right click on the Install folder of the custom
actions. Chose application folder and then click on the “Add Output”
button. Now add the primary output of the class library by clicking the button.
A new custom action will be added to the install
folder.
- The setup is ready now. Compile the projects and run
the setup.
- On running the setup following dialog box will be
shown.
- Please note that the “Allow saving
password” should be checked on at the time of setup, otherwise the setup
will give “login failed for the user”
error.
|
.NET Force is optimised for
Microsoft Internet Explorer 5 browsers.
Copyright © 2004 .NET Force.
Terms and Condition. All rights reserved.
|
 |
|