Asp.net與SQL一起打包部署安裝

來源:互聯網
上載者:User
asp.net|打包

借鑒msdn webcasts的Asp.net程式部署和李洪根的一篇文章,然後加上自己的親身體會,把整個SQL和Asp.net(vb.net)一起打包的全過程寫一下。
一.準備必要的檔案
1. SQL指令檔,產生以後安裝過程中需要的表和預存程序等等;

產生之後,就暫時把它命名為db.sql(注意大小寫)
2.LisenceFile.rtf的安裝檔案,因為我的系統是個人的,而且free的,所以就沒有做。這個以後也是要用到的。
二.在自己現有的項目裡面建立部署項目:
1. 在[檔案] 功能表上指向“添加項目”,然後選擇“建立項目”。(圖1-2)
2. 在“添加新項目”對話方塊中,選擇“項目類型”窗格中的“安裝和部署項目”,然後選擇“模板”窗格中的“Web 安裝項目”。在“名稱”框中鍵入 Test Installer。(圖1-3)
3. 單擊“確定”關閉對話方塊。
4. 項目被添加到方案總管中,並且檔案系統編輯器開啟。
5. 在“屬性”視窗中,選擇 ProductName 屬性,並鍵入 GCRM。

(1-2)

(圖1-3)其他項目的作用可以參考Webcast
三。將 VbNetTest項目的輸出添加到部署項目中(假如你的虛擬目錄是SQLANDASPNet)
1. 在“檔案系統編輯器”中,選擇“Web 應用程式檔案夾"。在“操作”菜單上,指向“添加”,然後選擇“項目輸出”。(圖1-4)
2. 在“添加項目輸出組”對話方塊中,選擇“項目”下拉式清單中的“SQLANDASPNet”。
3. 單擊“確定”關閉對話方塊。
4. 從列表中選擇“主輸出”和“內容檔案”組,然後單擊“確定”。(圖1-5)
 
(圖1-4)

(圖1-5)其他的作用可以參考webcast,源檔案就是所有項目的的檔案
四.

建立自訂安裝對話方塊
1. 在方案總管中選擇“Test Installer”項目。在“視圖”菜單上指向“編輯器”,然後選擇“使用者介面”。 (圖1-6)
2. 在使用者介面編輯器中,選擇“安裝”下的“啟動”節點。在“操作”菜單上,選擇“添加對話方塊”。
3. 在“添加對話方塊”對話方塊中,選擇“許可協議”對話方塊,然後單擊“確定”關閉對話方塊。 (註:我沒有添加,因為暫時我還是不需要,需要的要添加一下哦)
4. 在“添加對話方塊”對話方塊中,選擇“文字框 (A)”對話方塊,然後單擊“確定”關閉對話方塊。 (圖1-7)
5. 在“操作”菜單上,選擇“上移”。重複此步驟,直到“文字框 (A)”對話方塊位於“安裝資料夾”節點之上。
6. 在“屬性”視窗中,選擇 BannerText 屬性並鍵入:安裝資料庫.。
7. 選擇 BodyText 屬性並鍵入:安裝程式將在目標機器上安裝資料庫。
8. 選擇 Edit1Label 屬性並鍵入:資料庫名稱:。
9. 選擇 Edit1Property 屬性並鍵入 CUSTOMTEXTA1。
10. 選擇 Edit1Value 屬性並鍵入:GsCrm。
11. 選擇 Edit2Label 屬性並鍵入:伺服器名:。
12. 選擇 Edit2Property 屬性並鍵入 CUSTOMTEXTA2。
13. 選擇 Edit2Value 屬性並鍵入:(local)。
14. 選擇 Edit3Label 屬性並鍵入:使用者名稱:。
15. 選擇 Edit3Value 屬性並鍵入:sa。
16. 選擇 Edit3Property 屬性並鍵入 CUSTOMTEXTA3。
17. 選擇 Edit4Label 屬性並鍵入:密碼:。
18. 選擇 Edit4Property 屬性並鍵入 CUSTOMTEXTA4。
19. 選擇 Edit2Visible、Edit3Visible ,並將它們設定為 False。(圖1-8)



(圖1-7)


(圖1-8)

(五).建立自訂動作
1. 在方案總管中選擇“Test Installer”項目。在“視圖”菜單上指向“編輯器”,然後選擇“自訂動作”。(圖1-9)
2. 在自訂動作編輯器中選擇“安裝”節點。在“操作”菜單上,選擇“添加自訂動作”。
3. 在“選擇項目中的項”對話方塊中,雙擊“應用程式檔案夾”。
4. 選擇“主輸出來自 DBCustomAction(活動)”項,然後單擊“確定”關閉對話方塊。
5. 在“屬性”視窗中,選擇 CustomActionData 屬性並鍵入 /dbname=[CUSTOMTEXTA1] /server=[CUSTOMTEXTA2] /user=[CUSTOMTEXTA3] /pwd=[CUSTOMTEXTA4] /targetdir="[TARGETDIR]\"。 (圖1-10)

