Backing up DataBase using VB.net

  • Thread starter Thread starter 1JRoberts
  • Start date Start date
1

1JRoberts

Guest
I have an application and Database that I have created in Visual Express 2015.


I'm able to connect and query the Data Base, but when I try to Backup the data base, I get the following error.

"Database 'TestDataBase' does not exist. I Made sure that the name is entered correctly. BACKUP DATABASE is terminating abnormally."

I can connect and query the Database, so I'm sure connection string is OK.

Public SQLCon As New SqlConnection With {.ConnectionString = "Data Source=(LocalDB)\MSSQLLocalDB;AttachDbFilename=c:\users\jeff\documents\visual studio 2015\Projects\Database Backup\Database Backup\TestDataBase.mdf;Integrated Security=True"}



Public Sub Backup()

Dim stgBackupQuery As String = "BACKUP DATABASE TestDataBase TO DISK='C:\TestDataBase.bak'"

Try

SQLCon.Open()

SQLCmd = New SqlCommand(stgBackupQuery, SQLCon)

SQLCmd.ExecuteNonQuery()

SQLCon.Close()

Catch ex As Exception

MsgBox(ex.Message)

If SQLCon.State = ConnectionState.Open Then

SQLCon.Close()

End If

End Try

End Sub

I tried to add a path to the Database within the stgBackupQuery string, but I got syntax errors.

Not sure what the problem is. I wonder if it could be due the program being run in debug mode in Visual Studio. But I'm not sure.


Thanks

Continue reading...
 
Back
Top