Asp.net与SQL一起打包部署安装

借鉴MSDN webcasts的Asp.net程序部署李洪根的一篇文章,然后加上自己的亲身体会,把整个SQL和Asp.net(vb.net)一起打包的全过程写一下。
一.准备必要的文件
1. SQL脚本文件,生成以后安装过程中需要的表和存储过程等等;
Asp.net与SQL一起打包部署安装
生成之后,就暂时把它命名为db.sql(注意大小写)
2.LisenceFile.rtf的安装文件,因为我的系统是个人的,而且free的,所以就没有做。这个以后也是要用到的。
二.在自己现有的项目里面创建部署项目:
1. 在“文件”菜单上指向“添加项目”,然后选择“新建项目”。(图1-2)
2. 在“添加新项目”对话框中,选择“项目类型”窗格中的“安装和部署项目”,然后选择“模板”窗格中的“Web 安装项目”。在“名称”框中键入 Test Installer。(图1-3)
3. 单击“确定”关闭对话框。
4. 项目被添加到解决方案资源管理器中,并且文件系统编辑器打开。
5. 在“属性”窗口中,选择 ProductName 属性,并键入 GCRM。
Asp.net与SQL一起打包部署安装
(1-2)
Asp.net与SQL一起打包部署安装
(图1-3)其他项目的作用可以参考Webcast
三。将 VbNetTest项目的输出添加到部署项目中(假如你的虚拟目录是SQLANDASPNet)
1. 在“文件系统编辑器”中,选择“Web 应用程序文件夹"。在“操作”菜单上,指向“添加”,然后选择“项目输出”。(图1-4)
2. 在“添加项目输出组”对话框中,选择“项目”下拉列表中的“SQLANDASPNet”。
3. 单击“确定”关闭对话框。
4. 从列表中选择“主输出”和“内容文件”组,然后单击“确定”。(图1-5)
Asp.net与SQL一起打包部署安装
(图1-4)
Asp.net与SQL一起打包部署安装
(图1-5)其他的作用可以参考webcast,源文件就是所有项目的的文件
四.

创建自定义安装对话框
1. 在解决方案资源管理器中选择“Test Installer”项目。在“视图”菜单上指向“编辑器”,然后选择“用户界面”。 (图1-6)
2. 在用户界面编辑器中,选择“安装”下的“启动”节点。在“操作”菜单上,选择“添加对话框”。
3. 在“添加对话框”对话框中,选择“许可协议”对话框,然后单击“确定”关闭对话框。 (注:我没有添加,因为暂时我还是不需要,需要的要添加一下哦)
4. 在“添加对话框”对话框中,选择“文本框 (A)”对话框,然后单击“确定”关闭对话框。 (图1-7)
5. 在“操作”菜单上,选择“上移”。重复此步骤,直到“文本框 (A)”对话框位于“安装文件夹”节点之上。
6. 在“属性”窗口中,选择 BannerText 属性并键入:安装数据库.。
7. 选择 BodyText 属性并键入:安装程序将在目标机器上安装数据库。
8. 选择 Edit1Label 属性并键入:数据库名称:。
9. 选择 Edit1Property 属性并键入 CUSTOMTEXTA1。
10. 选择 Edit1Value 属性并键入:GsCrm。
11. 选择 Edit2Label 属性并键入:服务器名:。
12. 选择 Edit2Property 属性并键入 CUSTOMTEXTA2。
13. 选择 Edit2Value 属性并键入:(local)。
14. 选择 Edit3Label 属性并键入:用户名:。
15. 选择 Edit3Value 属性并键入:sa。
16. 选择 Edit3Property 属性并键入 CUSTOMTEXTA3。
17. 选择 Edit4Label 属性并键入:密码:。
18. 选择 Edit4Property 属性并键入 CUSTOMTEXTA4。
19. 选择 Edit2Visible、Edit3Visible ,并将它们设置为 False。(图1-8)
Asp.net与SQL一起打包部署安装

Asp.net与SQL一起打包部署安装
(图1-7)

Asp.net与SQL一起打包部署安装
(图1-8)

