Sabtu, 08 Januari 2011

Backup - Restore Database SQLServer 2005 by Coding Vb.Net

fuh.. sudah lama rasanya gag posting blog.. kali ni sya mau kasih coding nya vb.net untuk Backup Restore ..

gini, stiap kali anda bikin program database dan program tersebut dioperasikan, tentu saja sering terjadi salah input data, data hilang, atau masalah - masalah yang lainya. dengan cara ini anda dapat SAVE - LOAD database anda dengan sangat mudah !

Let's Check it Out.. :D

1. Coding untuk Backup Database (Terletak di Event Button)

Private Sub ButtonX2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ButtonX2.Click
With SaveFileDialog1
.Filter = "File Database (*.bak)|*.bak|" & "(*.*)|*.*"
.FilterIndex = 1
.Title = "Kotak Dialog BACKUP FILE"
.OverwritePrompt = True
If .ShowDialog = Windows.Forms.DialogResult.OK Then
Try
nama_file = .FileName

conn.Close()
comm = New SqlCommand("backup database DbItutor_v02 to disk='" & nama_file & "'", conn)
conn.Open()
comm.ExecuteNonQuery()
conn.Close()

MsgBox("File Telah Disimpan di " & nama_file, MsgBoxStyle.Information)

Catch ex As Exception
MessageBox.Show(ex.Message)
End Try
End If
End With
End Sub


1. Coding untuk Restore Database (Terletak di Event Button)

Private Sub ButtonX3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ButtonX3.Click
With OpenFileDialog1
.Filter = "File Database (*.bak)|*.bak|" & "(*.*)|*.*"
.FilterIndex = 1
.Title = "Kotak Dialog Restore File"
If .ShowDialog = Windows.Forms.DialogResult.OK Then
Try
nama_file = .FileName

conn.Close()

comm = New SqlCommand("Use Master Alter Database [DbItutor_v02] SET SINGLE_USER With ROLLBACK IMMEDIATE RESTORE DATABASE DbItutor_v02 FROM DISK = N'" & nama_file & "' WITH REPLACE, NOUNLOAD, STATS = 10 USE master ALTER DATABASE [DbItutor_v02] SET MULTI_USER;", conn)
conn.Open()
comm.ExecuteNonQuery()
conn.Close()

MsgBox("Restore Data Berhasil")
Catch ex As Exception
MessageBox.Show(ex.Message)
End Try
End If
End With
End Sub
Categories:

0 komentar:

Posting Komentar