ASP.NET安装部署代码实现
创始人
2024-04-27 09:40:37
0

ASP.NET安装部署代码实现之添加文件简介:

1. 将SQL Server生成的脚本文件db.sql添加到“Test Installer”项目

2. 将安装文件LisenceFile.rtf添加到“Test Installer”项目

3. 在用户界面编辑器中,选择许可协议,设置LisenceFile属性为LisenceFile.rtf文件

以下的ASP.NET安装部署代码实现是整个部署的最重要的一部分了

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

  1. Imports System.ComponentModel  
  2.  
  3. imports System.Configuration.Install  
  4.  
  5. imports System.IO  
  6.  
  7. imports System.Reflection  
  8.  
  9. ﹤runinstaller(true)﹥ Public Class DBCustomActionClass DBCustomAction  
  10.  
  11. inherits System.Configuration.Install.Installer  
  12.  
  13.  
  14. #region "组件设计器生成的代码 "  
  15.  
  16. public Sub New()Sub New()  
  17.  
  18. mybase.new()  
  19.  
  20. '该调用是组件设计器所必需的  
  21.  
  22. initializecomponent()  
  23.  
  24. '在 InitializeComponent() 调用之后添加任何初始化  
  25.  
  26. end Sub  
  27.  
  28. ' Installer 重写 dispose 以清理组件列表。  
  29.  
  30. protected Overloads Overrides Sub Dispose()Sub Dispose(ByVal disposing As Boolean)  
  31.  
  32. if disposing Then  
  33.  
  34. if Not (components Is Nothing) Then  
  35.  
  36. components.dispose()  
  37.  
  38.  end If  
  39.  
  40. end If  
  41.  
  42. mybase.dispose(disposing)  
  43.  
  44. end Sub  
  45.  
  46. private components As System.ComponentModel.IContainer  
  47.  
  48. ﹤system.diagnostics.debuggerstepthrough()﹥   
  49. Private Sub InitializeComponent()Sub InitializeComponent()  
  50.  
  51. end Sub  
  52.  
  53. #end Region  
  54.  
  55. '执行sql 语句  
  56.  
  57. private Sub ExecuteSql()Sub ExecuteSql(ByVal conn As String,   
  58. ByVal DatabaseName As String, ByVal Sql As String)  
  59.  
  60. dim mySqlConnection As New SqlClient.SqlConnection(conn)  
  61.  
  62. dim Command As New SqlClient.SqlCommand(Sql, mySqlConnection)  
  63.  
  64. command.connection.open()  
  65.  
  66. command.connection.changedatabase(databasename)  
  67.  
  68. try 
  69.  
  70. command.executenonquery()  
  71.  
  72. finally 
  73.  
  74. 'close Connection  
  75.  
  76. command.connection.close()  
  77.  
  78. end Try  
  79.  
  80. end Sub  
  81.  
  82. public Overrides Sub Install()Sub Install(ByVal stateSaver   
  83. As System.Collections.IDictionary)  
  84. MyBase.Install(stateSaver)  
  85.  
  86. ' ----------ASP.NET安装部署代码实现建立数据库------------  
  87.  
  88. try 
  89.  
  90. dim connStr As String = String.Format("data source={0};  
  91. user id={1};password={2};  
  92. persist security info=false;packet size=4096",   
  93. Me.Context.Parameters.Item("server"),   
  94. Me.Context.Parameters.Item("user"),   
  95. Me.Context.Parameters.Item("pwd"))  
  96.  
  97. '根据输入的数据库名称建立数据库  
  98.  
  99. executesql(connstr, "master",   
  100. "CREATE DATABASE " + Me.Context.Parameters.Item("dbname"))  
  101.  
  102. 'ASP.NET安装部署代码实现之调用osql执行脚本  
  103.  
  104. dim sqlProcess As New System.Diagnostics.Process  
  105.  
  106. sqlprocess.startinfo.filename = "osql.exe " 
  107.  
  108. sqlprocess.startinfo.arguments = String.Format(" -U {0} -P {1} -d {2} -i {3}db.sql",   
  109. Me.Context.Parameters.Item("user"), Me.Context.Parameters.Item("pwd"),   
  110. Me.Context.Parameters.Item("dbname"), Me.Context.Parameters.Item("targetdir"))  
  111.  
  112. sqlprocess.startinfo.windowstyle = ProcessWindowStyle.Hidden  
  113.  
  114. sqlprocess.start()  
  115.  
  116. sqlprocess.waitforexit() '等待执行  
  117.  
  118. sqlprocess.close()  
  119.  
  120. 'ASP.NET安装部署代码实现之删除脚本文件  
  121.  
  122. dim sqlFileInfo As New System.IO.FileInfo(String.Format("{0}db.sql",   
  123. Me.Context.Parameters.Item("targetdir")))  
  124.  
  125. if sqlFileInfo.Exists Then  
  126.  
  127. sqlfileinfo.delete()  
  128.  
  129. end If  
  130.  
  131. catch ex As Exception  
  132.  
  133. throw ex  
  134.  
  135. end Try  
  136.  
  137.  
  138.  
  139. ' -ASP.NET安装部署代码实现之将连接字符串写入Web.config--  
  140.  
  141. try 
  142.  
  143. dim FileInfo As System.IO.FileInfo = New System.IO.  
  144. FileInfo(Me.Context.Parameters.Item("targetdir") & "\web.config")  
  145.  
  146. if Not FileInfo.Exists Then  
  147.  
  148. throw New InstallException("没有找到配置文件")  
  149.  
  150. end If  
  151.  
  152. '实例化xml文档  
  153.  
  154. dim XmlDocument As New System.Xml.XmlDocument  
  155.  
  156. xmldocument.load(fileinfo.fullname)  
  157.  
  158. '查找到appsettings中的节点  
  159.  
  160. dim Node As System.Xml.XmlNode  
  161.  
  162. dim FoundIt As Boolean = False  
  163.  
  164. for Each Node In XmlDocument.Item("configuration").Item("appSettings")  
  165.  
  166. if Node.Name = "add" Then  
  167.  
  168. if Node.Attributes.GetNamedItem("key").Value = "connString" Then  
  169.  
  170. 'ASP.NET安装部署代码实现之写入连接字符串  
  171.  
  172. node.attributes.getnameditem("value").value = String.  
  173. Format("Persist Security Info=False;Data Source={0};  
  174. Initial Catalog={1};User ID={2};Password={3};  
  175. Packet Size=4096;Pooling=true;Max Pool Size=100;  
  176. Min Pool Size=1", _  
  177.  
  178. me.context.parameters.item("server"),   
  179. Me.Context.Parameters.Item("dbname"),   
  180. Me.Context.Parameters.Item("user"),   
  181. Me.Context.Parameters.Item("pwd"))  
  182.  
  183. foundit = True  
  184.  
  185. end If  
  186.  
  187. end If  
  188.  
  189. next Node  
  190.  
  191. if Not FoundIt Then  
  192.  
  193. throw New InstallException("web.Config 文件没有包含connString连接字符串设置")  
  194.  
  195. end If  
  196.  
  197. xmldocument.save(fileinfo.fullname)  
  198.  
  199. catch ex As Exception  
  200.  
  201. throw ex  
  202.  
  203. end Try  
  204.  
  205. end Sub  
  206.  
  207. end Class 

