Manufacturing Web Setup with VS2005

Source: Internet
Author: User
Tags create database

Use VS2005 Manufacturing WEB Setup program

Environment: windows2003 + vs2005+sqlserver2000

1, open the VS2005, open the project or website (the example here is a published Web site system).

2, from the Solution Explorer right click on the solution name, select Add-New project, and from the Add New Project window open, select Other project types in the project type column-install deployment, in the template bar, choose Web Project, Enter a name and address in the name and address bar below.

3. Generate SQL files (generate only SQL statements to create data tables)

Open Sqlser Enterprise Manager, login, select Database--Select the table to generate SQL statements, right--all tasks--Generate SQL scripts. Click "OK" in the open window to save the SQL script (note: SQL file must be named after the English name in lowercase, save the SQL file as a TXT file after saving, and the inside of all the "go" removed. )

4. Create installer Class Library and installer class

Right-click the solution name from Solution Explorer select Add-New project, select "Visal BASIC" in the project Type column from the Open Add New Project window, in the template bar, select Class Library, and enter a name and address in the name and address bar below. Click "OK".

5, from "Solution Explorer" click just generated class library, delete automatically generated Class1.vb class, right click Class Library name-add-new Item, select "Installer Class", enter the name, click the "Add" button.

6, open the newly generated installer class, in Dbtest.vb, add the following code

Imports System.ComponentModel

Imports System.Configuration.Install

Imports System.IO

Imports System.Reflection

Public Class Dbtest

' Declare private variables

Private SqlConnection1 as Sqlclient.sqlconnection

Private ServerName as String

Private AdminName as String

Private adminpwd as String

Public Sub New ()

MyBase.New ()

' Component Designer requires this call.

InitializeComponent ()

' Add initialization code after calling InitializeComponent

End Sub

Private Function GetSQL (ByVal Name As String) as String

Try

' Gets the current assembly.

Dim ASM as [Assembly] = [Assembly]. GetExecutingAssembly ()

' Resources are named using a fully qualified name.

Dim strm as Stream = Asm.getmanifestresourcestream (_

Asm.getname (). Name + "." + name)

' Reads the contents of the embedded file.

Dim reader as StreamReader = New StreamReader (STRM)

Return reader. ReadToEnd ()

Catch ex as Exception

MsgBox ("Error reading SQL file:" & ex. Message)

Throw EX

End Try

End Function

Private Sub ExecuteSQL (ByVal DatabaseName As String, ByVal Sql as String)

Dim Command as New sqlclient.sqlcommand (SQL, SqlConnection1)

Command.Connection.Open ()

Command.Connection.ChangeDatabase (DatabaseName)

Try

Command.executenonquery ()

Finally

' Finally, blocks are a great way to ensure the connection

' is always closed.

Command.Connection.Close ()

End Try

End Sub

Protected Sub adddbtable ()

Try

' Build the Database

ExecuteSQL ("Master", "CREATE DATABASE Water")

' Generate tables from SQL statements

ExecuteSQL ("Water", GetSQL ("Watertable.txt"))

Catch ex as Exception

' Reports any errors and abort.

MsgBox ("Generate Database error:" & ex. Message)

Throw EX

End Try

End Sub

Private Sub Writewebconfig ()

' Modify the Web.config file

Try

Dim FileInfo as System.IO.FileInfo = New System.IO.FileInfo (Me.Context.Parameters.Item ("Targetdir") & "/web.config ")

If not fileinfo.exists Then

Throw New installexception ("No Profile Found")

End If

' Instantiate an XML document

Dim XmlDocument as New System.Xml.XmlDocument

XmlDocument.Load (Fileinfo.fullname)

' Find a node in the appsettings

Dim Node as System.Xml.XmlNode

Dim Foundit as Boolean = False

For each Node in Xmldocument.item ("Configuration"). Item ("AppSettings")

If node.name = "Add" Then

If Node.Attributes.GetNamedItem ("key"). Value = "Adoconnbysql" Then

' Write connection string

Node.Attributes.GetNamedItem ("value"). Value = String.Format ("Provider=sqloledb;data source={0}; PASSWORD={2}; User Id={1};initial Catalog=water ", Me.Context.Parameters.Item (" Server "), Me.Context.Parameters.Item (" User "), Me.Context.Parameters.Item ("pwd"))

Foundit = True

End If

End If

Next Node

If not Foundit Then

Throw New installexception ("web. Config file does not contain connstring connection string settings ")

End If

Xmldocument.save (Fileinfo.fullname)

Catch ex as Exception

Throw EX

End Try

End Sub

"' <summary>

' Installer entry

"' </summary>

"<param name=" Statesaver "></param>

"' <remarks></remarks>

Public Overrides Sub Install (ByVal statesaver as _

System.Collections.IDictionary)

Mybase.install (statesaver)

' Read user connection database information

ServerName = Trim (Me.Context.Parameters.Item ("Server"))

AdminName = Trim (Me.Context.Parameters.Item ("user"))

adminpwd = Trim (Me.Context.Parameters.Item ("pwd"))

' Establish a database connection based on the information entered in the user interface

SqlConnection1 = New sqlclient.sqlconnection

sqlconnection1.connectionstring = "User id=" + adminname + ";D ata Source = + ServerName +"; Password= "+ adminpwd +"; Initial catalog=master "

' Sqlconnection1.open ()

' Add a table in the database

Adddbtable ()

' Modify the database connection string in the Web.config file

Writewebconfig ()

End Sub

End Class

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.