創(chuàng)建自定義安裝對話框
1. 在解決方案資源管理器中選擇“Test Installer”項目。在“視圖”菜單上指向“編輯器”,然后選擇“用戶界面”。 (圖1-6)
2. 在用戶界面編輯器中,選擇“安裝”下的“啟動”節(jié)點。在“操作”菜單上,選擇“添加對話框”。
3. 在“添加對話框”對話框中,選擇“許可協(xié)議”對話框,然后單擊“確定”關(guān)閉對話框。 (注:我沒有添加,因為暫時我還是不需要,需要的要添加一下哦)
4. 在“添加對話框”對話框中,選擇“文本框 (A)”對話框,然后單擊“確定”關(guān)閉對話框。 (圖1-7)
5. 在“操作”菜單上,選擇“上移”。重復(fù)此步驟,直到“文本框 (A)”對話框位于“安裝文件夾”節(jié)點之上。
6. 在“屬性”窗口中,選擇 BannerText 屬性并鍵入:安裝數(shù)據(jù)庫.。
7. 選擇 BodyText 屬性并鍵入:安裝程序?qū)⒃谀繕?biāo)機器上安裝數(shù)據(jù)庫。
8. 選擇 Edit1Label 屬性并鍵入:數(shù)據(jù)庫名稱:。
9. 選擇 Edit1Property 屬性并鍵入 CUSTOMTEXTA1。
10. 選擇 Edit1Value 屬性并鍵入:GsCrm。
11. 選擇 Edit2Label 屬性并鍵入:服務(wù)器名:。
12. 選擇 Edit2Property 屬性并鍵入 CUSTOMTEXTA2。
13. 選擇 Edit2Value 屬性并鍵入:(local)。
14. 選擇 Edit3Label 屬性并鍵入:用戶名:。
15. 選擇 Edit3Value 屬性并鍵入:sa。
16. 選擇 Edit3Property 屬性并鍵入 CUSTOMTEXTA3。
17. 選擇 Edit4Label 屬性并鍵入:密碼:。
18. 選擇 Edit4Property 屬性并鍵入 CUSTOMTEXTA4。
19. 選擇 Edit2Visible、Edit3Visible ,并將它們設(shè)置為 False。(圖1-8)
(圖1-7)
(圖1-8)
(五).創(chuàng)建自定義操作
1. 在解決方案資源管理器中選擇“Test Installer”項目。在“視圖”菜單上指向“編輯器”,然后選擇“自定義操作”。(圖1-9)
2. 在自定義操作編輯器中選擇“安裝”節(jié)點。在“操作”菜單上,選擇“添加自定義操作”。
3. 在“選擇項目中的項”對話框中,雙擊“應(yīng)用程序文件夾”。
4. 選擇“主輸出來自 DBCustomAction(活動)”項,然后單擊“確定”關(guān)閉對話框。
5. 在“屬性”窗口中,選擇 CustomActionData 屬性并鍵入 /dbname=[CUSTOMTEXTA1] /server=[CUSTOMTEXTA2] /user=[CUSTOMTEXTA3] /pwd=[CUSTOMTEXTA4] /targetdir="[TARGETDIR]\"。 (圖1-10)
附/targetdir="[targetdir]\"是安裝后的目標(biāo)路徑,為了在dbcustomaction類中獲得安裝后的路徑,我們設(shè)置此參數(shù)。
另外,安裝后的路徑也可以通過Reflection得到:
Dim Asm As System.Reflection.Assembly = _
System.Reflection.Assembly.GetExecutingAssembly
MsgBox("Asm.Location")
(圖1-9)
(圖1-10)
呵呵,已經(jīng)好多了,剩下來的是關(guān)鍵性步驟,我花了好多時間研究。
(六)創(chuàng)建安裝程序類
1. 在“文件”菜單上指向“新建”,然后選擇“項目”。
2. 在“新建項目”對話框中,選擇“項目類型”窗格中的“Visual Basic 項目”,然后選擇“模板”窗格中的“類庫”。在“名稱”框中鍵入 DBCustomAction。
3. 單擊“打開”關(guān)閉對話框。
4. 從“項目”菜單中選擇“添加新項”。
5. 在“添加新項”對話框中選擇“安裝程序類”。在“名稱”框中鍵入 DBCustomAction。
6. 單擊“確定”關(guān)閉對話框。(圖1-11,1-12)
注:這里是在原來的項目上建立一個簡單的安裝文件就可以了。
(圖1-11)
添加后的效果圖:
(圖1-12)這里的sql文件是要等一下添加的
(七)
添加文件
1. 將SQL Server生成的腳本文件db.sql添加到“Test Installer”項目(圖1-12)
2. 將安裝文件LisenceFile.rtf添加到“Test Installer”項目
3. 在用戶界面編輯器中,選擇許可協(xié)議,設(shè)置LisenceFile屬性為LisenceFile.rtf文件
(八)
一下的代碼是整個部署的最重要的一部分了
將代碼添加到安裝程序類中,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
組件設(shè)計器生成的代碼#region "組件設(shè)計器生成的代碼 "
public Sub New()Sub New()
mybase.new()
'該調(diào)用是組件設(shè)計器所必需的
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)
' ------------------------建立數(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"))
'調(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()
'刪除腳本文件
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中的節(jié)點
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連接字符串設(shè)置")
end If
xmldocument.save(fileinfo.fullname)
catch ex As Exception
throw ex
end Try
end Sub
end Class
有點難度的就是那個Process類,它調(diào)用了osql.exe程序,來執(zhí)行sql語句osql -U,-P,,-d,-i。
web.config的修改代碼是利用xml的語法實現(xiàn)。不是很難理解。
最后編譯生成!如圖:
安裝界面:如圖
哈哈,總算寫完了,也不知道有沒有寫好。
最后還是感謝李洪根老師和微軟的講師。結(jié)合他們的共同的經(jīng)驗,部署起來真的很簡單。
聲明:本網(wǎng)頁內(nèi)容旨在傳播知識,若有侵權(quán)等問題請及時與本網(wǎng)聯(lián)系,我們將在第一時間刪除處理。TEL:177 7030 7066 E-MAIL:11247931@qq.com