Thursday, July 31, 2014
Search:
Article
48Hrs Help PAQ's
FAQ's
Download
Books
MS Alerts
Write to Us
Feedback
Link




a) Why use .NET My Services?
b) What are .NET My Services?

Get This Blog via Email:


Email This Feed Using Squeet




Creating setup With support for database creation Using VS.NET


By Mahish Gour
June 4, 2004
Page is Viewed 21272 times


  
Manish Gour

Introduction:

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.

Steps to create the Installation:

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.

Conclusion:

In the article we learnt how to create database and table at the time of setup. Other database objects can be created in the similar fashion in the database. If you face any problem while creating the setup; send me a mail at manishgo_in@indiatimes.com .

Reference:
http://support.microsoft.com/default.aspx?scid=kb;EN-US;310083

Download:
http://www.dotnetforce.com/download/dbsetup.zip







Microsoft:
Microsoft largest software production company. Listed in Nasdaq as msft.
.NET:
The .NET Framework is a new computing platform that simplifies application development in the highly distributed environment of the Internet.
Class:
A reference type that encapsulates data (constants and fields) and behavior (methods, properties, indexers, events, operators, instance constructors, static constructors, and destructors), and can contain nested types. Class types support inheritance, a mechanism whereby a derived class can extend and specialize a base class. See also: encapsulation, indexer, property, reference type.
Contract:
The behavior and state that a class provides, which is matched with what a client of that class can expect to hold. A contract is expressed partly by the signatures for all public fields, methods, properties, and events of that class. This is augmented by a description (usually in simple descriptive text) of what each field or property represents, together with what each method does.
.NET Force is optimised for Microsoft Internet Explorer 5 browsers.
Copyright © 2004 .NET Force.
Terms and Condition. All rights reserved.