自动安装SQL Server数据库
2007-05-18 09:39:05 来源:WEB开发网核心提示: 1. Restore.bat文件内容:osql -E -S -i C:TempDBRestore.txt2. Restore.txt文件内容:use masterif exists (select * from sysdevices where name='TruckDB'
1. Restore.bat文件内容:
osql -E -S -i C:TempDBRestore.txt
2. Restore.txt文件内容:
use master
if exists (select * from sysdevices where name='TruckDB')
EXEC sp_dropdevice 'TruckDB'
Else
EXEC sp_addumpdevice 'disk','TruckDB', 'C:Program FilesMicrosoft SQL ServerMSSQLDataTruckDB.mdf'
restore database TruckDB
from disk='c:TempDBTruckDB'
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 strTemp As String
strTemp = strConfigLoc
'提取安装路径
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
‘安装数据库,调用自动批处理。
'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
Try
Shell(“c:TempDBRestore.bat”, AppWinStyle.Hide, True)
Catch
End Try
End Function
然后,在你的工程中添加一个安装工程,取名为MySetup1,按照正常的步骤添加工程输出(Project Output),选择输出文件(primary output)和内容文件(content files)两项,再添加文件夹到application Folder,文件夹的Name为TempDB,再给文件夹TempDB添加文件:osql.exe,Restore.bat,Restore.txt,TruckDB(数据库文件)。设定你的文件夹的properties的AlwaysCreate为True。对你的Setup工程进行编译。
这时,生成的安装包,将会在安装完程序后,自动调用Installer类的方法,恢复你的TruckDB数据库。
注意,TruckDB在生成的时候,应该备份保存到“C:Program FilesMicrosoft SQL ServerMSSQLData”下,便于恢复。
- ››sql server自动生成批量执行SQL脚本的批处理
- ››sql server 2008亿万数据性能优化
- ››SQL Server 2008清空数据库日志方法
- ››sqlserver安装和简单的使用
- ››SQL Sever 2008 R2 数据库管理
- ››SQL SERVER无法安装成功,sqlstp.log文件提示[未发...
- ››Sql Server中通过父记录查找出所有关联的子记录
- ››SqlServer触发器、存储过程和函数
- ››SQL Server 中的事务(含义,属性,管理)
- ››Sqlite数据库插入和读取图片数据
- ››Sql server 2005拒绝了对对象 'xx表' (数...
- ››Sql server 2005拒绝了对对象 'xx表' (数...
更多精彩
赞助商链接