On the. NET platform, select lihonggen0 blog for deploying web applications (installing databases and automatic configuration ).

Source: Internet
Author: User
It is convenient to deploy Web solutions on the. NET platform. We can use Visual Studio. NET 2003 adds a web installation project, and adds the project's main output and content files to the deployed "File System Editor" to easily complete the installation program.

However, the installation program created in this way only includes the web page and ASP. install the DLL files compiled by the net program to the IIS Directory of the target machine. This is acceptable for general applications (for example, the ACCESS database can be packaged into the installer together ); if the database is SQL Server, you need to install the database at the time of deployment. The creation of the installation program will be more complicated. You need to customize the installation program class. Execute the SQL script in the installer class and write the connection string to Web. config.

L install the database

Microsoft msdn introduced how to create a database when deploying an application. For example:

Http://msdn.microsoft.com/library/default.asp? Url =/library/en-US/vsintro7/html/vxwlk?throughusingcustomactiontocreatedatabaseduringinstallation. asp 

This method creates an installer class and calls ADO. net in the installer class to execute an SQL statement (the SQL statement is placed in a text file) to create a database.

 

However, this method has a problem. If SQL Server2000 is used to generate a script file for all table creation, views, and stored procedures, ADO is used. net to execute this script file, it will be because the script contains many "go" statements and errors. Of course, we can replace "go" with a line break and use ADO. Net to execute SQL statements one by one. Obviously, the efficiency is relatively low.

 

The best way is to call osql to execute the script. (Or create a CMD file for a database project, and the CMD file also calls osql when creating a database ).

 

First, create an ASP. NET web application, http: // localhost/vbnettest, and open the vbnettest project.

Create a deployment project

1. Point to "add project" on the "file" menu and select "new project ". 2. In the "Add new project" dialog box, select "Install and deploy Project" in the "project type" pane, and select "Web Installation Project" in the "template" pane ". In the Name box, type test installer. 3. Click OK to close the dialog box. 4. The project is added to Solution Explorer and opened in the file system editor. 5. In the Properties window, select productname properties and type gcrm. Set Vbnettest Add project output to deployment project 1. In the file system Editor, select the web application folder. On the "operations" menu, point to "add" and select "project output ". 2. In the "add project output group" dialog box, select "vbnettest" from the "project" drop-down list ". 3. Click OK to close the dialog box. 4. Select the "main output" and "Content File" groups from the list, and click "OK ". Create installer class 1. Point to "new" on the "file" menu and select "project ". 2. In the "new project" dialog box, select "Visual Basic Project" in the "project type" pane and select "class library" in the "template" pane ". In the "name" box, type dbcustomaction. 3. Click Open to close the dialog box. 4. Select "Add new project" from the "project" menu ". 5. In the "Add new project" dialog box, select "Installer class ". In the "name" box, type dbcustomaction. 6. Click OK to close the dialog box. Create custom installation dialog box 1. Select the "test installer" project in Solution Explorer. On the "View" menu, point to "Editor" and select "User Interface ". 2. In the User Interface Editor, select the "Start" node under "installation. On the "operations" menu, select "add dialog box ". 3. In the "add dialog box", select the "License Agreement" dialog box and click "OK" to close the dialog box. 4. In the Add dialog box, select the text box (A) and click OK to close the dialog box. 5. On the "operations" menu, select "Move Up ". Repeat this step until the "text box (a)" dialog box is located on the "Install Folder" node. 6. In the "properties" window, select the bannertext attribute and type: Install database .. 7. Select the bodytext attribute and type: the installer will install the database on the target machine. 8. Select the edit1label attribute and type: Database Name :. 9. Select the edit1property property and type customtexta1. 10. Select the edit1value attribute and type: gscrm. 11. Select the edit2label attribute and type: Server Name :. 12. Select the edit2property property and type customtexta2. 13. Select the edit2value attribute and type: (local ). 14. Select the edit3label attribute and type: User name :. 15. Select the edit3value attribute and type SA. 16. Select the edit3property property and type customtexta3. 17. Select the edit4label attribute and type: Password :. 18. Select the edit4property property and type customtexta4. 19. Select the edit2visible, edit3visible, and edit4visible attributes, and set them to false. Create a custom operation 1. Select the "test installer" project in Solution Explorer. On the "View" menu, point to "Editor" and select "Custom operations ". 2. Select the "Install" node in the Custom operation editor. On the "operations" menu, select "add custom operation ". 3. In the "select project items" dialog box, double-click "application folder ". 4. Select "main output from dbcustomaction (activity)" and click "OK" to close the dialog box. 5. In the "properties" window, select the customactiondata attribute and type
/Dbname = [customtexta1]/Server = [customtexta2]/user = [customtexta3]
/Pwd = [customtexta4]/targetdir = "[targetdir] \". Attachment/targetdir = "[targetdir] \" is the target path after installation. To obtain the path after installation in the dbcustomaction class, we set this parameter. In addition, the installation path can also be obtained through reflection: dim ASM as system. reflection. Assembly = _ system. reflection. Assembly. getexecutingassembly msgbox ("ASM. Location ") Add File 1. database the script file generated by SQL Server. add SQL to "test installer" Project 2. set the Installation File lisencefile. add RTF to "test installer" Project 3. in the User Interface Editor, select the license agreement and set the lisencefile attribute to lisencefile. RTF File Files in the project :Add the code to the installer class, dbcustomaction. VB class

