Asp.net and SQL together for packaging, deployment, and installation (Collection)
Install and package Asp.net and SQL together
Learning from Asp.net of msdn webcastsProgramDeployment and Li honggenArticleAdd your own experience and write down the whole process of packaging SQL and Asp.net (VB.net.
1. Prepare necessary documents
1. the SQL script file generates tables and stored procedures required during installation;
After it is generated, name it dB. SQL (case sensitive)
2. lisencefile. rtf Installation File, because my system is personal and free, so there is no. This will also be used in the future.
2. Create a deployment project in your existing project:
1. Point to add project on the File menu, and select Create project. (Figure 1-2)
2. In the Add new project dialog box, select the installation and deployment projects in the project type pane, and then select the Web installation project in the template pane. In the Name box, type test installer. (Figure 1-3)
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.
(1-2)
(Figure 1-3) for the functions of other projects, see webcast.
3. Add the output of the vbnettest project to the deployment project (assume that your virtual directory is sqlandaspnet)
1. In the file system Editor, select the web application folder & quot ;. On the operation menu, point to add and select project output. (Figure 1-4)
2. In the Add project output group dialog box, select sqlandaspnet from the project drop-down list.
3. Click OK to close the dialog box.
4. Select the primary output and Content File Group from the list, and click OK. (Figure 1-5)
(Figure 1-4)
(Figure 1-5) for other functions, see webcast. The source file is the file of all projects.
4.
Create custom installation dialog box
1. Select the test installer project in Solution Explorer. Point to the editor in the View menu and select the user interface. (Figure 1-6)
2. In the User Interface Editor, select the startup node for installation. On the operation menu, select the Add dialog box.
3. In the Add dialog box, select the license agreement dialog box and click OK to close the dialog box. (Note: I didn't add it because I still don't need it for the time being. You need to add it)
4. In the Add dialog box, select the text box (A) and click OK to close the dialog box. (Figure 1-7)
5. On the operation menu, select move up. Repeat this step until the text box (a) dialog box is located above the installation 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 edit2visible and edit3visible, and set them to false. (Figure 1-8)
(Figure 1-7)
(Figure 1-8)
(5). Create a custom operation
1. Select the test installer project in Solution Explorer. Point to the editor in the View menu and select Custom operations. (Figure 1-9)
2. Select the installation node in the Custom operation editor. On the operation menu, select add custom operation.
3. In the select items dialog box, double-click the application folder.
4. Select the active output item from dbcustomaction, and click OK to close the dialog box.
5. in the Properties window, select customactiondata and type/dbname = [customtexta1]/Server = [customtexta2]/user = [customtexta3]/Pwd = [customtexta4]/targetdir = & quot; [targetdir] \ & quot ;. (Figure 1-10)
Attachment/targetdir = & quot; [targetdir] \ & quot; is the target path after installation. To obtain the installed path 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 (& quot; ASM. Location & quot ;)
(Figure 1-9)
(Figure 1-10)
Haha, it is much better. The key step is the rest. I spent a lot of time researching it.
(6) Create an installer class
1. Point to new on the File menu, and select a project.
2. In the Create Project dialog box, select the Visual Basic Project in the project type pane, and then select the class library in the template pane. In the Name box, type dbcustomaction.
3. Click to open the close dialog box.
4. Select Add new item from the project menu.
5. Select the installer class in the Add new project dialog box. In the Name box, type dbcustomaction.
6. Click OK to close the dialog box. (Figure 1-11, 1-12)
Note: You can create a simple installation file on the original project.
(Figure 1-11)
Added:
(Figure 1-12) the SQL file must be added.
(7)
Add File
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 attribute to the lisencefile. rtf file.
(8)
ClickCodeIs the most important part of the entire deployment.
Add the code to the installer class, dbcustomaction. VB class
1importssystem. componentmodel
2
3importssystem. configuration. Install
4
5importssystem. Io
6
7importssystem. Reflection
8
9
10
11 & lt; runinstaller (true) & gt; publicclassdbcustomactionclassdbcustomaction
12
13inheritssystem. configuration. Install. Installer
14
15
16
17 code generated by the component designer # region & quot; code generated by the component Designer & quot;
18
19 publicsubnew () subnew ()
20
21mybase. New ()
22
23' this call is required by the component designer
24
25 initializecomponent ()
26
27 'add any initialization after initializecomponent () is called
28
29 endsub
30
31 'installer override dispose to clear the component list.
32
33 protectedoverloadsoverridessubdispose () subdispose (byvaldisposingasboolean)
34
35 ifdisposingthen
36
37 ifnot (componentsisnothing) then
38
39components. Dispose ()
40
41 endif
42
43 endif
44
45mybase. Dispose (disposing)
46
47 endsub
48
49privatecomponentsassystem. componentmodel. icontainer
50
51 & lt; system. Diagnostics. debuggerstepthrough () & gt; privatesubinitializecomponent () subinitializecomponent ()
52
53 endsub
54
55 # endregion
56
57 'execute the SQL statement
58
59 privatesubexecutesql () subexecutesql (byvalconnasstring, byvaldatabasenameasstring, byvalsqlasstring)
60
61dimmysqlconnectionasnewsqlclient. sqlconnection (conn)
62
63dimcommandasnewsqlclient. sqlcommand (SQL, mysqlconnection)
64
65command. Connection. open ()
66
67command. Connection. changedatabase (databasename)
68
69try
70
71command.exe cutenonquery ()
72
73 finally
74
75 'closeconnection
76
77command. Connection. Close ()
78
79 endtry
80
81 endsub
82
83 publicoverridessubinstall () subinstall (byvalstatesaverassystem. Collections. idictionary)
84mybase. Install (statesaver)
85
86 '------------------------ create a database -------------------------------------------------
87
88try
89
90 dimconnstrasstring = string. format (& quot; datasource = {0}; userid = {1}; Password = {2}; persistsecurityinfo = false; packetsize = 4096 & quot;, me. context. parameters. item (& quot; server & quot;), me. context. parameters. item (& quot; user & quot;), me. context. parameters. item (& quot; PWD & quot ;))
91
92 'create a database based on the input database name
93
94 executesql (connstr, & quot; Master & quot;, & quot; createdatabase & quot; + me. Context. Parameters. Item (& quot; dbname & quot ;))
95
96 'call osql to execute the script
97
98dimsqlprocessasnewsystem. Diagnostics. Process
99
100sqlprocess.startinfo.filename="=osql.exe & quot;
101
102sqlprocess. startinfo. arguments = string. format (& quot;-U {0}-P {1}-d {2}-I {3} dB. SQL & quot;, me. context. parameters. item (& quot; user & quot;), me. context. parameters. item (& quot; PWD & quot;), me. context. parameters. item (& quot; dbname & quot;), me. context. parameters. item (& quot; targetdir & quot ;))
103
104sqlprocess. startinfo. windowstyle = processwindowstyle. Hidden
105
106sqlprocess. Start ()
107
108sqlprocess. waitforexit () 'waiting for execution
109
110sqlprocess. Close ()
111
112 'delete the script file
113
114dimsqlfileinfoasnewsystem. io. fileinfo (string. format (& quot; {0} dB. SQL & quot;, me. context. parameters. item (& quot; targetdir & quot ;)))
115
116ifsqlfileinfo. existsthen
117
118sqlfileinfo. Delete ()
119
120 endif
121
122 catchexasexception
123
124 throwex
125
126 endtry
127
128
129
130 '--------------------- write the connection string to Web. config -----------------------------------
131
132try
133
134dimfileinfoassystem. io. fileinfo = Newsystem. io. fileinfo (Me. context. parameters. item (& quot; targetdir & quot;) & amp; & quot; \ Web. config & quot ;)
135
136ifnotfileinfo. existsthen
137
138 thrownewinstallexception (& quot; no configuration file found & quot ;)
139
140 endif
141
142 'instantiate XML document
143
144dimxmldocumentasnewsystem. xml. xmldocument
145
146xmldocument. Load (fileinfo. fullname)
147
148
149
150 find the node in the ettings
151
152dimnodeassystem. xml. xmlnode
153
154 dimfounditasboolean = false
155
156foreachnodeinxmldocument. Item (& quot; configuration & quot;). Item (& quot; appsettings & quot ;)
157
158ifnode. Name = & quot; add & quot; then
159
160ifnode. Attributes. getnameditem (& quot; Key & quot;). value = & quot; connstring & quot; then
161
162 'Write connection string
163
164node. attributes. getnameditem (& quot; Value & quot ;). value = string. format (& quot; persistsecurityinfo = false; datasource = {0}; initialcatalog = {1}; userid = {2}; Password = {3}; packetsize = 4096; pooling = true; maxpoolsize = 100; minpoolsize = 1 & quot ;,_
165
166me. context. parameters. item (& quot; server & quot;), me. context. parameters. item (& quot; dbname & quot;), me. context. parameters. item (& quot; user & quot;), me. context. parameters. item (& quot; PWD & quot ;))
167
168 foundit = true
169
170 endif
171
172 endif
173
174 nextnode
175
176 ifnotfounditthen
177
178 thrownewinstallexception (& quot; the Web. config file does not contain the connstring connection string setting & quot ;)
179
180 endif
181
182xmldocument. Save (fileinfo. fullname)
183
184 catchexasexception
185
186 throwex
187
188 endtry
189
190 endsub
191
192 endclass
193
194
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:
Haha, I finally finished writing, and I don't know if I have written it.
Finally, I would like to thank Mr. Li honggen and Microsoft's lecturers. Combined with their common experience, the deployment is really simple.