<RunInstaller(True)> Public Class Installer1
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()
components = New System.ComponentModel.Container
End Sub
#End Region
Public Overrides Sub Install(ByVal stateSaver As System.Collections.IDictionary)
MyBase.Install(stateSaver)
If Not InstallDB() Then
'失败,反安装
Me.Uninstall(stateSaver)
Exit Sub
End If
DeleteFile(String.Format("{0}DB.dat", Me.Context.Parameters.Item("targetdir")))
End Sub
Public Overrides Sub Uninstall(ByVal stateSaver As System.Collections.IDictionary)
'执行反安装
MyBase.Uninstall(stateSaver)
DeleteFile(String.Format("{0}DB.dat", Me.Context.Parameters.Item("targetdir")))
End Sub
Private Sub DeleteFile(ByVal paths As String)
'删除指定的文件
Try
Dim delFile As New System.IO.FileInfo(paths)
If delFile.Exists Then
delFile.Delete()
End If
Catch ex As Exception
End Try
End Sub
Private Sub CreateSql(ByVal paths As String)
Dim File As System.IO.StreamWriter
Dim db As String = String.Format("{0}", Me.Context.Parameters.Item("dbname"))
Dim path As String = String.Format("{0}", Me.Context.Parameters.Item("targetdir"))
Try
Dim s As New System.Text.StringBuilder
s.Append("use master" & vbCrLf)
s.Append("" & vbCrLf)
s.Append("if not exists (select * from sysdatabases where name='" & db & "')" & vbCrLf)
s.Append(" BEGIN" & vbCrLf)
s.Append(" create database " & db & vbCrLf)
s.Append(" END" & vbCrLf)
s.Append("" & vbCrLf)
s.Append("if exists (select * from sysdevices where name='DBdisk')" & vbCrLf)
s.Append(" BEGIN" & vbCrLf)
s.Append(" EXEC sp_dropdevice 'DBdisk'" & vbCrLf)
s.Append(" END" & vbCrLf)
s.Append("Else" & vbCrLf)
s.Append(" BEGIN" & vbCrLf)
s.Append(" EXEC sp_addumpdevice 'disk','DBdisk', '" & path & "DB.dat'" & vbCrLf)
s.Append(" END" & vbCrLf)
s.Append("" & vbCrLf)
s.Append("restore database " & db & vbCrLf)
s.Append("from disk='" & path & "DB.dat'" & vbCrLf)
s.Append("with replace")
File = New System.IO.StreamWriter(paths)
File.Write(s.ToString)
Catch ex As Exception
Finally
File.Close()
End Try
End Sub
Private Function InstallDB() As Boolean
'安装数据库,调用自动批处理。
Try
'创建临时脚本
CreateSql(String.Format("{0}Mydb2000tp.sql", Me.Context.Parameters.Item("targetdir")))
'调用osql执行脚本
Dim sqlProcess As New System.Diagnostics.Process
sqlProcess.StartInfo.FileName = "osql.exe"
sqlProcess.StartInfo.Arguments = String.Format(" -U {0} -P {1} -S {2} -i {3}Mydb2000tp.sql", Me.Context.Parameters.Item("user"), Me.Context.Parameters.Item("pwd"), Me.Context.Parameters.Item("server"), Me.Context.Parameters.Item("targetdir"))
sqlProcess.StartInfo.WindowStyle = ProcessWindowStyle.Hidden
sqlProcess.Start()
sqlProcess.WaitForExit() '等待执行
sqlProcess.Close()
'删除脚本文件
DeleteFile(String.Format("{0}Mydb2000tp.sql", Me.Context.Parameters.Item("targetdir")))
Return True
Catch ex As Exception
Return False
End Try
End Function
End Class
本文地址:http://com.8s8s.com/it/it41442.htm