Although most examples use SMO to backup and restore, this is not needed at all and can lead to problems when using a different SQL Server version as Microsoft sometimes changes the SMO dll's and functionality.
Here are some examples that do not use SMO, taken from:
Sql Server Database backup from my local pc using vb.net windows application[
^]
Dim sqlConn As New SqlConnection("Data Source=(local);Initial Catalog=master;User ID=sa;Trusted_Connection=true")
sqlConn.Open()
Dim sCommand = "BACKUP DATABASE [DatabaseName] TO DISK = N'E:\Backup\Backup.bak' WITH COPY_ONLY"
Using sqlCmd As New SqlCommand(sCommand, sqlConn)
sqlCmd.ExecuteNonQuery()
End Using
sCommand = "RESTORE DATABASE [DatabaseName] FROM DISK = N'E:\Backup\TestBackup.bak' WITH REPLACE"
Using sqlCmd As New SqlCommand(sCommand, sqlConn)
sqlCmd.ExecuteNonQuery()
End Using
If the restore does not work correctly, try USE Master first, or do not use the WITH REPLACE.
It might also help to DROP the existing database first.
Also see:
sql server - Restore SQL Database with Replace option - Stack Overflow[
^]