Asp.net与SQLserver一起打包部署安装图文教程 |
一.准备必要的文件<pps1. SQL脚本文件,生成以后安装过程中需要的表和存储过程等等; ![]() 生成之后,就暂时把它命名为db.sql(注意大小写) 2.LisenceFile.rtf的安装文件,因为我的系统是个人的,而且free的,所以就没有做 。这个以后也是要用到的 。 二.在自己现有的项目里面创建部署项目: 1. 在“文件”菜单上指向“添加项目”,然后选择“新建项目” 。(图1-2) 2. 在“添加新项目”对话框中,选择“项目类型”窗格中的“安装和部署项目”,然后选择“模板”窗格中的“Web 安装项目” 。在“名称”框中键入 Test Installer 。(图1-3) 3. 单击“确定”关闭对话框 。 4. 项目被添加到解决方案资源管理器中,并且文件系统编辑器打开 。 5. 在“属性”窗口中,选择 ProductName 属性,并键入 GCRM 。 ![]() (1-2) ![]() (图1-3)其他项目的作用可以参考Webcast 三 。将 VbNetTest项目的输出添加到部署项目中(假如你的虚拟目录是SQLANDASPNet) 1. 在“文件系统编辑器”中,选择“Web 应用程序文件夹" 。在“操作”菜单上,指向“添加”,然后选择“项目输出” 。(图1-4) 2. 在“添加项目输出组”对话框中,选择“项目”下拉列表中的“SQLANDASPNet” 。 3. 单击“确定”关闭对话框 。 4. 从列表中选择“主输出”和“内容文件”组,然后单击“确定” 。(图1-5) ![]() (图1-4) ![]() (图1-5)其他的作用可以参考webcast,源文件就是所有项目的的文件 四. 创建自定义安装对话框
(五).创建自定义操作 附/targetdir="[targetdir]\"是安装后的目标路径,为了在dbcustomaction类中获得安装后的路径,我们设置此参数 。 将代码添加到安装程序类中,dbcustomaction.vb类 复制代码 代码如下: 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 "组件设计器生成的代码 " public Sub New()Sub New() mybase.new() 该调用是组件设计器所必需的 initializecomponent() 在 InitializeComponent() 调用之后添加任何初始化 end Sub Installer 重写 dispose 以清理组件列表 。 protected Overloads Overrides Sub Dispose()Sub Dispose(ByVal disposing As Boolean) if disposing Then if Not (components Is 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 执行sql 语句 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) ------------------------建立数据库------------------------------------------------- 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")) 根据输入的数据库名称建立数据库 executesql(connstr, "master", "CREATE DATABASE " + Me.Context.Parameters.Item("dbname")) 调用osql执行脚本 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() 等待执行 sqlprocess.close() 删除脚本文件 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 ---------------------将连接字符串写入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("没有找到配置文件") end If 实例化xml文档 dim XmlDocument As New System.Xml.XmlDocument xmldocument.load(fileinfo.fullname) 查找到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 写入连接字符串 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 文件没有包含connString连接字符串设置") end If xmldocument.save(fileinfo.fullname) catch ex As Exception throw ex end Try end Sub end Class 有点难度的就是那个Process类,它调用了osql.exe程序,来执行sql语句osql -U,-P,,-d,-i 。 安装界面:如图 |