天天看點

用VS2005制造WEB安裝程式

環境:windows2003 + vs2005+sqlserver2000

 1、打開VS2005,打開工程或網站(這裡的示例是一個已釋出的網站系統).  

2、從“解決方案資料總管中”右鍵點選解決方案名稱,選擇“添加”――“建立項目”,從打開的“添加新項目”視窗,在“項目類型”欄選擇“其他項目類型”――“安裝部署”,在模闆欄,選“web項目”,在下面的名稱和位址欄輸入名稱和位址。

3、生成sql檔案(隻生成建立資料表的SQL語句)

打開SQLSER企業管理器,登入,選擇資料庫――選擇要生成SQL語句的表,右鍵――所有任務――生成SQL腳本。在打開的視窗中點選“确定”,儲存SQL腳本(注意:sql檔案一定要用小寫的英文名稱命名,儲存完成後把SQL檔案另存為TXT檔案,并把裡面的所有的“GO”去掉。)

4、建立安裝程式類庫和安裝程式類

從“解決方案資料總管中”右鍵點選解決方案名稱,選擇“添加”――“建立項目”,從打開的“添加新項目”視窗,在“項目類型”欄選擇“VISAL BASIC”,在模闆欄,選“類庫”,在下面的名稱和位址欄輸入名稱和位址。點選“确定”。

5、從“解決方案資料總管中”點選剛生成的類庫,删除自動生成的class1.vb類,右鍵點選類庫名稱――添加――建立項,選擇“安裝程式類”,輸入名稱,點選“添加”按鈕。

6、打開剛生成的安裝程式類,在dbtest.vb中,添加如下代碼

Imports System.ComponentModel

Imports System.Configuration.Install

Imports System.IO

Imports System.Reflection

Public Class dbtest

    '聲明私有變量

    Private sqlConnection1 As SqlClient.SqlConnection

    Private ServerName As String

    Private AdminName As String

    Private AdminPwd As String

    Public Sub New()

        MyBase.New()

        '元件設計器需要此調用。

        InitializeComponent()

        '調用 InitializeComponent 後添加初始化代碼

    End Sub

    Private Function GetSql(ByVal Name As String) As String

        Try

            ' Gets the current assembly.

            Dim Asm As [Assembly] = [Assembly].GetExecutingAssembly()

            ' Resources are named using a fully qualified name.

            Dim strm As Stream = Asm.GetManifestResourceStream( _

              Asm.GetName().Name + "." + Name)

            ' Reads the contents of the embedded file.

            Dim reader As StreamReader = New StreamReader(strm)

            Return reader.ReadToEnd()

        Catch ex As Exception

            MsgBox("讀取SQL檔案出錯: " & ex.Message)

            Throw ex

        End Try

    End Function