附/targetdir="[targetdir]\"是安裝後的目標路徑,為了在dbcustomaction類中獲得安裝後的路徑,我們設定此參數。
另外,安裝後的路徑也可以通過Reflection得到:
Dim Asm As System.Reflection.Assembly = _
System.Reflection.Assembly.GetExecutingAssembly
MsgBox("Asm.Location")

(圖1-9)

(圖1-10)
呵呵,已經好多了,剩下來的是關鍵性步驟,我花了好多時間研究。
(六)建立安裝程式類
1. 在[檔案] 功能表上指向“建立”,然後選擇“項目”。
2. 在“建立項目”對話方塊中,選擇“項目類型”窗格中的“Visual Basic 項目”,然後選擇“模板”窗格中的“類庫”。在“名稱”框中鍵入 DBCustomAction。
3. 單擊“開啟”關閉對話方塊。
4. 從“項目”菜單中選擇“添加新項”。
5. 在“添加新項”對話方塊中選擇“安裝程式類”。在“名稱”框中鍵入 DBCustomAction。
6. 單擊“確定”關閉對話方塊。(圖1-11,1-12)
注:這裡是在原來的項目上建立一個簡單的安裝檔案就可以了。

(圖1-11)
 添加後的效果圖:

(圖1-12)這裡的sql檔案是要等一下添加的
(七)
添加檔案
1. 將SQL Server產生的指令檔db.sql添加到“Test Installer”項目(圖1-12)
2. 將安裝檔案LisenceFile.rtf添加到“Test Installer”項目
3. 在使用者介面編輯器中,選擇許可協議,設定LisenceFile屬性為LisenceFile.rtf檔案
(八)
一下的代碼是整個部署的最重要的一部分了

將代碼添加到安裝程式類中,dbcustomaction.vb類

Imports System.ComponentModelimports System.Configuration.Installimports System.IOimports System.Reflection  Public Class DBCustomActionClass DBCustomActioninherits 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 Thenif Not (components Is Nothing) Thencomponents.dispose()end Ifend Ifmybase.dispose(disposing)end Subprivate components As System.ComponentModel.IContainer 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)trycommand.executenonquery()finally'close Connectioncommand.connection.close()end Tryend Subpublic Overrides Sub Install()Sub Install(ByVal stateSaver As System.Collections.IDictionary)MyBase.Install(stateSaver)' ------------------------建立資料庫-------------------------------------------------trydim 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"))'調用osql執行指令碼dim sqlProcess As New System.Diagnostics.Processsqlprocess.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.Hiddensqlprocess.start()sqlprocess.waitforexit() '等待執行sqlprocess.close()'刪除指令檔dim sqlFileInfo As New System.IO.FileInfo(String.Format("{0}db.sql", Me.Context.Parameters.Item("targetdir")))if sqlFileInfo.Exists Thensqlfileinfo.delete()end Ifcatch ex As Exceptionthrow exend Try ' ---------------------將連接字串寫入Web.config-----------------------------------trydim FileInfo As System.IO.FileInfo = New System.IO.FileInfo(Me.Context.Parameters.Item("targetdir") & "\web.config")if Not FileInfo.Exists Thenthrow New InstallException("沒有找到設定檔")end If'執行個體化xml文檔dim XmlDocument As New System.Xml.XmlDocumentxmldocument.load(fileinfo.fullname) '尋找到appsettings中的節點dim Node As System.Xml.XmlNodedim FoundIt As Boolean = Falsefor Each Node In XmlDocument.Item("configuration").Item("appSettings")if Node.Name = "add" Thenif 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 = Trueend Ifend Ifnext Nodeif Not FoundIt Thenthrow New InstallException("web.Config 檔案沒有包含connString連接字串設定")end Ifxmldocument.save(fileinfo.fullname)catch ex As Exceptionthrow exend Tryend Subend Class

有點難度的就是那個process類,它調用了osql.exe程式,來執行sql語句osql -U,-P,,-d,-i。
web.config的修改代碼是利用xml的文法實現。不是很難理解。
最後編譯產生!如圖:

安裝介面:如圖

哈哈,總算寫完了,也不知道有沒有寫好。
最後還是感謝李洪根老師和微軟的講師。結合他們的共同的經驗,部署起來真的很簡單。

 


相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.