(五).创建自定义操作
1. 在解决方案资源管理器中选择“Test Installer”项目。在“视图”菜单上指向“编辑器”,然后选择“自定义操作”。(图1-9)
2. 在自定义操作编辑器中选择“安装”节点。在“操作”菜单上,选择“添加自定义操作”。
3. 在“选择项目中的项”对话框中,双击“应用程序文件夹”。
4. 选择“主输出来自 DBCustomAction(活动)”项,然后单击“确定”关闭对话框。
5. 在“属性”窗口中,选择 CustomActionData 属性并键入 /dbname=[CUSTOMTEXTA1] /server=[CUSTOMTEXTA2] /user=[CUSTOMTEXTA3] /pwd=[CUSTOMTEXTA4] /targetdir="[TARGETDIR]/"。 (图1-10)

附/targetdir="[targetdir]/"是安装后的目标路径,为了在dbcustomaction类中获得安装后的路径,我们设置此参数。
另外,安装后的路径也可以通过Reflection得到:
Dim Asm As System.Reflection.Assembly = _
System.Reflection.Assembly.GetExecutingAssembly
MsgBox("Asm.Location")
Asp.net与SQL一起打包部署安装
(图1-9)
Asp.net与SQL一起打包部署安装
(图1-10)
呵呵,已经好多了,剩下来的是关键性步骤,我花了好多时间研究。
(六)创建安装程序类
1. 在“文件”菜单上指向“新建”,然后选择“项目”。
2. 在“新建项目”对话框中,选择“项目类型”窗格中的“Visual Basic 项目”,然后选择“模板”窗格中的“类库”。在“名称”框中键入 DBCustomAction。
3. 单击“打开”关闭对话框。
4. 从“项目”菜单中选择“添加新项”。
5. 在“添加新项”对话框中选择“安装程序类”。在“名称”框中键入 DBCustomAction。
6. 单击“确定”关闭对话框。(图1-11,1-12)
注:这里是在原来的项目上建立一个简单的安装文件就可以了。
Asp.net与SQL一起打包部署安装
(图1-11)
添加后的效果图:
Asp.net与SQL一起打包部署安装
(图1-12)这里的sql文件是要等一下添加的
(七)
添加文件
1. 将SQL Server生成的脚本文件db.sql添加到“Test Installer”项目(图1-12)
2. 将安装文件LisenceFile.rtf添加到“Test Installer”项目
3. 在用户界面编辑器中,选择许可协议,设置LisenceFile属性为LisenceFile.rtf文件
(八)
一下的代码是整个部署的最重要的一部分了

将代码添加到安装程序类中,dbcustomaction.vb类

1Asp.net与SQL一起打包部署安装ImportsSystem.ComponentModel
2Asp.net与SQL一起打包部署安装
3Asp.net与SQL一起打包部署安装importsSystem.Configuration.Install
4Asp.net与SQL一起打包部署安装
5Asp.net与SQL一起打包部署安装importsSystem.IO
6Asp.net与SQL一起打包部署安装
7Asp.net与SQL一起打包部署安装importsSystem.Reflection
8Asp.net与SQL一起打包部署安装
9Asp.net与SQL一起打包部署安装
10Asp.net与SQL一起打包部署安装
11Asp.net与SQL一起打包部署安装Asp.net与SQL一起打包部署安装<runinstaller(true)>PublicClassDBCustomActionClassDBCustomAction
12Asp.net与SQL一起打包部署安装
13Asp.net与SQL一起打包部署安装inheritsSystem.Configuration.Install.Installer
14Asp.net与SQL一起打包部署安装
15Asp.net与SQL一起打包部署安装
16Asp.net与SQL一起打包部署安装
17Asp.net与SQL一起打包部署安装Asp.net与SQL一起打包部署安装组件设计器生成的代码#region"组件设计器生成的代码"
18Asp.net与SQL一起打包部署安装
19Asp.net与SQL一起打包部署安装Asp.net与SQL一起打包部署安装publicSubNew()SubNew()
20Asp.net与SQL一起打包部署安装
21Asp.net与SQL一起打包部署安装mybase.new()
22Asp.net与SQL一起打包部署安装
23Asp.net与SQL一起打包部署安装'该调用是组件设计器所必需的
24Asp.net与SQL一起打包部署安装
25Asp.net与SQL一起打包部署安装initializecomponent()
26Asp.net与SQL一起打包部署安装
27Asp.net与SQL一起打包部署安装'在InitializeComponent()调用之后添加任何初始化
28Asp.net与SQL一起打包部署安装
29Asp.net与SQL一起打包部署安装endSub

