Imports Microsoft.SqlServer.Management.Smo Imports System.IO Public Class Form1 Private Sub SimpleButtonLoadDatabases_Click(sender As Object, e As EventArgs) Handles SimpleButtonLoadDatabases.Click Try Me.Cursor = Cursors.WaitCursor() Me.ComboBoxDatabases.Items.Clear() Dim server As New Server(Label1.Text) For Each database As Database In server.Databases Me.ComboBoxDatabases.Items.Add(database.Name) Next Try Catch ex As Exception End Try Catch ex As Exception MessageBox.Show(ex.Message) Finally Me.Cursor = Cursors.Default() End Try End Sub Private Sub Buttonbackup_Click(sender As Object, e As EventArgs) Handles Buttonbackup.Click Try Cursor.Current = Cursors.WaitCursor() Dim serverName As String = Me.Label1.Text Dim pathName As String = Me.pathforbackup.Text Dim databaseName As String = Me.ComboBoxDatabases.Text If String.IsNullOrEmpty(serverName) Then Throw New ArgumentException("Enter server name") If String.IsNullOrEmpty(pathName) Then Throw New ArgumentException("Set backup file path") If String.IsNullOrEmpty(databaseName) Then Throw New ArgumentException("Must set database name") Dim _Day As String = Today.Day.ToString().PadLeft(2, CChar("0")) Dim _Month As String = Today.Month.ToString().PadLeft(2, CChar("0")) Dim _Year As String = Today.Year.ToString().Substring(2) Dim _Hour As String = Now.Hour.ToString().PadLeft(2, "0"c) Dim _Minute As String = Now.Minute.ToString().PadLeft(2, "0"c) Dim fileName As String = databaseName + "-" + _Day + _Month + _Year + _Hour + _Minute If Not Directory.Exists(Me.pathforbackup.Text) Then Directory.CreateDirectory(Me.pathforbackup.Text) fileName = Me.pathforbackup.Text + "\" + fileName Dim backupServer As New Server(serverName) Dim dataBackup As New Backup() dataBackup.Action = BackupActionType.Database dataBackup.Database = databaseName 'set the devices: file, tape etc dataBackup.Devices.AddDevice(fileName, DeviceType.File) dataBackup.SqlBackup(backupServer) MessageBox.Show("Backup of " & databaseName & " completed sucessfully") Catch exSMO As SmoException MessageBox.Show(exSMO.Message) Catch ex As Exception MessageBox.Show(ex.Message) Finally Cursor.Current = Cursors.Default() End Try End Sub Private Sub ButtonRestore_Click(sender As Object, e As EventArgs) Handles ButtonRestore.Click Dim res As New Restore Dim serverName As String = Me.Label1.Text Dim fileName As String = Me.pathforbackup.Text Dim databaseName As String = Me.ComboBoxDatabases.Text Dim restoreServer As New Server(serverName) Try If String.IsNullOrEmpty(serverName) Then Throw New ArgumentException("Enter server name") If String.IsNullOrEmpty(fileName) Then Throw New ArgumentException("Set restore file path") If String.IsNullOrEmpty(databaseName) Then Throw New ArgumentException("Must set database name") res.Database = databaseName res.Action = RestoreActionType.Database res.Devices.AddDevice(fileName, DeviceType.File) res.SqlRestore(restoreServer) MessageBox.Show("Restore of " + databaseName + " Complete", "Restore", MessageBoxButtons.OK, MessageBoxIcon.Information) Catch exSMO As SmoException MessageBox.Show(exSMO.Message) Catch ex As Exception MessageBox.Show(ex.Message) Finally Cursor.Current = Cursors.Default() End Try End Sub Private Sub getrestorefile_Click(sender As Object, e As EventArgs) Handles getrestorefile.Click Dim dlgFolder As New FolderBrowserDialog() Try Me.Cursor = Cursors.WaitCursor dlgFolder.ShowNewFolderButton = True If dlgFolder.ShowDialog(Me) = Windows.Forms.DialogResult.OK Then Me.TextBox1.Text = dlgFolder.SelectedPath.ToString() End If Catch eX As Exception MessageBox.Show(eX.Message) Finally Me.Cursor = Cursors.Default End Try End Sub End Class
var
This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)