Sunday, March 29, 2015

Membuat backup database access dengan vb.net



Prinsip kerja backup database access ini adalah meng-copy database ke folder tertentu. Hal ini perlu dilakukan dengan tujuan untuk menyelamatkan data jika terjadi sesuatu yang tidak diinginkan. Langkah-langkah yang harus dilakukan dalah sebagai berikut :
1.       Tambahkan drive listbox ke dalam form
2.       Tambahkan dirlistbox ke dalam form
3.       Tambahkan filelistbox ke dalam form
4.       Tambahkan texbox dan button


Kemudian buatlah coding di bawah ini :

Public Class BackupData
    Private Sub DriveListBox1_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles DriveListBox1.SelectedIndexChanged
        DirListBox1.Path = DriveListBox1.Drive
    End Sub

    Private Sub DirListBox1_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles DirListBox1.SelectedIndexChanged
        'FileListBox1.Pattern = "(*.mdb) |*.mdb"
        FileListBox1.Path = DirListBox1.Path
    End Sub

    Private Sub FileListBox1_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles FileListBox1.SelectedIndexChanged
        TextBox1.Text = FileListBox1.Path & "\" & FileListBox1.FileName
    End Sub

    Private Sub DriveListBox2_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles DriveListBox2.SelectedIndexChanged
        DirListBox2.Path = DriveListBox2.Drive
    End Sub

    Private Sub DirListBox2_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles DirListBox2.SelectedIndexChanged
        TextBox2.Text = DirListBox2.Path & "\" & FileListBox1.FileName
    End Sub

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        Try
            If TextBox1.Text = "" Then
                MsgBox("Anda belum memilih file yang akan dicopy")
                Exit Sub
            ElseIf TextBox2.Text = "" Then
                MsgBox("Anda tidak memilih direktori tujuan peng-Copy-an")
                Exit Sub
            End If
            My.Computer.FileSystem.CopyFile(TextBox1.Text, TextBox2.Text)

            MsgBox("Copy File sukses")
            TextBox1.Clear()
            TextBox2.Clear()
        Catch ex As Exception
            MsgBox(ex.Message)
        End Try

    End Sub

    Private Sub BackupDatabase_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        TextBox1.Clear()
        TextBox2.Clear()
        DriveListBox1.Drive = "C:\"
        DriveListBox2.Drive = "C:\"
        FileListBox1.FileName = ""
    End Sub

    Private Sub BackupData_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

    End Sub
End Class


EmoticonEmoticon