Introduction to ASP. NET installation and deployment code implementation:
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.
The following ASP. NET installation and deployment code implementation is the most important part of the entire deployment.
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 DBCustomActionClass DBCustomAction
-
- Inherits System. Configuration. Install. Installer
-
-
- # Region "code generated by the component designer"
-
- PublicSub 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.
-
- ProtectedOverloads Overrides Sub Dispose () Sub Dispose (ByVal disposing As Boolean)
-
- IfDisposing Then
-
- IfNot (components Is Nothing) Then
-
- Components. dispose ()
-
- End If
-
- End If
-
- Mybase. dispose (disposing)
-
- End Sub
-
- PrivateComponents As System. ComponentModel. IContainer
-
- <System. diagnostics. debuggerstepthrough ()>
- Private Sub InitializeComponent ()
-
- End Sub
-
- # End Region
-
- 'Execute SQL statements
-
- PrivateSub ExecuteSql () 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.exe cutenonquery ()
-
- Finally
-
- 'Close Connection
-
- Command. connection. close ()
-
- End Try
-
- End Sub
-
- PublicOverrides Sub Install () Sub Install (ByVal stateSaver
- As System. Collections. IDictionary)
- MyBase. Install (stateSaver)
-
- '---------- ASP. NET installation and deployment code to build 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"))
-
- 'Asp. NET installation and deployment code implementation: Calling osql execution scripts
-
- 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 ()
-
- 'Asp. NET installation and deployment code implementation: deleting script files
-
- Dim sqlFileInfo As New System. IO. FileInfo (String. Format ("{0} db. SQL",
- Me. Context. Parameters. Item ("Targetdir")))
-
- IfSqlFileInfo. Exists Then
-
- Sqlfileinfo. delete ()
-
- End If
-
- CatchEx As Exception
-
- ThrowEx
-
- End Try
-
-
-
- '-ASP. NET installation and deployment code implementation: writing the connection string to Web. config --
-
- Try
-
- Dim FileInfo As System. IO. FileInfo = New System. IO.
- FileInfo (Me. Context. Parameters. Item ("Targetdir")&"\ Web. config")
-
- IfNot FileInfo. Exists Then
-
- ThrowNew 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
-
- ForEach Node In XmlDocument. Item ("Configuration"). Item ("AppSettings")
-
- IfNode. Name ="Add"Then
-
- IfNode. Attributes. GetNamedItem ("Key"). Value ="ConnString"Then
-
- 'Asp. NET installation and deployment code implementation-writing 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
-
- IfNot FoundIt Then
-
- ThrowNew InstallException ("The web. Config file does not contain the connString connection string Settings")
-
- End If
-
- Xmldocument. save (fileinfo. fullname)
-
- CatchEx As Exception
-
- ThrowEx
-
- End Try
-
- End Sub
-
- End Class
The processclass, which is somewhat difficult, calls the osql.exe program to execute the SQL statements osql-U,-P,-d,-I.
The modification code of web. config is implemented using xml syntax. Not difficult to understand.
Finally compiled and generated!
Installation interface:
This section describes the implementation of ASP. NET installation and deployment code. It is helpful for you to learn how to install and deploy ASP. NET.
- Detailed illustration of creating a project for ASP. NET installation and deployment
- Import project diagram of ASP. NET installation and deployment
- Create dialog box for ASP. NET installation and deployment
- Create custom operations for ASP. NET installation and deployment
- ASP. NET installation and deployment: Creating and installing programs