Backup dan Restore Database SQL Server 2005 Lewat Aplikasi Biasanya soal backup dan restore SQL Server, jarang ada program aplikasi yg mau menghandlenya, saia juga nggak tahu kenapa...Biasanya lebih suka diserahkan aja ama Database Server-nya dengan alasan security, agar bisa di schedulling dll... Lalu bagaimana kalau Database Server nggak boleh di remote, letaknya jauh dll ? Mungkin ada baiknya kalau kita bisa kasih fasilitas backup database di aplikasi kita sehingga aplikasi kita terlihat berkelas dan kaya akan feature maintenancenya. Disini kita akan pakai SMOObject yang merupakan object yang memang digunakan untuk memudahkan management Sql Server via aplikasi. Dibandingkan dengan SQL-DMO, SMO relatif lebih mudah digunakan dan sudah merupakan managed code tidak seperti SQL-DMO yang masih menggunakan COM. Sehingga ada issue performance kalau kita akses via .Net managed code. Untuk melakukan backup dan restore dibutuhkan suatu device yang digunakan sebagai media penyimpanan file backup. Device tersebut dapat berupa file, logical device, atau media tape. Dalam contoh ini saya menggunakan file sebagai media penyimpanan file backup yang berekstensi .bak. Class BackupDeviceItem sudah disediakan oleh SMO. Setelah backupdeviceitem nya dibuat, tambahkan backupdeviceitem tsb ke object Backup. Ketika backup berlangsung kita dapat membuat suatu notifikasi yang memperlihatkan progress backup atau restore tersebut. Dengan memanfaatkan event PercentComplete dari object backup atau restore maka server akan memberikan notifikasi persentase proses yang sedang berlangsung, dari sini kita dapat membuat sebuah progress bar atau label yang akan memperlihatkan prosentase proses backup dan restore. Info tersebut didapatkan dari class PercentCompleteEventArgs. Backup
Info persentase progress yang ditampilkan di label dan progressbar control didapatkan dari event PercentComplete
Info hasil backup database didapatkan dari event Complete.
Restore
Code: Imports SMOObject = Microsoft.SqlServer.Management.Smo Imports Microsoft.SqlServer.Management.Common Public Class frmBackupRestore Dim backup As SMOObject.Backup Dim restore As SMOObject.Restore Private Sub frmBackupRestore_Load(ByVal sender As System.Object, _ ByVal e As System.EventArgs) Handles MyBase.Load ToolStripProgressBar1.Visible = False lblBackUpServerName.Text = My.Settings.DataSource lblBackUpDbName.Text = My.Settings.Database lblRestoreServerName.Text = My.Settings.DataSource lblRestoreDbName.Text = My.Settings.Database End Sub Private Sub btnBackup_Click(ByVal sender As System.Object, _ ByVal e As System.EventArgs) Handles btnBackup.Click If txtFileBackupName.Text = String.Empty Then MsgBox("Nama file belum diisi") Exit Sub End If If MessageBox.Show("Backup database?", _ "Konfirmasi", MessageBoxButtons.YesNo) = _ Windows.Forms.DialogResult.No Then Exit Sub Cursor = Cursors.WaitCursor ToolStripProgressBar1.Visible = True Try '//buat backup device nya Dim bdi As New SMOObject.BackupDeviceItem bdi.Name = txtFileBackupName.Text bdi.DeviceType = SMOObject.DeviceType.File '//buat object backup backup = New SMOObject.Backup backup.Database = My.Settings.Database '//notifikasi untuk progress bar tiap 10% backup.PercentCompleteNotification = 10
backup.Devices.Add(bdi) backup.Initialize = True '//associate event percent complete '//dengan add handler nya untuk '//refresh progress bar value AddHandler backup.PercentComplete, _ AddressOf BackupPercentComplete AddHandler backup.Complete, _ AddressOf BackupComplete Dim server As New SMOObject.Server(My.Settings.DataSource) backup.SqlBackup(server) Catch ex As Exception ToolStripProgressBar1.Visible = False MsgBox(ex.Message) Finally Cursor = Cursors.Default End Try End Sub Private Sub btnBackupLocation_Click(ByVal sender As System.Object, _ ByVal e As System.EventArgs) Handles btnBackupLocation.Click If SaveFileDialog1.ShowDialog = _ Windows.Forms.DialogResult.OK Then txtFileBackupName.Text = SaveFileDialog1.FileName End If End Sub Private Sub btnRestore_Click(ByVal sender As System.Object, _ ByVal e As System.EventArgs) Handles btnRestore.Click If txtRestoreFileName.Text = String.Empty Then MsgBox("Nama file belum diisi") Exit Sub End If If MessageBox.Show("Apakah backup akan di restore?", _ "Konfirmasi", MessageBoxButtons.YesNo) = _ Windows.Forms.DialogResult.No Then Exit Sub Cursor = Cursors.WaitCursor ToolStripProgressBar1.Visible = True Try '//buat backup device nya '//tentukan nama file .bak '//set ke file device type, karena tipe device '//yang digunakan disini yaitu file Dim bdi As New SMOObject.BackupDeviceItem bdi.Name = txtRestoreFileName.Text bdi.DeviceType = SMOObject.DeviceType.File
restore = New SMOObject.Restore restore.Database = My.Settings.Database restore.Action = SMOObject.RestoreActionType.Database restore.PercentCompleteNotification = 10 restore.UnloadTapeAfter = True restore.ReplaceDatabase = True restore.Devices.Add(bdi) '//associate event percent complete '//dengan add handler nya untuk '//refresh progress bar value AddHandler restore.PercentComplete, _ AddressOf RestorePercentComplete AddHandler restore.Complete, _ AddressOf RestoreComplete Dim server As New SMOObject.Server(My.Settings.DataSource) restore.SqlRestore(server) Catch ex As Exception ToolStripProgressBar1.Visible = False MsgBox(ex.Message) Finally Cursor = Cursors.Default End Try End Sub Private Sub btnRestoreLocation_Click(ByVal sender As System.Object, _ ByVal e As System.EventArgs) Handles btnRestoreLocation.Click If OpenFileDialog1.ShowDialog = _ Windows.Forms.DialogResult.OK Then txtRestoreFileName.Text = OpenFileDialog1.FileName End If End Sub Private Sub BackupPercentComplete(ByVal sender As System.Object, _ ByVal e As SMOObject.PercentCompleteEventArgs) '//update progress bar value '//dan text label info ToolStripProgressBar1.Value = e.Percent lblInfo.Text = "Backup Progress " & e.Percent & " %" lblInfo.Refresh() End Sub Private Sub BackupComplete(ByVal sender As System.Object, _ ByVal e As ServerMessageEventArgs) '//hapus handler dari event nya RemoveHandler backup.PercentComplete, _ AddressOf BackupPercentComplete RemoveHandler backup.Complete, _
AddressOf BackupComplete lblInfo.Text = e.Error.Message ToolStripProgressBar1.Value = 0 ToolStripProgressBar1.Visible = False Cursor = Cursors.Default End Sub Private Sub RestorePercentComplete(ByVal sender As System.Object, _ ByVal e As SMOObject.PercentCompleteEventArgs) '//update progress bar value '//dan text label info ToolStripProgressBar1.Value = e.Percent lblInfo.Text = "Restore Progress " & e.Percent & " %" lblInfo.Refresh() End Sub Private Sub RestoreComplete(ByVal sender As System.Object, _ ByVal e As ServerMessageEventArgs) '//hapus handler dari event nya RemoveHandler restore.PercentComplete, _ AddressOf RestorePercentComplete RemoveHandler restore.Complete, _ AddressOf RestoreComplete lblInfo.Text = e.Error.Message ToolStripProgressBar1.Value = 0 ToolStripProgressBar1.Visible = False Cursor = Cursors.Default End Sub End Class
http://www.kaskus.us/showthread.php?t=2324143
http://herosetyanofario.wordpress.com/2011/06/15/membuat-license-key-untukaplikasi-vb-net-di-komputer-klien/