Private Sub ExecuteSql(ByVal DatabaseName As String, ByVal Sql As String)

        Dim Command As New SqlClient.SqlCommand(Sql, sqlConnection1)

        Command.Connection.Open()

        Command.Connection.ChangeDatabase(DatabaseName)

            Command.ExecuteNonQuery()

        Finally

            ' Finally, blocks are a great way to ensure that the connection

            ' is always closed.

            Command.Connection.Close()

    Protected Sub AddDBTable()

            ' 生成資料庫

            ExecuteSql("master", "CREATE DATABASE water")

            ' 根據SQL語句生成表

            ExecuteSql("water", GetSql("watertable.txt"))

            ' Reports any errors and abort.

            MsgBox("生成資料庫錯誤: " & ex.Message)

    Private Sub WriteWebConfig()

        '修改web.config檔案

            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中的節點

            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 = "AdoConnBySql" Then

                        '寫入連接配接字元串

                        Node.Attributes.GetNamedItem("value").Value = String.Format("Provider=SQLOLEDB;Data Source={0};Password={2};User ID={1};Initial Catalog=water", Me.Context.Parameters.Item("server"), 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連接配接字元串設定")

            XmlDocument.Save(FileInfo.FullName)

    ''' <summary>

    ''' 安裝程式入口

    ''' </summary>

    ''' <param name="stateSaver"></param>

    ''' <remarks></remarks>

    Public Overrides Sub Install(ByVal stateSaver As _

        System.Collections.IDictionary)

        MyBase.Install(stateSaver)

        '讀取使用者連接配接資料庫資訊

        ServerName = Trim(Me.Context.Parameters.Item("server"))

        AdminName = Trim(Me.Context.Parameters.Item("user"))

        AdminPwd = Trim(Me.Context.Parameters.Item("pwd"))

        '根據使用者界面輸入的資訊建立資料庫聯接

        sqlConnection1 = New SqlClient.SqlConnection

        sqlConnection1.ConnectionString = "User ID=" + AdminName + ";Data Source = " + ServerName + ";Password=" + AdminPwd + ";Initial Catalog=master"

        'sqlConnection1.Open()

        '添加資料庫中的表

        AddDBTable()

        '修改web.config檔案中的資料庫連接配接串

        WriteWebConfig()

End Class

6、添加SQL腳本檔案到類庫中。

   右鍵點選類庫名稱――添加――現有項,選擇watertable.txt檔案。點選“确定”。

   右鍵選擇剛剛添加的watertable.txt檔案,選“屬性”,把“生成操作”項内容修改為“嵌入的資源”。

7、生成類庫的DLL檔案。

8、右鍵安裝項目名稱――“視圖”――“檔案系統”,在打開的“檔案系統視窗”,右鍵“WEB應用程式檔案夾”――“添加”――項目輸出,

從“項目”下拉清單框中,選擇類庫為“主輸出”,

重複上步操作,選擇網站為“内容檔案”

9、添加安裝時使用者要輸入的資料庫伺服器名稱、使用者名、密碼的使用者界面。

   右鍵安裝項目名稱――視圖――使用者界面,在打開的安裝界面中,右鍵“啟動”,選擇“添加對話框”,在彈出的視窗中,選擇“文本框A”。

右鍵文本框A,選擇“上移”,重複執行,直到在“安裝位址”上。

右鍵“文本框A”――屬性,進行如下修改。

Edit1lable1       資料庫伺服器:

Edit1property     DBSERVERNAME

Edit1visible       true

Edit2lable1       使用者名:

Edit2property     USERNAME

Edit2visible       true

Edit3lable1       使用者名:

Edit3property     PASSWORD

Edit3visible       true

Edit4visible       false

10、添加自定義操作

在“解決方案資料總管中”,右鍵安裝項目名稱――“視圖”――“自定義操作”,在打開的自定義操作視窗,右鍵“安裝”――“添加自定義操作”,在打開的“選擇項目的項”,輕按兩下“WEB應用程式檔案夾”――主輸出來自clslib(活動)。

右鍵“主輸出來自clslib(活動)”――“屬性視窗”,修改屬性

Customactiondata 為

 /server=[DBSERVERNAME] /user=[USERNAME] /pwd=[PASSWORD]  /targetdir="[TARGETDIR]\"

注意:每個變量之間一定要有一個空格做間隔,如果想得到安裝檔案在安裝機器中的路徑,一定要添加  targetdir 變量。

11、添加系統必備

右鍵安裝項目名稱――屬性――選擇系統必備

在打開的系統必備視窗,選擇 “.net framework 2.0”――選擇“從與我的應用程式相同路徑下載下傳系統必備”,點選“确定”

12、生成安裝項目。

從菜單“生成”――“生成websetup”,生成安裝項目。

13、安裝項目界面

從“解決方案資料總管”――安裝項目,右鍵安裝項目名稱――安裝。

生成的資料庫

生成的網站

修改後的web.config

寫在後面的話:

做完程式後,發現用SQLSER的恢複資料庫方法比用SQL檔案要好的多,也友善的多,就做了一個過程,代碼如下:

Private sub RestoreDB()

Dim strdir as string

Dim strSql as string

‘得到安裝程式路徑

Strdir=me.context.parameters.item(“targetdir”)

Strsql="RESTORE DATABASE water FROM DISK = '" + strdir + "data.bak' " +

                " WITH MOVE 'water_data' TO '" + "c:\water.mdf', " +

                " MOVE 'water_log' TO '" + "c:\water_log.ldf' "

Executesql(“master”,strsql)

End sub

注意:data.bak是sqlserver備份資料庫名,也可以沒有字尾,如果沒有字尾,在程式中也不要有,還有備份檔案要和釋出的網站檔案放在一起,water_data、water_log是邏輯資料名,在sqlserver資料庫中的備份恢複中都能檢視到的,TO 後面是把*.mdf和*.ldf檔案複制到一個路徑下,直接寫死就可以了。

本文轉自 牛海彬 51CTO部落格,原文連結:http://blog.51cto.com/newhappy/135713,如需轉載請自行聯系原作者