Imports system. componentmodel

Imports system. configuration. Install

Imports system. Io

Imports system. Reflection

 

<Runinstaller (true)> public class dbcustomaction

Inherits system. configuration. Install. Installer

 

# Region "code generated by the component designer"

Public sub new ()

Mybase. New ()

'The call is required by the component designer

Initializecomponent ()

'Add any initialization after initializecomponent () is called.

End sub

'Installer override dispose to clear the component list.

Protected overloads overrides sub dispose (byval disposing as Boolean)

If disposing then

If not (components is nothing) then

Components. Dispose ()

End if

End if

Mybase. Dispose (disposing)

End sub

Private components as system. componentmodel. icontainer

<System. Diagnostics. debuggerstepthrough ()> private sub initializecomponent ()

End sub

# End Region

'Execute SQL statements

Private sub executesql (byval conn as string, byval databasename as string, byval SQL as string)

Dim mysqlconnection as new sqlclient. sqlconnection (conn)

Dim command as new sqlclient. sqlcommand (SQL, mysqlconnection)

Command. Connection. open ()

Command. Connection. changedatabase (databasename)

Try

Command. executenonquery ()

Finally

'Close connection

Command. Connection. Close ()

End try

End sub

Public overrides sub install (byval statesaver as system. Collections. idictionary)

Mybase. Install (statesaver)

'------------------------ Create a database -------------------------------------------------

Try

Dim connstr as string = string. format ("Data Source = {0}; user id = {1}; Password = {2}; persist Security info = false; packet size = 4096", me. context. parameters. item ("server"), me. context. parameters. item ("user"), me. context. parameters. item ("PWD "))

'Create a database based on the input database name

Executesql (connstr, "Master", "create database" + me. Context. Parameters. Item ("dbname "))

'Call osql to execute the script

Dim sqlprocess as new system. Diagnostics. Process

Sqlprocess. startinfo. filename = "osql.exe"

Sqlprocess. startinfo. arguments = string. format ("-U {0}-P {1}-d {2}-I {3} dB. SQL ", me. context. parameters. item ("user"), me. context. parameters. item ("PWD"), me. context. parameters. item ("dbname"), me. context. parameters. item ("targetdir "))

Sqlprocess. startinfo. windowstyle = processwindowstyle. Hidden

Sqlprocess. Start ()

Sqlprocess. waitforexit () 'waiting for execution

Sqlprocess. Close ()

'Delete the script file

Dim sqlfileinfo as new system. Io. fileinfo (string. Format ("{0} dB. SQL", me. Context. Parameters. Item ("targetdir ")))

If sqlfileinfo. exists then

Sqlfileinfo. Delete ()

End if

Catch ex as exception

Throw ex

End try

 

'--------------------- Write the connection string to Web. config -----------------------------------

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 configuration file found ")

End if

'Instantiate XML document

Dim xmldocument as new system. xml. xmldocument

Xmldocument. Load (fileinfo. fullname)

 

'Find the node in the deleetask'

Dim node as system. xml. xmlnode

Dim foundit as Boolean = false

For each node in xmldocument. Item ("configuration"). Item ("etettings ")

If node. Name = "add" then

If node. Attributes. getnameditem ("key"). value = "connstring" then

'Write the connection string

Node. Attributes. getnameditem ("value"). value = string. Format ("persist
Security info = false; Data Source = {0}; initial catalog = {1}; User
Id = {2}; Password = {3}; packet size = 4096; pooling = true; Max pool size = 100; min
Pool size = 1 ",_

Me. context. parameters. item ("server"), me. context. parameters. item ("dbname"), 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 ("the 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

End Class

Finally compiled and generated! Installation interface:

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.