30Asp.net与SQL一起打包部署安装
31Asp.net与SQL一起打包部署安装'Installer重写dispose以清理组件列表。
32Asp.net与SQL一起打包部署安装
33Asp.net与SQL一起打包部署安装Asp.net与SQL一起打包部署安装protectedOverloadsOverridesSubDispose()SubDispose(ByValdisposingAsBoolean)
34Asp.net与SQL一起打包部署安装
35Asp.net与SQL一起打包部署安装ifdisposingThen
36Asp.net与SQL一起打包部署安装
37Asp.net与SQL一起打包部署安装ifNot(componentsIsNothing)Then
38Asp.net与SQL一起打包部署安装
39Asp.net与SQL一起打包部署安装components.dispose()
40Asp.net与SQL一起打包部署安装
41Asp.net与SQL一起打包部署安装endIf
42Asp.net与SQL一起打包部署安装
43Asp.net与SQL一起打包部署安装endIf
44Asp.net与SQL一起打包部署安装
45Asp.net与SQL一起打包部署安装mybase.dispose(disposing)
46Asp.net与SQL一起打包部署安装
47Asp.net与SQL一起打包部署安装endSub

48Asp.net与SQL一起打包部署安装
49Asp.net与SQL一起打包部署安装privatecomponentsAsSystem.ComponentModel.IContainer
50Asp.net与SQL一起打包部署安装
51Asp.net与SQL一起打包部署安装Asp.net与SQL一起打包部署安装<system.diagnostics.debuggerstepthrough()>PrivateSubInitializeComponent()SubInitializeComponent()
52Asp.net与SQL一起打包部署安装
53Asp.net与SQL一起打包部署安装endSub

54Asp.net与SQL一起打包部署安装
55Asp.net与SQL一起打包部署安装#endRegion

56Asp.net与SQL一起打包部署安装
57Asp.net与SQL一起打包部署安装'执行sql语句
58Asp.net与SQL一起打包部署安装
59Asp.net与SQL一起打包部署安装Asp.net与SQL一起打包部署安装privateSubExecuteSql()SubExecuteSql(ByValconnAsString,ByValDatabaseNameAsString,ByValSqlAsString)
60Asp.net与SQL一起打包部署安装
61Asp.net与SQL一起打包部署安装dimmySqlConnectionAsNewSqlClient.SqlConnection(conn)
62Asp.net与SQL一起打包部署安装
63Asp.net与SQL一起打包部署安装dimCommandAsNewSqlClient.SqlCommand(Sql,mySqlConnection)
64Asp.net与SQL一起打包部署安装
65Asp.net与SQL一起打包部署安装command.connection.open()
66Asp.net与SQL一起打包部署安装
67Asp.net与SQL一起打包部署安装command.connection.changedatabase(databasename)
68Asp.net与SQL一起打包部署安装
69Asp.net与SQL一起打包部署安装try
70Asp.net与SQL一起打包部署安装
71Asp.net与SQL一起打包部署安装command.executenonquery()
72Asp.net与SQL一起打包部署安装
73Asp.net与SQL一起打包部署安装finally
74Asp.net与SQL一起打包部署安装
75Asp.net与SQL一起打包部署安装'closeConnection
76Asp.net与SQL一起打包部署安装
77Asp.net与SQL一起打包部署安装command.connection.close()
78Asp.net与SQL一起打包部署安装
79Asp.net与SQL一起打包部署安装endTry
80Asp.net与SQL一起打包部署安装
81Asp.net与SQL一起打包部署安装endSub

