. NET platform for deployment of Web applications (installation database and Autoconfiguration)

Source: Internet
Author: User
Tags config file system reflection access database create database install node visual studio
web| Program | data | database. NET platform for deployment of Web applications (installation database and Autoconfiguration)

Lee Honggen

Under the. NET platform, it is more convenient to deploy a WEB solution. We can use Visual Studio.NET 2003 to add a Web Setup project, add the project's primary output and content files to the deployed File System Editor, and make the setup process very easy.

However, the installer, which installs the Web page and the asp.net DLL file to the target machine's IIS directory, is OK for general applications (such as using an Access database, packaged together into the installer), and if the database is SQL SERVER, You need to install the database at the time of deployment, and the setup process will be more complex, and we need to customize the installer class. Executes the SQL script in the Installer class and writes the connection string to the web.config.

L Install the database

Microsoft MSDN has described the creation of a database when deploying applications. Such as:

http://msdn.microsoft.com/library/default.asp?url=/library/en-us/vsintro7/html/ Vxwlkwalkthroughusingcustomactiontocreatedatabaseduringinstallation.asp

This method creates a database by creating an installer class that invokes the Ado.net Execute SQL statement (SQL statement in a text file) in the Installer class.



However, there is a problem with this approach, if you use SQL Server2000 to generate all the tables, views, stored procedures, a script file, the Ado.net to execute the script file, will be because there are many "go" statements in the script error. Of course, we can replace "go" with line breaks, using ado.net to execute SQL statements. Obviously, this is less efficient.



The best way to do this is to invoke osql to execute the script. (or create a database project of the cmd file, and the cmd file set up when the database is also called osql).



First, we create a new ASP.net Web application, http://localhost/VbNetTest, and open the Vbnettest project

To create a deployment project
1. On the File menu, point to Add Item, and then select New Project.
2. In the Add New Project dialog box, select Install and deploy projects in the Project Types pane, and then select Web Setup Project in the Templates pane. Type Test Installer in the Name box.
3. Click OK to close the dialog box.
4. The project is added to Solution Explorer and the File System Editor opens.
5. In the Properties window, select the ProductName property and type Gcrm.

To add the output of the Vbnettest project to the deployment project
1. In the File System Editor, select the Web Applications folder. On the Action menu, point to Add, and then select Project Output.
2. In the Add Project Output Group dialog box, select Vbnettest in the Project Drop-down list.
3. Click OK to close the dialog box.
4. Select the primary output and content files groups from the list, and then click OK.

To create an installer class
1. On the File menu, point to New, and then select Project.
2. In the New Project dialog box, select Visual Basic project in the Project Types pane, and then select Class Library in the Templates pane. In the Name box, type DBCustomAction.
3. Click Open to close the dialog box.
4. From the Project menu, choose Add New Item.
5. In the Add New Item dialog box, select Setup class. In the Name box, type DBCustomAction.
6. Click OK to close the dialog box.

To create a Custom installation dialog box
1. Select the "Test Installer" project in Solution Explorer. On the View menu, point to Editor, and then select User interface.
2. In the User Interface Editor, select the startup node under install. On the Action menu, select Add dialog box.
3. In the Add Dialog dialog box, select the License Agreement dialog box, and then click OK to close the dialog box.
4. In the Add Dialog dialog box, select the text Box (A) dialog box, and then click OK to close the dialog box.
5. On the Action menu, select Move Up. Repeat this step until the text box (A) dialog box is above the installation folders node.
6. In the Properties window, select the BannerText property and type: Install the database ...
7. Select the BodyText property and type: Setup will install the database on the target machine.
8. Select the Edit1Label property and type: Database name:.
9. Select the Edit1Property property and type CUSTOMTEXTA1.
10. Select the Edit1Value property and type: Gscrm.
11. Select the Edit2label property and type: Server name:.
12. Select the Edit2property property and type CUSTOMTEXTA2.
13. Select the Edit2value property and type: (local).
14. Select the Edit3label property and type: User name:.
15. Select the Edit3value property and type: SA.
16. Select the Edit3property property and type CUSTOMTEXTA3.
17. Select the Edit4label property and type: password:.
18. Select the Edit4property property and type CUSTOMTEXTA4.
19. Select the Edit2Visible, Edit3Visible, and Edit4Visible properties and set them to False.

Create a custom action
1. Select the "Test Installer" project in Solution Explorer. On the View menu, point to Editor, and then select Custom Actions.
2. Select the Install node in the Custom Action Editor. On the Action menu, select Add Custom action.
3. In the Select Item in Project dialog box, double-click Application Folder.
4. Select the main output from DBCustomAction (active) Item, and then click OK to close the dialog box.
5. In the Properties window, select the CustomActionData property and type/dbname=[customtexta1]/server=[customtexta2]/user=[customtexta3/pwd=[ CUSTOMTEXTA4]/targetdir= "[targetdir]\".

Attached/targetdir= "[targetdir]\" is the target path after installation, in order to obtain the installed path in the DBCustomAction class, we set this parameter.
In addition, the installed path can also be obtained by reflection:
Dim Asm as System.Reflection.Assembly = _
System.Reflection.Assembly.GetExecutingAssembly
MsgBox ("Asm.location")
Add files
1. Add SQL Server generated script file Db.sql to the "Test Installer" project
2. Add the installation file Lisencefile.rtf to the "Test Installer" project
3. In the User Interface Editor, select the license agreement and set the Lisencefile property to Lisencefile.rtf file

Documents in the project:

Add 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 "Component Designer generated code"

Public Sub New ()

MyBase.New ()

' This call is required by the Component Designer

InitializeComponent ()

' Add any initialization after the InitializeComponent () call

End Sub

' Installer overrides dispose to clean up the list of components.

Protected Overloads Overrides Sub Dispose (ByVal disposing as Boolean)

If disposing Then

If not (components are 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 statement

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)

'------------------------Build a database-------------------------------------------------

Try

Dim connstr as String = String.Format ("Data source={0};user id={1};p assword={2};p ersist security Info=false;packet size= 4096 ", Me.Context.Parameters.Item (" Server "), Me.Context.Parameters.Item (" user "), Me.Context.Parameters.Item (" pwd " ))

' Establish a database based on the database name entered

ExecuteSQL (ConnStr, "Master", "CREATE DATABASE" + Me.Context.Parameters.Item ("dbname"))

' Invoke osql execution 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"), M E.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 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



'---------------------writes the connection string to the 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 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 = "ConnString" Then

' Write 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 ("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 compile the build!

Installation Interface:





Disclaimer: This copyright and interpretation of the right to Lee Honggen all, if necessary reprint, please retain the full content and this statement.
qq:21177563 msn:lihonggen@hotmail.com
Rubric: http://www.csdn.net/develop/author/netauthor/lihonggen0/

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.