但是,這樣製作的安裝程序,只是將Web頁和ASP.NET程序編譯的DLL文件安裝到目標機器的IIS目錄,對於一般的應用程序是可以的(比如用Access數據庫,可以一起打包到安裝程序中);如果數據庫是SQL SERVER,需要在部署的時候一併安裝數據庫,安裝程序的製作就會複雜一些,需要我們自定義安裝程序類。在安裝程序類中執行SQL腳本並將連接字符串寫入Web.config。
l 安裝數據庫
微軟MSDN上介紹過在部署應用程序的時候建立數據庫。如:
這種方法是創建一個安裝程序類,在安裝程序類中調用ADO.NET執行SQL 語句(SQL語句放在一個文本文件中)來創建數據庫。
<?xml:namespace prefix = o ns = "urn:schemas-microsoft-com:office:office" />
但是,這種方法有一個問題,如果用SQL Server2000生成了所有建表、視圖、存儲過程的一個腳本文件,用ADO.NET來執行這個腳本文件,就會因爲腳本中有許多“GO”語句而出現錯誤。當然,我們可以把“GO”替換成換行符,利用ADO.NET一條條執行SQL 語句。很顯然,這樣的效率比較低。
最好的辦法是調用osql執行腳本。(或者創建一個數據庫項目的cmd文件,而cmd文件建立數據庫的時候也是調用的osql)。
首先,我們新建一個ASP.NET Web應用程序,http://localhost/VbNetTest,並打開VbNetTest 項目
創建部署項目Imports System.ComponentModel
Imports System.Configuration.Install
Imports System.IO
Imports System.Reflection
<RunInstaller(True)> Public Class DBCustomAction
Inherits System.Configuration.Install.Installer
#Region "組件設計器生成的代碼 "
Public Sub New()
MyBase.New()
'該調用是組件設計器所必需的
InitializeComponent()
'在 InitializeComponent() 調用之後添加任何初始化
End Sub
' Installer 重寫 dispose 以清理組件列表。
Protected Overloads Overrides 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()
End Sub
#End Region
'執行SQL 語句
Private 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(ByVal stateSaver As System.Collections.IDictionary)MyBase.Install(stateSaver)
' ------------------------建立數據庫-------------------------------------------------
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"))
'根據輸入的數據庫名稱建立數據庫
ExecuteSql(connStr, "master", "CREATE DATABASE " + Me.Context.Parameters.Item("dbname"))
'調用osql執行腳本
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() '等待執行
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中的節點
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連接字符串設置")
End If
XmlDocument.Save(FileInfo.FullName)
Catch ex As Exception
Throw ex
End Try
End Sub
End Class
最後編譯生成!