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类

  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安装部署之创建安装程序类
责任编辑:仲衡 来源: 博客园
相关推荐

2009-07-29 16:31:39

ASP.NET安装部署

2009-08-05 16:59:55

ASP.NET组件设计

2009-08-10 16:37:26

ASP.NET安装部署

2009-07-23 13:09:23

2009-07-23 13:19:51

2009-08-10 17:04:42

ASP.NET安装部署

2009-08-10 16:14:26

ASP.NET安装部署

2009-08-04 17:16:16

ASP.NET代码优化

2009-08-10 18:19:37

ASP.NET安装环境

2009-08-10 18:43:05

ASP.NET安装步骤

2009-08-05 10:16:54

部署ASP.NET应用

2009-08-10 16:58:45

ASP.NET安装部署

2009-08-10 16:50:34

ASP.NET安装部署

2009-08-12 14:10:37

asp.net分页代码

2009-07-22 17:45:35

ASP.NET教程

2009-07-28 17:17:19

ASP.NET概述

2009-08-03 14:22:33

什么是ASP.NET

2009-08-04 11:29:14

HTML代码ASP.NET控件

2009-07-23 10:08:24

asp.net mvc

2009-07-28 09:45:34

Webpart部署方式使用ASP.NET
点赞
收藏

51CTO技术栈公众号