<RunInstaller(True)> Public Class DBCreate
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
Public Overrides Sub Install(ByVal stateSaver As System.Collections.IDictionary)
MyBase.Install(stateSaver)
''''程序集物理路径
Dim Asm As System.Reflection.Assembly = System.Reflection.Assembly.GetExecutingAssembly
Dim strPath As String = Left(Asm.Location, InStrRev(Asm.Location, "\") - 1)
strPath = Left(strPath, InStrRev(strPath, "\"))
'''''数据库连接字段
Dim strKey As String = "Data Source=127.0.0.1;Initial Catalog=Test;User ID=sa;Password=密码"
strKey = Replace(strKey, "sa", Me.Context.Parameters.Item("SqlUserID"))
strKey = Replace(strKey, "密码", Me.Context.Parameters.Item("SqlPWD"))
Dim strSqlDBName As String = Me.Context.Parameters.Item("SqlDBName")
''''恢复设备备份
' Dim sql As String = "RESTORE " + Me.Context.Parameters.Item("SqlDBName") + " FROM DISK = '" + strPath + "TestPro.BAK'"
Dim sql As String = "RESTORE DATABASE " + strSqlDBName + " " _
& "FROM DISK = '" + strPath + "TestPro.BAK' " _
& "WITH MOVE 'Test_Data' TO 'C:\" + strSqlDBName + "_data.mdf', " _
& "MOVE 'Test_log' TO 'C:\" + strSqlDBName + "_log.ldf'"
'''''修改配置文件
Dim FileInfo As System.IO.FileInfo = New System.IO.FileInfo(strPath + "web.config")
If Not FileInfo.Exists Then
Throw New InstallException("没找到配置文件 web.config")
End If
Dim XmlDocument As New System.Xml.XmlDocument
XmlDocument.Load(FileInfo.FullName)
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 ' skip any comments
If Node.Attributes.GetNamedItem("key").Value = "External" Then
Node.Attributes.GetNamedItem("value").Value = Replace(strKey, "Test", Me.Context.Parameters.Item("SqlDBName"))
FoundIt = True
End If
End If
Next Node
If Not FoundIt Then '''
Throw New InstallException("Config file did not contain a ServerName section")
End If
XmlDocument.Save(FileInfo.FullName)
'''修改文件夹的属性为可写入,你可以不用
Dim f2 As New FileIOPermission(FileIOPermissionAccess.Read Or FileIOPermissionAccess.Write, strPath)
f2.AddPathList(FileIOPermissionAccess.Write Or FileIOPermissionAccess.Read, strPath)
End Sub
''''执行Sql语句
Private Sub ExecuteSql(ByVal Conn As String, ByVal Sql As String)
Dim oConn = New System.Data.SqlClient.SqlConnection
oConn.ConnectionString = Conn
Dim Command As New SqlClient.SqlCommand(Sql, oConn)
Throw New InstallException("安装过程中出现错误: " & ex.Message)
Throw ex
Finally
Command.Connection.Close()
End Try
End Sub
''''在文本文件中提取Sql语句 参考部分
Private Function GetSql(ByVal Name As String) As String
Try
Dim Asm As [Assembly] = [Assembly].GetExecutingAssembly()
Dim strm As Stream = Asm.GetManifestResourceStream(Asm.GetName().Name + "." + Name)
Dim reader As StreamReader = New StreamReader(strm)
Return reader.ReadToEnd()
Catch ex As Exception
MsgBox("In GetSQL: " & ex.Message)
Throw ex
End Try