Imports System.Data.OleDb.OleDbCommand
Imports System.Data.OleDb.OleDbConnection
Imports System.Data.OleDb.OleDbDataAdapter
Imports System.Data.OleDb.OleDbDataReader
Imports System.Data.OleDb.OleDbParameter
Imports System.Diagnostics '进程
Imports System.Data.SqlClient
Public Class attachMapDb '卸离Map数据库用 ,同时还加入2个database 文件
Dim masterstring As String = "Provider=SQLOLEDB;Persist Security Info=False;Data Source=HSMAP;Initial Catalog=master;User ID=user1;Password=user1;"
Dim masterconn As New OleDb.OleDbConnection(masterstring)
Dim HSmappath As String = "C:\hs\Map\Hsmap.exe"
Dim sqlstr As String = "sp_detach_db aoi" '卸离Map数据库用
Dim DATABASEPATH As String = "C:\HS\Database" '正在使用的数据库文件夹
Dim databaseusingPath_mdf As String = "C:\HS\Database\AOI_Data.MDF" '正在使用的数据库地址
Dim databaseusingPath_ldf As String = "C:\HS\Database\AOI_Log.LDF" '正在使用的数据库地址
Public Sub mainY() '主流程
killMapProgram() '关闭相关进程,
If executeDB(sqlstr) Then '卸离Map数据库
copydatabaseFile() '更换文件
fujiaDB() '附加数据库
startHSMap() '启动程式
End If
End Sub
Public Sub fujiaDB() '附加数据库
Try
Dim sqlBuilder As New System.Data.SqlClient.SqlConnectionStringBuilder
sqlBuilder.AttachDBFilename = databaseusingPath_mdf
sqlBuilder.DataSource = "dongG\sqlexpress"
sqlBuilder.IntegratedSecurity = True
sqlBuilder.UserInstance = False
sqlBuilder.InitialCatalog = "aoi"
Using cn As New SqlConnection(sqlBuilder.ConnectionString)
cn.Open()
cn.Close()
End Using
Catch ex As Exception
writeLog("附加AOI新数据库失败")
End Try
End Sub
Public Sub killMapProgram() '1
Dim p As New System.Diagnostics.Process
For Each p In Process.GetProcesses()
If p.ProcessName.ToUpper() = "HSMAP" Then
p.Kill()
SyncLock GetType(attachMapDb)
writeLog("HSMap已关闭!")
End SyncLock
End If
Next
End Sub
Public Sub startHSMap() '重开进程
Try
Shell(HSmappath, AppWinStyle.NormalFocus, False)
Catch ex As Exception
writeLog("启动HSmap程式失败!")
End Try
End Sub
Public Function executeDB(ByVal sqlstr As String) As Boolean '非查询命令,用主数据库master挂载。
Try
masterconn.Open()
Dim command As New OleDb.OleDbCommand(sqlstr, masterconn)
command.ExecuteNonQuery()
executeDB = True
masterconn.Close()
Catch ex As Exception
masterconn.Close()
executeDB = False
writeLog("分离数据库错误!")
End Try
End Function
Public Sub copydatabaseFile() ' 剪切文件
Try
Dim BACKUPDATADATE As String = Application.StartupPath & "\backup\" & nowtoNum(Now) '定义个新文件夹用来备份
If (BACKUPDATADATE) = False Then (BACKUPDATADATE) '如果没有就创建
Dim appth As String = Application.StartupPath & "\MapDb" '干净的数据库文件夹
If (appth) = True Then
Dim sr() As String = (appth) ' 取得小的数据库文件夹内的文件
Dim RD() As String = (DATABASEPATH) '计算正在使用的数据库文件夹的文件名
For I As Integer = 0 To RD.Length - 1
Dim RDSUBSTR As String = RD(I).Substring(RD(I).LastIndexOf("\"))
(RD(I), BACKUPDATADATE & "\" & RDSUBSTR) '拷贝当前使用的数据库文件夹的文件到备份文件夹中去。
(RD(I)) '删除
(sr(0), RD(I)) 'copy 到 正在用的文件位置以备使用。
Next
Else
writeLog("没发现干净的数据库文件夹!")
End If
Catch ex As Exception
writeLog("剪切文件错误!")
End Try
End Sub
End Class