The previous article introduces how to pack the installer along with the desktop version of SQL Server, here you also want to use SQL's OSQL to restore the specified database when you publish your program to a computer that already has a SQL environment. Go to your SQL Server Date.
First, in C: / Create a temporary directory, such as C: / Tempbd, copy osql.exe to the directory, copy your database backup (TRUCKDB) to the directory; create a restore.bat and restore.txt files in the directory. The content is as follows:
1. RESTORE.BAT file content:
OSQL-E -S -I C: /Tempdb/Restore.txt
2. RESTORE.TXT file content:
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
Second, add an Installer Class in your project: Select Project Main Project, add Installer Class, the name is assumed to be installer1. Select the code page of Instollr1, add the following code:
Public overrides sub install (byval statesaver as system.collections.idictionary)
'Over-write Install method
Dim file as system.IO.FILE
IF file.exists ("c: / program files / microsoft sql server / mssql / data / TRUCKDB_DATA.MDF") = true kilion
Mybase.install (StateSaver)
Dim CheckedDir as system.io.directory
IF CHECKEDDIR.EXISTS ("C: / Program Files / Microsoft SQL Server / MSSQL / DATA") = false kil
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
'Extraction installation path
Strtemp = strTemp.Remove (Strtemp.lastIndexOf ("/"), Len (Strtemp) - Strtemp.lastIndexof ("/")) 'Copy DateBase to Computer.
IF creATDIR (strTemp) = false then
'Failure, reverse installation
Me.unInstall (StateSaver)
EXIT SUB
Else
END IF
IF installdb (strtemp) = false kil
'Failure, reverse installation
Me.unInstall (StateSaver)
EXIT SUB
Else
END IF
'Delete database temporary files
DeleteDir ("c: / tempdb")
Deletedir (strTemp "/ tempdb)
End Sub
Public overrides subnstall (Byval Stateaver as system.collections.idictionary)
'Execution reverse installation
'Using reflection extraction installation path
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 = startemp.remove ("/"), Len (strTemp) - strTemp.lastIndexof ("/"))
'Delete database files and temporary files
Deletedir (strTemp "/ tempdb)
DeleteDir ("c: / tempdb")
End Sub
Private function deletedir (byval path as string) as boolean
'Delete the specified folder
DIM DIR AS System.io.directory
If Dir.exists (PATH) = True Ten Dir.Delete (Path, True)
END FUNCTION
Private function creatdir (byval path as string) as boolean
'Creating a specified folder
Dim files as system.IO.FILE
DIM DIRS AS System.io.directory
Try
If DIRS.EXISTS ("C: / Tempdb") = false the 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)
All files of 'Copy specified folders to the target folder (single layer).
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 - 1spath = 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
'Install the database, call the automatic batch.
'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
Then, add a installation project in your project, name MySetup1, add the project output, select the output file (PRIMARY OUTPUT), the content file, and add a folder. To the Application Folder, the folder's name is Tempdb, add a file to the folder Tempdb: osql.exe, restore.bat, restore.txt, truckdb (Database File). Set your folder's Properties's AlwayScreate for True. Compile your setup project.
At this time, the generated mounting package will automatically call the installer class to restore your TRUCKDB database after the program is installed.
Note that TRUCKDB should be backed up to "C: / Program Files / Microsoft SQL Server / MSSQL / DATA /" for easy recovery.
"About the engineering band database package (3) - Automatically install backup database data"