BTruckDB'
with replace
其次,在你的工程中添加一個Installer Class:選中Project主工程,添加Installer Class,名稱假定為installer1.選擇instller1的代碼頁,添加下面的代碼:
Public Overrides Sub Install(ByVal stateSaver As System.Collections.IDictionary)
'重寫install方法
Dim file As System.IO.File
If file.Exists("C:Program FilesMicrosoft SQL ServerMSSQLDataTruckDB_data.mdf") = True Then Exit Sub
MyBase.Install(stateSaver)
Dim CheckedDir As System.IO.Directory
If CheckedDir.Exists("C:Program FilesMicrosoft SQL ServerMSSQLData") = False Then
CheckedDir.CreateDirectory("C:Program FilesMicrosoft SQL ServerMSSQLData")
End If
Dim FullPath As String
Dim Asm As System.Reflection.Assembly = System.Reflection.Assembly.GetExecutingAssembly()
Dim strConfigLoc As String
strConfigLoc = Asm.Location
Dim file As System.IO.File
If file.Exists("C:Program FilesMicrosoft SQL ServerMSSQLDataTruckDB_data.mdf") = True Then Exit Sub
MyBase.Install(stateSaver)
Dim CheckedDir As System.IO.Directory
If CheckedDir.Exists("C:Program FilesMicrosoft SQL ServerMSSQLData") = False Then
CheckedDir.CreateDirectory("C:Program FilesMicrosoft SQL ServerMSSQLData")
End If
Dim FullPath As String
Dim Asm As System.Reflection.Assembly = System.Reflection.Assembly.GetExecutingAssembly()
Dim strConfigLoc As String
strConfigLoc = Asm.Location
Dim strTemp As String
strTemp = strConfigLoc
您正在看的SQLserver教程是:實現自動安裝SQLServer數據庫。>
'提取安裝路徑
strTemp = strTemp.Remove(strTemp.LastIndexOf(""), Len(strTemp) - strTemp.LastIndexOf(""))
'Copy DateBase to computer.
If CreatDIR(strTemp) = False Then
'失敗,反安裝
Me.Uninstall(stateSaver)
Exit Sub
Else
End If
If InstallDB(strTemp) = False Then
‘失敗,反安裝
Me.Uninstall(stateSaver)
Exit Sub
Else
End If
‘刪除數據庫臨時文件
DeleteDIR(“c:TempDB”)
DeleteDIR(strTemp + “TempDB”)
End Sub
Public Overrides Sub Uninstall(ByVal stateSaver As System.Collections.Idictionary)
‘執行反安裝
‘利用反射提取安裝路徑
MyBase.Uninstall(stateSaver)
Dim Asm As System.Reflection.Assembly = System.Reflection.Assembly.GetExecutingAssembly()
Dim strConfigLoc As String
strConfigLoc = Asm.Location
Dim strTemp As String
strTemp = strConfigLoc
strTemp = strTemp.Remove(strTemp.LastIndexOf(“”), Len(strTemp) ? strTemp.LastIndexOf(“”))
‘刪除數據庫文件和臨時文件
DeleteDIR(strTemp + “TempDB”)
DeleteDIR(“c:TempDB”)
End Sub
Private Function DeleteDIR(ByVal path As String) As Boolean
‘刪除指定的文件夾
Dim dir As System.IO.Directory
If dir.Exists(path) = True Then dir.Delete(path, True)
> End Function
Private Function CreatDIR(ByVal path As String) As Boolean
‘創建指定的文件夾
Dim Files As System.IO.File
Dim Dirs As System.IO.Directory
Try
If Dirs.Exists(“c:TempDB”) = False Then Dirs.CreateDirectory(“c:TempDB”)
‘copy Creat DB files
CopyFile(path + “TempDB”, “C:TempDB”)
Return True
Catch
Return False
End Try
End Function
Private Sub CopyFile(ByVal SourceDirName As String, ByVal DestDirName As String)
‘copy指定的文件夾的所有文件到目標文件夾(單層)。
Dim dir As System.IO.Directory
Dim File As System.IO.File
Dim sPath, oPath As String
Dim I As Integer
For I = 0 To dir.GetFiles(SourceDirName)。Length ? 1
sPath = dir.GetFiles(SourceDirName)。GetValue(i)。ToString
oPath = Microsoft.VisualBasic.Right(sPath, Len(sPath) ? Len(SourceDirName))
File.Copy(sPath, DestDirName + oPath, True)
Next
End Sub
Private Function InstallDB(ByVal path As String) As Boolean
‘安裝數據庫,調用自動批處理。