installDB.vb
上传用户:wj57717022
上传日期:2014-12-16
资源大小:4093k
文件大小:4k
- Imports System.ComponentModel
- Imports System.Configuration.Install
- <RunInstaller(True)> Public Class installDB
- 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}webjxc.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}webjxc.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("EXEC sp_attach_db @dbname = N'webjxc',@filename1 = N'" & path & "webjxc_Data.mdf',@filename2 = N'" & path & "webjxc_log.ldf'" & vbCrLf)
- 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(" -E -S {0} -i {1}Mydb2000tp.sql", 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