自动安装SQL Server数据库

发表于:2007-07-02来源:作者:点击数: 标签:
下面简单介绍一下如何在工程中添加Microsoft SQL Server 2000 Desktop Engine (MSDE)的自动安装合并模块MSM: 1. 首先你需要下载MSDE2000的sp3安装包。可以从下面的链接去下载,URL: http://www.microsoft.com/sql/downloads/2000/sp3.asp 2. 下载完成,你可

下面简单介绍一下如何在工程中添加Microsoft SQL Server 2000 Desktop Engine (MSDE)的自动安装合并模块MSM:



1.     首先你需要下载MSDE2000的sp3安装包。可以从下面的链接去下载,URL: http://www.microsoft.com/sql/downloads/2000/sp3.asp



 

2.     下载完成,你可以安装或者用ZIP解压到本地硬盘,将可以看到:Msi、MSM、Setup这3个目录和setup等文件,其中的MSM文件夹中的东东就是本文要介绍的MSDE2000合并模块。



 

3.     在你的工程中添加一个安装工程(假设为MySetup1,路径为c:\MySetup1),按照正常的步骤添加工程输出(Project Output),选择输出文件(primary output)和内容文件(content files)两项。



 

4.     添加合并模块(Merge Moudle),选择浏览,指定到你的MSDE的MSM文件夹,选则MSM和msm\1033下的所有的文件(REPL.MSM、REPL_RES.MSM、 DMO_RES.MSM和DMO.MSM可以不选),打开。



 

5.     设置安装工程的属性(Properties)中的SearchPath,指定为你的MSM所在路径(这时需要添加两个:path\MSM和path\MSM\1033)。



 

6.     设置关于安装程序的其他属性咱们暂且不提,这是可以对你的安装工程进行编译了,当编译通过后,你可以在c:\MySetup1\Debug\看到你的打包工程MySetup.msi。



 

7.     这是工程打包告一段落,下面我们需要修改打好的安装包,使它可以在安装完程序后自动安装MSDE的一个实例(假设实例名为:MyServer)。现在我们需要用到MS的一个工具ORCA,下载地址:http://support.microsoft.com/default.aspx?scid=kb;EN-US;255905安装了orca后就可以利用这个工具对我们的MySetup1.msi进行修改了。



 

8.     用ORCA打开安装包文件MySetup.msi,找到Property这个table,ADD Row 在Propetry中填入SqlInstanceName,value中填入实例名MyServer。其他的参数请参见:http://support.microsoft.com/?id=810826和http://support.microsoft.com/default.aspx?scid=kb;en-us;281983



 

9.     所有的参数中有关密码的属性,我没有成功,就是SqlSaPwd,这个参数是和SqlSecurityMode一起使用的,但是我一直没有设置成功,密码总是为空的,不知何故!



 

10. 选择InstallExecuteSequence这个table,找到SetPropSQLMSDEInstalled这个Action,修改206为102;找到RemoveExistingProducts,修改1525为6601。保存,退出!

 



 

这时,在没有SQL环境的机器上运行你的安装包,在程序安装完后,会自动安装MSDE的MyServer实例,并在重起机器后,自动启动Sql Server的实例。=======================================================================前一篇中介绍了如何连同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 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 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 Files\Microsoft SQL Server\MSSQL\Data\”下,便于恢复。


 



首先,在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 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 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 Files\Microsoft SQL Server\MSSQL\Data\”下,便于恢复。

原文转自:http://www.ltesting.net