Vs2003下B/S应用程序安装打包开发小结
.Net 下安装打包主要有三个要点,
1,目标web 应用的发布设定
2,嵌入SQL 脚本进行数据库的创建,赋予初始化参数
3,对于Web 应用程序的web.config配置文件写入Sa或其它用户名及密码
补充一点就是,整合打包,对目标服务器安装环境的检测, 打包时,把.Net Framework也打包进去,生成安装文件。安装PluginInstaller.msi可以在你打包时,把框架打包进去,然后在安装软件时,提醒你是否要安装.Net Framework.。安装PluginInstaller.msi后,再打包编译,在你编译生成的Setup.exe同一文件夹中会出现dotnetfx.exe,langpack.exe等文件,说明框架已经打包进去了”,PluginInstaller.msi的下载地址:http://www.microsoft.com/downloads/details.aspx?FamilyID=
.
难点一,嵌入SQL 脚本进行数据库的创建,赋予初始化参数。
A,读取用用户输入的sa及密码,等数据库服务器,在此以sql server为例,
1,要读取,当然需要有输入框,这个在安装项目上右键—视图—用户界面—显示出了安装的界面的操作树,在启动根上右键—添加对话框,选择合适的文本框后(如文本框A)—文件框A右键—属性—属性窗口则出现了文本框总将出现的输入框属性,以及标题,需要注意的edit1Property,对应着输入框的存放文本框名,
B, 自定义安装模块方法DBCustomAction
1、项目添加一个新项目->选择类库模板->命名为DBCustomAction
2、单击项目右键->添加新项->选择安装程序类(命名为DBCustomAction.cs)这个类是继承自System.Configuration.Install.Installer的类。
主要的VB代码如下
Dim tdir As String
Public docName As String = [String].Empty
Private node As XmlNode = Nothing
Private _configType As Integer
Public Overrides Sub Install(ByVal stateSaver As _
System.Collections.IDictionary)
MyBase.Install(stateSaver)
' AddDBTable(Me.Context.Parameters.Item("dbname"))
strPass = Me.Context.Parameters("strPass") '取得传来的sa密码
tdir = Me.Context.Parameters("targetdir") '取得传来的安装路径
If strPass <> "" Then
AddDBTable("ZWT2007SQLDB")
Else
End If
updateConfig() ‘修改config文件方法
End Sub
Private Function GetSql(ByVal Name As String) As String
Try
' Gets the current assembly.
Dim Asm As [Assembly] = [Assembly].GetExecutingAssembly()
' Resources are named using a fully qualified name.
Dim strm As Stream = Asm.GetManifestResourceStream( _
Asm.GetName().Name + "." + Name)
' Reads the contents of the embedded file.
Dim reader As StreamReader = New StreamReader(strm)
Return reader.ReadToEnd()
Catch ex As Exception
MsgBox("In GetSQL: " & ex.Message)
Throw ex
End Try
End Function
Private Sub ExecuteSql(ByVal DatabaseName As String, ByVal Sql As String)
Dim sqlConnection1 As New SqlClient.SqlConnection
sqlConnection1.ConnectionString = ("user id=sa;password=" + strPass + ";database=master;server=(local)")
Dim Command As New SqlClient.SqlCommand(Sql, sqlConnection1)
Command.Connection.Open()
Command.Connection.ChangeDatabase(DatabaseName)
Try
Command.ExecuteNonQuery()
Finally
' Finally, blocks are a great way to ensure that the connection
' is always closed.
Command.Connection.Close()
End Try
End Sub
Protected Sub AddDBTable(ByVal strDBName As String)
Try
' Creates the database.
ExecuteSql("master", "CREATE DATABASE " + strDBName)
' Creates the tables.
ExecuteSql(strDBName, GetSql("sql.txt")) ‘注意哦,这个是附加到程序中的个txt文件,内容为sql 脚本,项目属性为内嵌资源,不然打包时可能会漏了
Catch ex As Exception
' Reports any errors and abort.
MsgBox("In exception handler: " & ex.Message)
Throw ex
End Try
End Sub
由上可以看到,需要得到sa的密码,必须从安装项目中传来
3,在安装项目中,右键—视图—用户自定义操作—在自定义操作树的,安装根上右键—添加自定义操作—项目—添加输出,来自项目DBCustomAction主输出,并右键此增加项—属性—CoustomActionData中的值设定传送参数值(如, /strPass=[EDITA1] /targetdir="[TARGETDIR]\" , [EDITA1]是1操作中SA输入密码,默认名[TARGETDIR]则为intall模板中安装文件将存放的位置)
难点二, 对于Web 应用程序的web.config配置文件写入Sa或其它用户名及密码
1, 同上操作1,3可以得到TARGETDIR即目标文件发布安装位置后,可以在DBCustomAction方法中增加updateConfig方法即可
2, VB代码如下:
Dim t1 As String = ("server=localhost;database=DBname;uid=sa;pwd=" + Me.strPass)
Dim t2 As String = ("Driver={SQL Server};database=DBname;server=localhost;uid=sa;pwd=" + Me.strPass)
Dim tttdir As String = Me.tdir + "Administrator\Web.config"
SetValue(tttdir, "sqlserver", t1)
SetValue(tttdir, "Adosqlserver", t2)
tttdir = Me.tdir + "Web.config"
SetValue(tttdir, "sqlserver", t1)
SetValue(tttdir, "Adosqlserver", t2)
End Sub
SetValue '实现对appSettings值的修改
saveConfigDoc '写入操作
removeElement '删除appSettings的操作
modifyElement '修改 appSettings
loadConfigDoc '载入appSettings信息