Click here to Skip to main content
15,892,161 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
I download a vb.net application to backup and restore sql database..
but I got only code... so using that code I create a new vb.net application for the same purpose.
Taking Backing is successfully completed but Restore is giving error... please help... code is given below: I am using sql server which is installed default with Visual studio 2012 ultimate... for that I have define sever in Lablel1 = (LocalDB)\v11.0 ... Please help....

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
Posted
Comments
OriginalGriff 8-Jan-14 4:25am    
What error is is giving? Where? Is there a message?
Member 10279246 8-Jan-14 10:39am    
MessageBox.Show(exSMO.Message)
This line giving me error message... restore failed
Kornfeld Eliyahu Peter 8-Jan-14 4:33am    
You cannot restore a database that in use!
Member 10279246 8-Jan-14 10:46am    
I am not using that... I have select server which is (LocalDB)\v11.0 and select one of database and take backup... Now I want to restore the same database which I have recently backup... then it gives me error
Kornfeld Eliyahu Peter 8-Jan-14 12:28pm    
You have no lines for connecting the DB to run the restore command on that connection as sql query?

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900