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"
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
.Files = _BACKUPFILE
.Database = _DATABASE
.BackupSetName = "MyDbBackupNameSet"
.BackupSetDescription = "Backup from VB.NET application"
_SQLServer.Connect(_INSTANCE, _USER, _PWD)
.SQLBackup(_SQLServer)
End With
MessageBox.Show("Backup berhasil dibuat", "Message", MessageBoxButtons.OK, MessageBoxIcon.Information)
End Sub
restore procedure:
Private Sub RestoreDB()
With oRestore
.Files = _BACKUPFILE
.Database = _DATABASE
.ReplaceDatabase = True
_SQLServer.Connect(_INSTANCE, _USER, _PWD)
.SQLRestore(_SQLServer)
End With
MessageBox.Show("Restore Successfull", "Message", MessageBoxButtons.OK, MessageBoxIcon.Information)
End Sub
Private Sub BackupDB()
With _Backup
.Files = _BACKUPFILE
.Database = _DATABASE
.BackupSetName = "MyDbBackupNameSet"
.BackupSetDescription = "Backup from VB.NET application"
_SQLServer.Connect(_INSTANCE, _USER, _PWD)
.SQLBackup(_SQLServer)
End With
MessageBox.Show("Backup berhasil dibuat", "Message", MessageBoxButtons.OK, MessageBoxIcon.Information)
End Sub
restore procedure:
Private Sub RestoreDB()
With oRestore
.Files = _BACKUPFILE
.Database = _DATABASE
.ReplaceDatabase = True
_SQLServer.Connect(_INSTANCE, _USER, _PWD)
.SQLRestore(_SQLServer)
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.
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!
0 comments:
Post a Comment
Write your constructive comment here...