有点难度的就是那个Process类,它调用了osql.exe程序,来执行sql语句osql -U,-P,,-d,-i。

web.config的修改代码是利用xml的语法实现。不是很难理解。

***编译生成!如图:

编译生成图 

安装界面:如图

安装界面图

ASP.NET安装部署代码实现的基本情况就向你介绍到这里,希望对你学习ASP.NET安装部署有所帮助。

【编辑推荐】

  1. ASP.NET安装部署之创建项目详细图解
  2. ASP.NET安装部署之导入项目图解
  3. ASP.NET安装部署之创建对话框图解
  4. ASP.NET安装部署之创建自定义操作
  5. ASP.NET安装部署之创建安装程序类

相关内容

热门资讯

如何允许远程连接到MySQL数... [[277004]]【51CTO.com快译】默认情况下,MySQL服务器仅侦听来自localhos...
如何利用交换机和端口设置来管理... 在网络管理中,总是有些人让管理员头疼。下面我们就将介绍一下一个网管员利用交换机以及端口设置等来进行D...
施耐德电气数据中心整体解决方案... 近日,全球能效管理专家施耐德电气正式启动大型体验活动“能效中国行——2012卡车巡展”,作为该活动的...
20个非常棒的扁平设计免费资源 Apple设备的平面图标PSD免费平板UI 平板UI套件24平图标Freen平板UI套件PSD径向平...
德国电信门户网站可实时显示全球... 德国电信周三推出一个门户网站,直观地实时提供其安装在全球各地的传感器网络检测到的网络攻击状况。该网站...
为啥国人偏爱 Mybatis,... 关于 SQL 和 ORM 的争论,永远都不会终止,我也一直在思考这个问题。昨天又跟群里的小伙伴进行...
《非诚勿扰》红人闫凤娇被曝厕所... 【51CTO.com 综合消息360安全专家提醒说,“闫凤娇”、“非诚勿扰”已经被黑客盯上成为了“木...
2012年第四季度互联网状况报... [[71653]]  北京时间4月25日消息,据国外媒体报道,全球知名的云平台公司Akamai Te...