SQL Server 2005 Database Restore

Want to backup or restore databases but do not know how?
Maybe this can help you to backup or restore database using vb.net and sql server 2005. 

The first add a reference Database Management Objects (DMO) Library to use the classes that will be used to our project, right click on the project, "Add Reference", click the tab "COM", locate the component whose name is "Microsoft SQL DMO Object Library", then click ok.

The following script will run in the form :

Dim _SQLServer As New SQLDMO.SQLServer
Dim WithEvents _Backup As New SQLDMO.Backup
Dim WithEvents _Restore As New SQLDMO.Restore
Dim _INSTANCE As String = "YourMSSQLInstanceName/ServerName"
Dim _USER As String = "YourMSSQLUserLogin"
Dim _PWD As String = "YourLoginPassword"
Dim _BACKUPFILE As String = "c:\MyBackup.bkp"
Dim _DATABASE As String = "YourDbName"

create procedures for backup and restore :

backup procedure:
Private Sub BackupDB()
With _Backup
.Database = _DATABASE
.BackupSetName = "MyDbBackupNameSet"
.BackupSetDescription = "Backup from VB.NET application"
_SQLServer.Connect(_INSTANCE, _USER, _PWD)
End With
MessageBox.Show("Backup berhasil dibuat", "Message", MessageBoxButtons.OK, MessageBoxIcon.Information)
End Sub

restore procedure:
Private Sub RestoreDB()
With oRestore
.Database = _DATABASE
.ReplaceDatabase = True
_SQLServer.Connect(_INSTANCE, _USER, _PWD)
End With
MessageBox.Show("Restore Successfull", "Message", MessageBoxButtons.OK, MessageBoxIcon.Information)
End Sub

To perform backup / restore, we only live call the procedure above.
The example above is only the basis of how we can do backup / restore DB.
We can add "SaveFileDialog" for backups and "OpenFileDialog" to restore order to determine the location and the backup filename.

Good Luck!


Post a Comment

Write your constructive comment here...

About Me

Hi, I’m Yani and my Syszoom blog is to share everything about the latest information around the technology field. I started Syszoom blog in 2011 to help other people solve their problem about computer, software, etc. I spent over five years as a teacher, and Syszoom blog is implementation of my knowledge in information technology field. To contact me, please email me at ditsucces@gmail.com.
Powered by Blogger.