[SIZE=2][COLOR=#0000ff]Private[/COLOR][/SIZE][SIZE=2] [/SIZE][SIZE=2][COLOR=#0000ff]Function[/COLOR][/SIZE][SIZE=2] BackupRestore([/SIZE][SIZE=2][COLOR=#0000ff]ByVal[/COLOR][/SIZE][SIZE=2] strFilePath [/SIZE][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][SIZE=2] [/SIZE][SIZE=2][COLOR=#0000ff]String[/COLOR][/SIZE][SIZE=2], [/SIZE][SIZE=2][COLOR=#0000ff]ByVal[/COLOR][/SIZE][SIZE=2] blnBackup [/SIZE][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][SIZE=2] [/SIZE][SIZE=2][COLOR=#0000ff]Boolean[/COLOR][/SIZE][SIZE=2]) [/SIZE][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][SIZE=2] [/SIZE][SIZE=2][COLOR=#0000ff]Boolean
[/COLOR][/SIZE][SIZE=2][/SIZE][SIZE=2][COLOR=#008000]' the message to be displayed at the end of backup/restore
[/COLOR][/SIZE][SIZE=2][/SIZE][SIZE=2][COLOR=#0000ff]Dim[/COLOR][/SIZE][SIZE=2] strMessage [/SIZE][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][SIZE=2] [/SIZE][SIZE=2][COLOR=#0000ff]String[/COLOR][/SIZE][SIZE=2] = [/SIZE][SIZE=2][COLOR=#0000ff]String[/COLOR][/SIZE][SIZE=2].Empty
[/SIZE][SIZE=2][COLOR=#008000]' the title for the message above
[/COLOR][/SIZE][SIZE=2][/SIZE][SIZE=2][COLOR=#0000ff]Dim[/COLOR][/SIZE][SIZE=2] strTitle [/SIZE][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][SIZE=2] [/SIZE][SIZE=2][COLOR=#0000ff]String[/COLOR][/SIZE][SIZE=2] = [/SIZE][SIZE=2][COLOR=#0000ff]String[/COLOR][/SIZE][SIZE=2].Empty
[/SIZE][SIZE=2][COLOR=#008000]' this will be the connection to the sql server instance at runtime
[/COLOR][/SIZE][SIZE=2][/SIZE][SIZE=2][COLOR=#0000ff]Dim[/COLOR][/SIZE][SIZE=2] cnSQLConnection [/SIZE][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][SIZE=2] SqlClient.SqlConnection = [/SIZE][SIZE=2][COLOR=#0000ff]New[/COLOR][/SIZE][SIZE=2] SqlClient.SqlConnection([/SIZE][SIZE=2][COLOR=#0000ff]My[/COLOR][/SIZE][SIZE=2].Settings.MB2007ConnectionString)
[/SIZE][SIZE=2][COLOR=#008000]' this will be the connection to the server
[/COLOR][/SIZE][SIZE=2][/SIZE][SIZE=2][COLOR=#0000ff]Dim[/COLOR][/SIZE][SIZE=2] scnServerConnection [/SIZE][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][SIZE=2] ServerConnection = [/SIZE][SIZE=2][COLOR=#0000ff]Nothing
[/COLOR][/SIZE][SIZE=2][/SIZE][SIZE=2][COLOR=#008000]' this will be our admin instance
[/COLOR][/SIZE][SIZE=2][/SIZE][SIZE=2][COLOR=#0000ff]Dim[/COLOR][/SIZE][SIZE=2] srvAdminServer [/SIZE][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][SIZE=2] Server = [/SIZE][SIZE=2][COLOR=#0000ff]Nothing
[/COLOR][/SIZE][SIZE=2][/SIZE][SIZE=2][COLOR=#008000]' this is the database to use
[/COLOR][/SIZE][SIZE=2][/SIZE][SIZE=2][COLOR=#0000ff]Dim[/COLOR][/SIZE][SIZE=2] dbTargetDatabase [/SIZE][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][SIZE=2] Database = [/SIZE][SIZE=2][COLOR=#0000ff]Nothing
[/COLOR][/SIZE][SIZE=2][/SIZE][SIZE=2][COLOR=#008000]' this is a backup class
[/COLOR][/SIZE][SIZE=2][/SIZE][SIZE=2][COLOR=#0000ff]Dim[/COLOR][/SIZE][SIZE=2] bkBackup [/SIZE][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][SIZE=2] [/SIZE][SIZE=2][COLOR=#0000ff]New[/COLOR][/SIZE][SIZE=2] Backup
[/SIZE][SIZE=2][COLOR=#008000]' this is a backup device item to use with the backup class
[/COLOR][/SIZE][SIZE=2][/SIZE][SIZE=2][COLOR=#0000ff]Dim[/COLOR][/SIZE][SIZE=2] bdiBackup [/SIZE][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][SIZE=2] BackupDeviceItem = [/SIZE][SIZE=2][COLOR=#0000ff]Nothing
[/COLOR][/SIZE][SIZE=2][/SIZE][SIZE=2][COLOR=#008000]' this is a restore class
[/COLOR][/SIZE][SIZE=2][/SIZE][SIZE=2][COLOR=#0000ff]Dim[/COLOR][/SIZE][SIZE=2] rsRestore [/SIZE][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][SIZE=2] [/SIZE][SIZE=2][COLOR=#0000ff]New[/COLOR][/SIZE][SIZE=2] Restore
strFilePath &= [/SIZE][SIZE=2][COLOR=#a31515]"\MB.bak"
[/COLOR][/SIZE][SIZE=2][/SIZE][SIZE=2][COLOR=#008000]' open a connection
[/COLOR][/SIZE][SIZE=2]cnSQLConnection.Open()
[/SIZE][SIZE=2][COLOR=#008000]' and connect
[/COLOR][/SIZE][SIZE=2]scnServerConnection = [/SIZE][SIZE=2][COLOR=#0000ff]New[/COLOR][/SIZE][SIZE=2] Microsoft.SqlServer.Management.Common.ServerConnection(cnSQLConnection)
[/SIZE][SIZE=2][COLOR=#008000]' set the admin server
[/COLOR][/SIZE][SIZE=2]srvAdminServer = [/SIZE][SIZE=2][COLOR=#0000ff]New[/COLOR][/SIZE][SIZE=2] Server(scnServerConnection)
[/SIZE][SIZE=2][COLOR=#008000]' name of the database we are interested in
[/COLOR][/SIZE][SIZE=2]dbTargetDatabase = srvAdminServer.Databases([/SIZE][SIZE=2][COLOR=#a31515]"MB2007"[/COLOR][/SIZE][SIZE=2])
[/SIZE][SIZE=2][COLOR=#008000]' add a BackupDeviceItem naming the output file and setting type of device as file
[/COLOR][/SIZE][SIZE=2]bdiBackup = [/SIZE][SIZE=2][COLOR=#0000ff]New[/COLOR][/SIZE][SIZE=2] BackupDeviceItem(strFilePath, DeviceType.File)
[/SIZE][SIZE=2][COLOR=#008000]' if true user wants to perform a backup, otherwise it is a restore
[/COLOR][/SIZE][SIZE=2][/SIZE][SIZE=2][COLOR=#0000ff]If[/COLOR][/SIZE][SIZE=2] blnBackup.Equals([/SIZE][SIZE=2][COLOR=#0000ff]True[/COLOR][/SIZE][SIZE=2]) [/SIZE][SIZE=2][COLOR=#0000ff]Then
[/COLOR][/SIZE][SIZE=2][/SIZE][SIZE=2][COLOR=#008000]' we want to perform a backup action
[/COLOR][/SIZE][SIZE=2]bkBackup.Action = BackupActionType.Database
[/SIZE][SIZE=2][COLOR=#008000]' get the dynamically named database name at runtime
[/COLOR][/SIZE][SIZE=2]bkBackup.Database = cnSQLConnection.Database.ToString()
[/SIZE][SIZE=2][COLOR=#008000]' add device to the backup object
[/COLOR][/SIZE][SIZE=2]bkBackup.Devices.Add(bdiBackup)
[/SIZE][SIZE=2][COLOR=#008000]' name the backup set
[/COLOR][/SIZE][SIZE=2]bkBackup.BackupSetName = [/SIZE][SIZE=2][COLOR=#a31515]"MB2007Set"
[/COLOR][/SIZE][SIZE=2][/SIZE][SIZE=2][COLOR=#008000]' set the time to now, then it will expire immediately allow the backup to run again without
[/COLOR][/SIZE][SIZE=2][/SIZE][SIZE=2][COLOR=#008000]' complaining about using same file ... read help on backup object inialize method.
[/COLOR][/SIZE][SIZE=2]bkBackup.ExpirationDate = [/SIZE][SIZE=2][COLOR=#0000ff]Date[/COLOR][/SIZE][SIZE=2].Now
[/SIZE][SIZE=2][COLOR=#008000]' make this the first backup in set if exire date has passed, which it would have done above
[/COLOR][/SIZE][SIZE=2]bkBackup.Initialize = [/SIZE][SIZE=2][COLOR=#0000ff]True
[/COLOR][/SIZE][SIZE=2][/SIZE][SIZE=2][COLOR=#008000]' full backup required so set to false
[/COLOR][/SIZE][SIZE=2]bkBackup.Incremental = [/SIZE][SIZE=2][COLOR=#0000ff]False
[/COLOR][/SIZE][SIZE=2][/SIZE][SIZE=2][COLOR=#008000]' truncate log after backup
[/COLOR][/SIZE][SIZE=2]bkBackup.LogTruncation = BackupTruncateLogType.Truncate
[/SIZE][SIZE=2][COLOR=#008000]' call SqlBackup method to perform backup on the admin instance of SQL Server
[/COLOR][/SIZE][SIZE=2]bkBackup.SqlBackup(srvAdminServer)
[/SIZE][SIZE=2][COLOR=#008000]' create user message
[/COLOR][/SIZE][SIZE=2]strMessage = [/SIZE][SIZE=2][COLOR=#a31515]"Database successfully backed up to the following file:"[/COLOR][/SIZE][SIZE=2] & _
System.Environment.NewLine & System.Environment.NewLine & _
strFilePath
[/SIZE][SIZE=2][COLOR=#008000]' create messagebox title
[/COLOR][/SIZE][SIZE=2]strTitle = [/SIZE][SIZE=2][COLOR=#a31515]"Backup Confirmation"
[/COLOR][/SIZE][SIZE=2][/SIZE][SIZE=2][COLOR=#0000ff]Else
[/COLOR][/SIZE][SIZE=2][/SIZE][SIZE=2][COLOR=#008000]' we want to restore the database
[/COLOR][/SIZE][SIZE=2]rsRestore.Action = RestoreActionType.Database
rsRestore.Database = cnSQLConnection.Database.ToString()
rsRestore.Devices.Add(bdiBackup)
cnSQLConnection.ChangeDatabase([/SIZE][SIZE=2][COLOR=#a31515]"MASTER"[/COLOR][/SIZE][SIZE=2])
rsRestore.SqlRestore(srvAdminServer)
[/SIZE][SIZE=2][COLOR=#008000]' create user message
[/COLOR][/SIZE][SIZE=2]strMessage = [/SIZE][SIZE=2][COLOR=#a31515]"Database has been successfully restored from the following file:"[/COLOR][/SIZE][SIZE=2] & _
System.Environment.NewLine & System.Environment.NewLine & _
strFilePath
strTitle = [/SIZE][SIZE=2][COLOR=#a31515]"Restore Confirmation"
[/COLOR][/SIZE][SIZE=2][/SIZE][SIZE=2][COLOR=#0000ff]End[/COLOR][/SIZE][SIZE=2] [/SIZE][SIZE=2][COLOR=#0000ff]If
[/COLOR][/SIZE][SIZE=2]MessageBox.Show(strMessage, strTitle, MessageBoxButtons.OK, MessageBoxIcon.Information)
BackupRestore = [/SIZE][SIZE=2][COLOR=#0000ff]True
[/COLOR][/SIZE][SIZE=2][/SIZE][SIZE=2][COLOR=#008000]
[/COLOR][/SIZE][SIZE=2]cnSQLConnection.Dispose()
scnServerConnection.Disconnect()
scnServerConnection = [/SIZE][SIZE=2][COLOR=#0000ff]Nothing
[/COLOR][/SIZE][SIZE=2]srvAdminServer = [/SIZE][SIZE=2][COLOR=#0000ff]Nothing
[/COLOR][/SIZE][SIZE=2]dbTargetDatabase = [/SIZE][SIZE=2][COLOR=#0000ff]Nothing
[/COLOR][/SIZE][SIZE=2]bkBackup = [/SIZE][SIZE=2][COLOR=#0000ff]Nothing
[/COLOR][/SIZE][SIZE=2]bdiBackup = [/SIZE][SIZE=2][COLOR=#0000ff]Nothing
[/COLOR][/SIZE][SIZE=2][/SIZE][SIZE=2][COLOR=#0000ff]End[/COLOR][/SIZE][SIZE=2] [/SIZE][SIZE=2][COLOR=#0000ff]Function
[/COLOR][/SIZE]