您好,登錄后才能下訂單哦!
如何用代碼實(shí)現(xiàn)ASP.NET安裝部署,相信很多沒有經(jīng)驗(yàn)的人對此束手無策,為此本文總結(jié)了問題出現(xiàn)的原因和解決方法,通過這篇文章希望你能解決這個(gè)問題。
ASP.NET安裝部署代碼實(shí)現(xiàn)之添加文件簡介:
1. 將SQL Server生成的腳本文件db.sql添加到“Test Installer”項(xiàng)目
2. 將安裝文件LisenceFile.rtf添加到“Test Installer”項(xiàng)目
3. 在用戶界面編輯器中,選擇許可協(xié)議,設(shè)置LisenceFile屬性為LisenceFile.rtf文件
以下的ASP.NET安裝部署代碼實(shí)現(xiàn)是整個(gè)部署的最重要的一部分了
將代碼添加到安裝程序類中,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 "組件設(shè)計(jì)器生成的代碼 " public Sub New()Sub New() mybase.new() '該調(diào)用是組件設(shè)計(jì)器所必需的 initializecomponent() '在 InitializeComponent() 調(diào)用之后添加任何初始化 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 '執(zhí)行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安裝部署代碼實(shí)現(xiàn)建立數(shù)據(jù)庫------------ 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")) '根據(jù)輸入的數(shù)據(jù)庫名稱建立數(shù)據(jù)庫 executesql(connstr, "master", "CREATE DATABASE " + Me.Context.Parameters.Item("dbname")) 'ASP.NET安裝部署代碼實(shí)現(xiàn)之調(diào)用osql執(zhí)行腳本 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() '等待執(zhí)行 sqlprocess.close() 'ASP.NET安裝部署代碼實(shí)現(xiàn)之刪除腳本文件 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安裝部署代碼實(shí)現(xiàn)之將連接字符串寫入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 '實(shí)例化xml文檔 dim XmlDocument As New System.Xml.XmlDocument xmldocument.load(fileinfo.fullname) '查找到appsettings中的節(jié)點(diǎn) 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安裝部署代碼實(shí)現(xiàn)之寫入連接字符串 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連接字符串設(shè)置") end If xmldocument.save(fileinfo.fullname) catch ex As Exception throw ex end Try end Sub end Class
有點(diǎn)難度的就是那個(gè)Process類,它調(diào)用了osql.exe程序,來執(zhí)行sql語句osql -U,-P,,-d,-i。
web.config的修改代碼是利用xml的語法實(shí)現(xiàn)。不是很難理解。
***編譯生成!如圖:
安裝界面:如圖
看完上述內(nèi)容,你們掌握如何用代碼實(shí)現(xiàn)ASP.NET安裝部署的方法了嗎?如果還想學(xué)到更多技能或想了解更多相關(guān)內(nèi)容,歡迎關(guān)注億速云行業(yè)資訊頻道,感謝各位的閱讀!
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。