0

Currently I used to back up my files in the way that, when user click on Backup the program will ask,

To Backup you must close your current session. This application will be closed now. Do you want to continue?

So the application will be closed and a new application will be launch in which if you click Backup it will copy the .mdf File and the .ldf File

|

But I have read in many pages that 'Copying the .mdf File and the .ldf File' is the unsafest way, so is there any other way to do Backup other than using SSMS because I want the user to be able to Backup within the Application.

|

Current code:

Sub Backup()

    Dim con As New SqlClient.SqlConnection("data source=.\SQLEXPRESS;initial catalog=BQDB;Integrated Security=True")
    Dim cmd As New SqlCommand()

    Try
        con.Open()
        cmd.CommandType = CommandType.Text
        cmd.CommandText = "Backup database BQDB To Disk='C:\Users\Zulfikar\BQBackup.BAK'"
        cmd.Connection = con
        cmd.ExecuteNonQuery()
    Catch ex As Exception
        MessageBox.Show(ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error)
    End Try
End Sub

|

Error Message Using Justin's Code

1 Answer 1

0

I tend to see a performance boost when using SqlCommands to backup databases.

      Sub Backup()

               Dim con As New SqlClient.SqlConnection("data source=DATASOURCE;initial catalog=NAME OF DATABASE;Integrated Security=True")
               Dim cmd As New SqlCommand()

                    Try                         
                        con.Open() 
                        cmd.CommandType = CommandType.Text
                        cmd.CommandText = "Backup database BQDB To Disk='C:\Users\Zulfikar\BQBackup.BAK'"
                        cmd.Connection = con
                        cmd.ExecuteNonQuery()
                    Catch ex As Exception
                        MessageBox.Show(ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error)
                    End Try
                End Sub
4
  • Hi @Justin Woodmancy , I am having 2 problem while trying your code... | Problem #1: My database name is BQDB so how can I replace this with your code? Is it like this? --- cmd.CommandText = "Backup database BQDB To Disk='C:\Users\Zulfikar\BQBackup.BAK'" Jun 30, 2016 at 4:17
  • Problem #2: When I tried your code the error I get is: Unable to connect to any of the specified MySQL hosts , so what the problem and what is the fix? Jun 30, 2016 at 4:22
  • Check my edit. It sounds like you don't have your connection string to the database set up. Jun 30, 2016 at 13:21
  • Hi How can I find my data source , I mean what should I replace with 'DATASOURCE' and the 'Name Of Database' is the name of the .mdf file right? Jul 3, 2016 at 11:16

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.