82Asp.net与SQL一起打包部署安装
83Asp.net与SQL一起打包部署安装Asp.net与SQL一起打包部署安装publicOverridesSubInstall()SubInstall(ByValstateSaverAsSystem.Collections.IDictionary)
84Asp.net与SQL一起打包部署安装MyBase.Install(stateSaver)
85Asp.net与SQL一起打包部署安装
86Asp.net与SQL一起打包部署安装'------------------------建立数据库-------------------------------------------------
87Asp.net与SQL一起打包部署安装
88Asp.net与SQL一起打包部署安装try
89Asp.net与SQL一起打包部署安装
90Asp.net与SQL一起打包部署安装dimconnStrAsString=String.Format("datasource={0};userid={1};password={2};persistsecurityinfo=false;packetsize=4096",Me.Context.Parameters.Item("server"),Me.Context.Parameters.Item("user"),Me.Context.Parameters.Item("pwd"))
91Asp.net与SQL一起打包部署安装
92Asp.net与SQL一起打包部署安装'根据输入的数据库名称建立数据库
93Asp.net与SQL一起打包部署安装
94Asp.net与SQL一起打包部署安装executesql(connstr,"master","CREATEDATABASE"+Me.Context.Parameters.Item("dbname"))
95Asp.net与SQL一起打包部署安装
96Asp.net与SQL一起打包部署安装'调用osql执行脚本
97Asp.net与SQL一起打包部署安装
98Asp.net与SQL一起打包部署安装dimsqlProcessAsNewSystem.Diagnostics.Process
99Asp.net与SQL一起打包部署安装
100Asp.net与SQL一起打包部署安装sqlprocess.startinfo.filename="osql.exe"
101Asp.net与SQL一起打包部署安装
102Asp.net与SQL一起打包部署安装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"))
103Asp.net与SQL一起打包部署安装
104Asp.net与SQL一起打包部署安装sqlprocess.startinfo.windowstyle=ProcessWindowStyle.Hidden
105Asp.net与SQL一起打包部署安装
106Asp.net与SQL一起打包部署安装sqlprocess.start()
107Asp.net与SQL一起打包部署安装
108Asp.net与SQL一起打包部署安装sqlprocess.waitforexit()'等待执行
109Asp.net与SQL一起打包部署安装
110Asp.net与SQL一起打包部署安装sqlprocess.close()
111Asp.net与SQL一起打包部署安装
112Asp.net与SQL一起打包部署安装'删除脚本文件
113Asp.net与SQL一起打包部署安装
114Asp.net与SQL一起打包部署安装dimsqlFileInfoAsNewSystem.IO.FileInfo(String.Format("{0}db.sql",Me.Context.Parameters.Item("targetdir")))
115Asp.net与SQL一起打包部署安装
116Asp.net与SQL一起打包部署安装ifsqlFileInfo.ExistsThen
117Asp.net与SQL一起打包部署安装
118Asp.net与SQL一起打包部署安装sqlfileinfo.delete()
119Asp.net与SQL一起打包部署安装
120Asp.net与SQL一起打包部署安装endIf
121Asp.net与SQL一起打包部署安装
122Asp.net与SQL一起打包部署安装catchexAsException
123Asp.net与SQL一起打包部署安装
124Asp.net与SQL一起打包部署安装throwex
125Asp.net与SQL一起打包部署安装
126Asp.net与SQL一起打包部署安装endTry
127Asp.net与SQL一起打包部署安装
128Asp.net与SQL一起打包部署安装
129Asp.net与SQL一起打包部署安装
130Asp.net与SQL一起打包部署安装'---------------------将连接字符串写入Web.config-----------------------------------
131Asp.net与SQL一起打包部署安装
132Asp.net与SQL一起打包部署安装try
133Asp.net与SQL一起打包部署安装
134Asp.net与SQL一起打包部署安装dimFileInfoAsSystem.IO.FileInfo=NewSystem.IO.FileInfo(Me.Context.Parameters.Item("targetdir")&"/web.config")
135Asp.net与SQL一起打包部署安装
136Asp.net与SQL一起打包部署安装ifNotFileInfo.ExistsThen
137Asp.net与SQL一起打包部署安装
138Asp.net与SQL一起打包部署安装throwNewInstallException("没有找到配置文件")
139Asp.net与SQL一起打包部署安装
140Asp.net与SQL一起打包部署安装endIf
141Asp.net与SQL一起打包部署安装
142Asp.net与SQL一起打包部署安装'实例化xml文档
143Asp.net与SQL一起打包部署安装
144Asp.net与SQL一起打包部署安装dimXmlDocumentAsNewSystem.Xml.XmlDocument
145Asp.net与SQL一起打包部署安装
146Asp.net与SQL一起打包部署安装xmldocument.load(fileinfo.fullname)
147Asp.net与SQL一起打包部署安装
148Asp.net与SQL一起打包部署安装
149Asp.net与SQL一起打包部署安装
150Asp.net与SQL一起打包部署安装'查找到appsettings中的节点
151Asp.net与SQL一起打包部署安装
152Asp.net与SQL一起打包部署安装dimNodeAsSystem.Xml.XmlNode
153Asp.net与SQL一起打包部署安装
154Asp.net与SQL一起打包部署安装dimFoundItAsBoolean=False
155Asp.net与SQL一起打包部署安装
156Asp.net与SQL一起打包部署安装forEachNodeInXmlDocument.Item("configuration").Item("appSettings")
157Asp.net与SQL一起打包部署安装
158Asp.net与SQL一起打包部署安装ifNode.Name="add"Then
159Asp.net与SQL一起打包部署安装
160Asp.net与SQL一起打包部署安装ifNode.Attributes.GetNamedItem("key").Value="connString"Then
161Asp.net与SQL一起打包部署安装
162Asp.net与SQL一起打包部署安装'写入连接字符串
163Asp.net与SQL一起打包部署安装
164Asp.net与SQL一起打包部署安装node.attributes.getnameditem("value").value=String.Format("PersistSecurityInfo=False;DataSource={0};InitialCatalog={1};UserID={2};Password={3};PacketSize=4096;Pooling=true;MaxPoolSize=100;MinPoolSize=1",_
165Asp.net与SQL一起打包部署安装
166Asp.net与SQL一起打包部署安装me.context.parameters.item("server"),Me.Context.Parameters.Item("dbname"),Me.Context.Parameters.Item("user"),Me.Context.Parameters.Item("pwd"))
167Asp.net与SQL一起打包部署安装
168Asp.net与SQL一起打包部署安装foundit=True
169Asp.net与SQL一起打包部署安装
170Asp.net与SQL一起打包部署安装endIf
171Asp.net与SQL一起打包部署安装
172Asp.net与SQL一起打包部署安装endIf
173Asp.net与SQL一起打包部署安装
174Asp.net与SQL一起打包部署安装nextNode
175Asp.net与SQL一起打包部署安装
176Asp.net与SQL一起打包部署安装ifNotFoundItThen
177Asp.net与SQL一起打包部署安装
178Asp.net与SQL一起打包部署安装throwNewInstallException("web.Config文件没有包含connString连接字符串设置")
179Asp.net与SQL一起打包部署安装
180Asp.net与SQL一起打包部署安装endIf
181Asp.net与SQL一起打包部署安装
182Asp.net与SQL一起打包部署安装xmldocument.save(fileinfo.fullname)
183Asp.net与SQL一起打包部署安装
184Asp.net与SQL一起打包部署安装catchexAsException
185Asp.net与SQL一起打包部署安装
186Asp.net与SQL一起打包部署安装throwex
187Asp.net与SQL一起打包部署安装
188Asp.net与SQL一起打包部署安装endTry
189Asp.net与SQL一起打包部署安装
190Asp.net与SQL一起打包部署安装endSub

191Asp.net与SQL一起打包部署安装
192Asp.net与SQL一起打包部署安装endClass

193Asp.net与SQL一起打包部署安装
194Asp.net与SQL一起打包部署安装

有点难度的就是那个Process类,它调用了osql.exe程序,来执行sql语句osql -U,-P,,-d,-i。
web.config的修改代码是利用xml的语法实现。不是很难理解。
最后编译生成!如图:
Asp.net与SQL一起打包部署安装

安装界面:如图
Asp.net与SQL一起打包部署安装
哈哈,总算写完了,也不知道有没有写好Asp.net与SQL一起打包部署安装
最后还是感谢李洪根老师和微软的讲师。结合他们的共同的经验,部署起来真的很简单。