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 installs the DLL files compiled by the web page and asp.net program to the IIS Directory of the target machine. For general applications, such as Access database, can be packaged together in the installer); if the database is SQL SERVER, you need to install the database at deployment, the installation program will be more complex, we need to customize the installer class. Execute the SQL script in the installer class and write the connection string to Web. config.
ASP. NET installation and deployment · Installation Database
Microsoft MSDN introduced how to create a database when deploying an application. For example, this method creates an installer class and calls ADO. NET in the installer class to execute SQL statements and place them 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 the database project, and the cmd file also calls osql when creating the database ).
First, create an asp.net Web application, http: // localhost/VbNetTest, and open the VbNetTest project.
ASP. NET installation and deployment · 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.
Add the output of the VbNetTest project to the 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 ".
ASP. NET installation and deployment · 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.
ASP. NET installation and deployment · 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.
ASP. NET installation and deployment · create custom operations
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. Add the script file DB. SQL generated by SQL Server 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 attribute to the 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 ("AppSettings")
-
- 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 the 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!
- ASP. net mvc jQuery Delete Link
- Introduce jquery jqrte control in ASP. net mvc Framework
- Sample ASP. net mvc project: Suteki. Shop
- Example of ASP. net mvc three-tier architecture
- The concept of dependency injection in ASP. net mvc Architecture