SQL DB Backup

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • OuTCasT
    Contributor
    • Jan 2008
    • 374

    SQL DB Backup

    hi all.

    I'm trying to back up a database in vs2008 using a stored procedure...

    Code:
    Dim sqlComAddumpDevice As SqlCommand = New SqlCommand("use master exec sp_addumpdevice 'disk','CompanyBackup','C:\Backup\companyName.bak'", sqlcon)
    That creates the dump device for the backup but when i want to back up that database i want to be able to let the user choose the name of the backup

    Code:
    Dim sqlComBackupDatabase As SqlCommand = New SqlCommand("Backup database ' " & strCompanyName &  " ' to CompanyBackup", sqlcon)
    i get the strCompanyName from user input.

    it gives me a syntax error

    [HTML]An unhandled exception of type 'System.Data.Sq lClient.SqlExce ption' occurred in System.Data.dll

    Additional information: Incorrect syntax near 'test'.[/HTML]

    but when i do the command without user input

    Code:
    Dim sqlComBackupDatabase As SqlCommand = New SqlCommand("Backup database test to CompanyBackup", sqlcon)
    then it works 100%

    Can anyone help
  • MrMancunian
    Recognized Expert Contributor
    • Jul 2008
    • 569

    #2
    The difference between the two code parts you posted is the use of quotes (' '). In your first example, working with a variable for the companyname you use quotes.

    Code:
    Dim sqlComBackupDatabase As SqlCommand = New SqlCommand("Backup database ' " & strCompanyName &  " ' to CompanyBackup", sqlcon)
    When you use test as name the query will look like this:

    Code:
    Dim sqlComBackupDatabase As SqlCommand = New SqlCommand("Backup database ' test ' to CompanyBackup", sqlcon).
    When you don't use a variable your code looks like this:

    Code:
    Dim sqlComBackupDatabase As SqlCommand = New SqlCommand("Backup database test to CompanyBackup", sqlcon)
    You see the difference?

    Comment

    • OuTCasT
      Contributor
      • Jan 2008
      • 374

      #3
      Originally posted by MrMancunian
      The difference between the two code parts you posted is the use of quotes (' '). In your first example, working with a variable for the companyname you use quotes.

      Code:
      Dim sqlComBackupDatabase As SqlCommand = New SqlCommand("Backup database ' " & strCompanyName &  " ' to CompanyBackup", sqlcon)
      When you use test as name the query will look like this:

      Code:
      Dim sqlComBackupDatabase As SqlCommand = New SqlCommand("Backup database ' test ' to CompanyBackup", sqlcon).
      When you don't use a variable your code looks like this:

      Code:
      Dim sqlComBackupDatabase As SqlCommand = New SqlCommand("Backup database test to CompanyBackup", sqlcon)
      You see the difference?
      ya i know

      but i cant use a static value because there is going to be different backups to make....

      when i insert something from user textbox i use this

      Code:
      dim sqlcom as sqlcommand = new sqlcommand("insert into Table(employeename,employeesurname)Values('" & txtName.text & "','" & txtSurname.text & "')",sqlcon)
      thats what i used to get the inputted values from the textboxes....

      why wouldnt it work in the command im doing for the backup

      Comment

      • OuTCasT
        Contributor
        • Jan 2008
        • 374

        #4
        Originally posted by MrMancunian
        The difference between the two code parts you posted is the use of quotes (' '). In your first example, working with a variable for the companyname you use quotes.

        Code:
        Dim sqlComBackupDatabase As SqlCommand = New SqlCommand("Backup database ' " & strCompanyName &  " ' to CompanyBackup", sqlcon)
        When you use test as name the query will look like this:

        Code:
        Dim sqlComBackupDatabase As SqlCommand = New SqlCommand("Backup database ' test ' to CompanyBackup", sqlcon).
        When you don't use a variable your code looks like this:

        Code:
        Dim sqlComBackupDatabase As SqlCommand = New SqlCommand("Backup database test to CompanyBackup", sqlcon)
        You see the difference?

        ok well i found the problem

        Code:
        "Backup database " & strCompanyName & " to CompanyBackup"
        i jst took out the single quote.....

        Comment

        • MrMancunian
          Recognized Expert Contributor
          • Jul 2008
          • 569

          #5
          Originally posted by OuTCasT
          ok well i found the problem

          Code:
          "Backup database " & strCompanyName & " to CompanyBackup"
          i jst took out the single quote.....
          That's what I told you in first place <g>

          Comment

          Working...