《关于工程带数据库打包(3)―自动安装备份数据库数据》

前一篇中介绍了如何连同 Sql Server 的桌面版本一同打包到安装程序的简单步骤,这里还想就自己对于:发布程序到已经有 SQL 环境的计算机时,自动使用 SQL 的 Osql 来恢复指定的数据库到你的 SQL Server 的 Date 中。

首先,在 c:\ 创建一个临时目录,例如 c:\TempBD ,拷贝 Osql.exe 到目录下,拷贝你的数据库备份( TruckDB )到目录下;在目录下分别创建 Restore.bat 和 Restore.txt 文件,内容如下:

1. Restore.bat 文件内容:

osql -E -S -i C:\TempDB\Restore.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 Files\Microsoft SQL Server\MSSQL\Data\TruckDB.mdf'

restore database TruckDB

from disk='c:\TempDB\TruckDB'

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 Files\Microsoft SQL Server\MSSQL\Data\TruckDB_data.mdf") = True Then Exit Sub

MyBase .Install(stateSaver)

Dim CheckedDir As System.IO.Directory

If CheckedDir.Exists("C:\Program Files\Microsoft SQL Server\MSSQL\Data") = False Then

CheckedDir.CreateDirectory("C:\Program Files\Microsoft SQL Server\MSSQL\Data")

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 Files\Microsoft SQL Server\MSSQL\Data ” ) = False Then

' CheckedDir.CreateDirectory( “ C:\Program Files\Microsoft SQL Server\MSSQL\Data ” )

' End If

Try

Shell( “ c:\TempDB\Restore.bat ” , AppWinStyle.Hide, True )

Catch

End Try

End <SPAN

Published At
Categories with Web编程
Tagged with
comments powered by Disqus