IReturnend Function
'********************************************************** ************************ ** Module name: FRESTOREDATABASE_A '** Description: Restore the database, return to error information, normal recovery, return "" '** call: FRESTOREDATABASE_A "Backup File Name", "Database Name" ** Parameter Description:' ** SDATABASEPATH Restore Data Stock Directory '** SBACKUPNUMBER is from that backup number Restore' ** SREPLACEXIST Specify whether to overwrite Already existing data '**: References Microsoft ActiveX Data Objects 2.x library' ** Creation: Zou Jian '** Japan: December 09, 2003' ********** *********************************************************** ************* Public Function fRestoreDatabase_a (ByVal sBackUpfileName $ _, ByVal sDataBaseName $ _, Optional ByVal sDataBasePath $ = "" _, Optional ByVal sBackupNumber & = 1 _, Optional ByVal sReplaceExist As Boolean = False _) AS STRING DIM IDB AS AdoDb.Connection, IRE AS AdoDb.Recordset Dim Iconcstr $, ISQL $, IRETURN $, II & IN ERROR GOTO LBERR 'Create Object Set Idb = New ADO DB.Connection SET IRE = New AdoDb.Recordset 'Connecting the Database Server, modify the connection string iconcstr = "provike = sqloledb.1; integrated security = sspi; data source = zj" IDB. Open iconcstr 'gets the restored data inventory, if not specified, stored into SQL Server Data Directory if SDatabasePath = "", ISQL = "SELECT FileName from master..sysfiles" IRE.Open ISQL, IDB, AdopenkeySet, AdlockReadOnly isql = IRE (0) Ire.close SDatabasePath = Left (isql, ISTRREV (ISQL, "/")) endiff '
Check if the database exists If sReplaceExist = False Then iSql = "select 1 from master..sysdatabases where name = '" & sDataBaseName & "'" iRe.Open iSql, iDb, adOpenKeyset, adLockReadOnly If iRe.EOF = False Then iReturn = " The database already exists! "IRE.Close Goto Lbexit End if Ire.close End if 'Close the user process to prevent other users from being using the database, resulting in data recovery failed ISQL =" SELECT SPID from master..sysprocesses where dbid = db_id (' " & SDatabaseNameName & "')" Ire.Open ISQL, IDB, AdopenkeySet, AdlienkeySet, Adlieadonly While Ire.eof = false isql = "kill" & IRE (0) idb.execute isql IRE.MOVENEXT Were.Close' Get Database Recovery Information ISQL = "restore filelistonly from disk = '" & sBackUpfileName & "'" & vbCrLf & _ "with file =" & sBackupNumber iRe.Open iSql, iDb, adOpenKeyset, adLockReadOnly 'to generate a database restore statement iSql = "restore database [" & sDataBaseName & "]" & vbcrlf & _ "from disk = '" & sbackupfilename "" & vbcrlf & _ "with file =" & sbackupnumb ER & VBCRLF with IRE while not .eof Ireturn = IRE ("PhysicalName") II = IRETURN, ".") IReturn = IIF (II = 0, "", MID (IReturn, II)) & "'" isql = ISQL & ", Move '" & IRE ("LogicalName") & _ "' To '" & SDatabasePath & SDatabaseName & Ireturn & SDatabaseName & IReturn