安装程序类(VB.net,新建数据库,修改webconfig文件)
把asp.net2.0网站打包做成安装程序时,有时需要安装数据库(适当修改webconfig文件)
以下是用到的安装程序类。。。。
(当然还需要其他不少东西,这里就不写了。。。)
详细过程见:http://blog.csdn.net/sunearlier/archive/2006/06/25/830805.aspx
Imports System.ComponentModel
Imports System.Configuration.Install
Imports System.IO
Imports System.Reflection
<RunInstaller(True)> Public Class DBCustomAction
Inherits System.Configuration.Install.Installer
组件设计器生成的代码
'执行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("connectionStrings")
If Node.Name = "add" Then
If Node.Attributes.GetNamedItem("name").Value = "richs_helpConnectionString" Then
'写入连接字符串
Node.Attributes.GetNamedItem("connectionString").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
Imports System.Configuration.Install
Imports System.IO
Imports System.Reflection
<RunInstaller(True)> Public Class DBCustomAction
Inherits System.Configuration.Install.Installer
组件设计器生成的代码
'执行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("connectionStrings")
If Node.Name = "add" Then
If Node.Attributes.GetNamedItem("name").Value = "richs_helpConnectionString" Then
'写入连接字符串
Node.Attributes.GetNamedItem("connectionString").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