Problem using Backup Database

jotaeva

Member
Joined
Sep 2, 2009
Messages
8
Programming Experience
Beginner
I try to create a Sub for a SQL Dabase backup by code using this code:

VB.NET:
Expand Collapse Copy
Private Sub btnBackup_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnBackup.Click
  Dim sBackup As String = "BACKUP DATABASE " & Me.txtBase.Text & _
  " TO DISK = N'" & Me.txtBackup.Text & _
  "' WITH NOFORMAT, NOINIT, NAME =N'" & Me.txtBase.Text & _
  "' -Full Database Backup',SKIP, STATS = 10"

  Dim csb As New SqlConnectionStringBuilder
  csb.DataSource = Me.txtServidor.Text
  csb.InitialCatalog = Me.txtBase.Text
  csb.IntegratedSecurity = True
  Using con As New SqlConnection(csb.ConnectionString)
   Try
    con.Open()
    Dim cmdBackUp As New SqlCommand(sBackup, con)
    cmdBackUp.ExecuteNonQuery()
    MessageBox.Show("Backup Completo", "Backup Base de Datos", MessageBoxButtons.OK, MessageBoxIcon.Information, MessageBoxDefaultButton.Button1)
    con.Close()
   Catch ex As Exception
    MessageBox.Show(ex.Message, "Error al copiar la base de datos", MessageBoxButtons.OK, MessageBoxIcon.Error)
   End Try
  End Using
 End Sub

But I receive this error:

Incorrect syntax near '-'.
Unclosed quotation mark after the character string ',SKIP, STATS = 10'.


Can you help me?
 
I think your error message is pretty self explainatory. It tells you right there, there is a problem with your opening and closing quotation marks within your statement. Display your query variable and you will see the problem; you have a total of 5 single quotation marks.
 
Last edited:
Back
Top