如何用代码实现ASP.NET安装部署
发表于:2025-11-07 作者:千家信息网编辑
千家信息网最后更新 2025年11月07日,如何用代码实现ASP.NET安装部署,相信很多没有经验的人对此束手无策,为此本文总结了问题出现的原因和解决方法,通过这篇文章希望你能解决这个问题。ASP.NET安装部署代码实现之添加文件简介:1. 将
千家信息网最后更新 2025年11月07日如何用代码实现ASP.NET安装部署
如何用代码实现ASP.NET安装部署,相信很多没有经验的人对此束手无策,为此本文总结了问题出现的原因和解决方法,通过这篇文章希望你能解决这个问题。
ASP.NET安装部署代码实现之添加文件简介:
1. 将SQL Server生成的脚本文件db.sql添加到"Test Installer"项目
2. 将安装文件LisenceFile.rtf添加到"Test Installer"项目
3. 在用户界面编辑器中,选择许可协议,设置LisenceFile属性为LisenceFile.rtf文件
以下的ASP.NET安装部署代码实现是整个部署的最重要的一部分了
将代码添加到安装程序类中,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) ' ----------ASP.NET安装部署代码实现建立数据库------------ 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")) 'ASP.NET安装部署代码实现之调用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() 'ASP.NET安装部署代码实现之删除脚本文件 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 ' -ASP.NET安装部署代码实现之将连接字符串写入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 'ASP.NET安装部署代码实现之写入连接字符串 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。
web.config的修改代码是利用xml的语法实现。不是很难理解。
***编译生成!如图:

安装界面:如图
看完上述内容,你们掌握如何用代码实现ASP.NET安装部署的方法了吗?如果还想学到更多技能或想了解更多相关内容,欢迎关注行业资讯频道,感谢各位的阅读!
代码
文件
字符
字符串
数据
数据库
组件
脚本
生成
内容
方法
更多
界面
程序
语句
问题
项目
如图
设计
重要
数据库的安全要保护哪些东西
数据库安全各自的含义是什么
生产安全数据库录入
数据库的安全性及管理
数据库安全策略包含哪些
海淀数据库安全审计系统
建立农村房屋安全信息数据库
易用的数据库客户端支持安全管理
连接数据库失败ssl安全错误
数据库的锁怎样保障安全
邢台嵌入式软件开发招聘
crm营销的关键是数据库应用
用知网查重会进入数据库吗
删除数据库表s的
商务数据库综合程序设计实验报告
网络安全周黑板报内容
测试网络安全知识
怎样连服务器ip
非关系数据库相同字段
昌平区软件开发范围
苏州计算机软件开发需要多少钱
主要软件开发商介绍
河北外国语学院网络技术专业
网络安全手抄报幼儿图片
hp服务器proc
山东省通信网络安全中心电话
灵石天气预报软件开发
欣达网络技术有限公司
可以代替数据库的网站
微信小程序上传图片到服务器失败
数据库营销的分析报告
坦克世界登陆显示服务器忙
公网连接服务器远程管理卡
黄浦区信息软件开发服务收费
湖南板材套料软件开发商
无线网络安全设置怎么设置
服务器图片福字图片
c 根据数据库生成实体类
梦幻手游纵歌长安服务器有人玩吗
华为查找设备服务器