asp.net with SQL Server deploy installation Graphics Tutorial _ Practical Tips

Source: Internet
Author: User
Tags reflection create database install node
I. Preparation of the necessary documents
1. SQL script files to generate the tables and stored procedures needed in the future installation process, and so on;

After the build, it is temporarily named Db.sql (note case)
2.LISENCEFILE.RTF installation files, because my system is personal, and free, so did not do. This is to be used later.
Two. Create a deployment project within your existing project:
1. On the File menu, point to Add Item, and then select New Project. (Figure 1-2)
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. (Figure 1-3)
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.

(1-2)

(Figure 1-3) The role of other projects can be referred to webcast
Three. Add the output of the Vbnettest project to the deployment project (if your virtual directory is sqlandaspnet)
1. In the File System Editor, select Web Application Folder. On the Action menu, point to Add, and then select Project Output. (Figure 1-4)
2. In the Add Project Output Group dialog box, select Sqlandaspnet 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. (Figure 1-5)

(Figure 1-4)

(Fig. 1-5) Other functions can refer to webcast, the source file is the file for all the purposes
Four.

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. (Figure 1-6)
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. (Note: I did not add, because temporarily I still do not need, need to add a bit OH)
4. In the Add Dialog dialog box, select the text Box (A) dialog box, and then click OK to close the dialog box. (Figure 1-7)
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 Edit2Visible, edit3visible, and set them to False. (Figure 1-8)



(Figure 1-7)


(Figure 1-8)

(v). Create a custom Action
1. In Solution Explorer, select the Test Installer project. On the View menu, point to Editor, and then select Custom Actions. (Figure 1-9)
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]\". (Figure 1-10)

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")

(Figure 1-9)

(Figure 1-10)
Oh, already much better, the rest is the key step, I spent a lot of time studying.
(vi) 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. (Figure 1-11,1-12)
Note: This is the original project to create a simple installation file on it.

(Figure 1-11)
Added effect chart:

(Figure 1-12) The SQL file here is to be added
Seven
Add files
1. Add the script file Db.sql generated by SQL Server to the "Test Installer" project (Figure 1-12)
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
(eight)
The code is the most important part of the entire deployment.

Add code to the Installer class, DBCustomAction.vb class

Copy Code code as follows:

Imports System.ComponentModel
Imports System.Configuration.Install
Imports System.IO
Imports System.Reflection
<runinstaller (TRUE) > public Class dbcustomactionclass dbcustomaction
Inherits System.Configuration.Install.Installer
Component Designer generated code #region "Component Designer generated code"
Public Sub New () 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 () 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 () Sub InitializeComponent ()
End Sub
#end Region
' Execute SQL statement
Private Sub 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.executenonquery ()
Finally
' Close Connection
Command.connection.close ()
End Try
End Sub
Public Overrides Sub Install () 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

A bit difficult is the process class, which calls the Osql.exe program to execute the SQL statement osql-u,-p,,-d,-i.
Web.config's modified code is implemented using the syntax of XML. It's not hard to understand.
Finally compile the build! As shown in figure:

Installation Interface: Figure

Haha, finally finished, do not know whether to write well. The
finally thanked Lee Honggen and the lecturer at Microsoft. Combined with their common experience, deployment is really simple.

Related Article

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.