<Global.Microsoft.VisualBasic.CompilerServices.DesignerGenerated()> _
Partial Class Form1
Inherits System.Windows.Forms.Form
'Form overrides dispose to clean up the component list.
<System.Diagnostics.DebuggerNonUserCode()> _
Protected Overrides Sub Dispose(ByVal disposing As Boolean)
Try
If disposing AndAlso components IsNot Nothing Then
components.Dispose()
End If
Finally
MyBase.Dispose(disposing)
End Try
End Sub
'Required by the Windows Form Designer
Private components As System.ComponentModel.IContainer
'NOTE: The following procedure is required by the Windows Form Designer
'It can be modified using the Windows Form Designer.
'Do not modify it using the code editor.
<System.Diagnostics.DebuggerStepThrough()> _
Private Sub InitializeComponent()
Dim DataGridViewCellStyle1 As System.Windows.Forms.DataGridViewCellStyle = New System.Windows.Forms.DataGridViewCellStyle()
Dim DataGridViewCellStyle2 As System.Windows.Forms.DataGridViewCellStyle = New System.Windows.Forms.DataGridViewCellStyle()
Me.ButtonBackup = New System.Windows.Forms.Button()
Me.ButtonRestore = New System.Windows.Forms.Button()
Me.DataGridView1 = New System.Windows.Forms.DataGridView()
Me.BackgroundWorker1 = New System.ComponentModel.BackgroundWorker()
Me.ProgressBar1 = New System.Windows.Forms.ProgressBar()
Me.Index = New System.Windows.Forms.DataGridViewTextBoxColumn()
Me.DatabaseDate = New System.Windows.Forms.DataGridViewTextBoxColumn()
Me.BackupDate = New System.Windows.Forms.DataGridViewTextBoxColumn()
CType(Me.DataGridView1, System.ComponentModel.ISupportInitialize).BeginInit()
Me.SuspendLayout()
'
'ButtonBackup
'
Me.ButtonBackup.Location = New System.Drawing.Point(12, 41)
Me.ButtonBackup.Name = "ButtonBackup"
Me.ButtonBackup.Size = New System.Drawing.Size(75, 23)
Me.ButtonBackup.TabIndex = 0
Me.ButtonBackup.Text = "Back up"
Me.ButtonBackup.UseVisualStyleBackColor = True
'
'ButtonRestore
'
Me.ButtonRestore.Location = New System.Drawing.Point(358, 41)
Me.ButtonRestore.Name = "ButtonRestore"
Me.ButtonRestore.Size = New System.Drawing.Size(75, 23)
Me.ButtonRestore.TabIndex = 1
Me.ButtonRestore.Text = "Restore"
Me.ButtonRestore.UseVisualStyleBackColor = True
'
'DataGridView1
'
Me.DataGridView1.AllowUserToAddRows = False
Me.DataGridView1.AllowUserToDeleteRows = False
Me.DataGridView1.AllowUserToResizeColumns = False
Me.DataGridView1.AllowUserToResizeRows = False
Me.DataGridView1.ColumnHeadersHeightSizeMode = System.Windows.Forms.DataGridViewColumnHeadersHeightSizeMode.AutoSize
Me.DataGridView1.Columns.AddRange(New System.Windows.Forms.DataGridViewColumn() {Me.Index, Me.DatabaseDate, Me.BackupDate})
Me.DataGridView1.Location = New System.Drawing.Point(12, 70)
Me.DataGridView1.MultiSelect = False
Me.DataGridView1.Name = "DataGridView1"
Me.DataGridView1.RowHeadersVisible = False
Me.DataGridView1.SelectionMode = System.Windows.Forms.DataGridViewSelectionMode.FullRowSelect
Me.DataGridView1.Size = New System.Drawing.Size(421, 199)
Me.DataGridView1.TabIndex = 2
'
'BackgroundWorker1
'
Me.BackgroundWorker1.WorkerReportsProgress = True
Me.BackgroundWorker1.WorkerSupportsCancellation = True
'
'ProgressBar1
'
Me.ProgressBar1.Location = New System.Drawing.Point(12, 12)
Me.ProgressBar1.Name = "ProgressBar1"
Me.ProgressBar1.Size = New System.Drawing.Size(421, 23)
Me.ProgressBar1.TabIndex = 3
'
'Index
'
Me.Index.DataPropertyName = "Index"
Me.Index.HeaderText = "#"
Me.Index.Name = "Index"
Me.Index.ReadOnly = True
Me.Index.Resizable = System.Windows.Forms.DataGridViewTriState.[False]
Me.Index.SortMode = System.Windows.Forms.DataGridViewColumnSortMode.NotSortable
Me.Index.Width = 30
'
'DatabaseDate
'
Me.DatabaseDate.DataPropertyName = "DatabaseDate"
DataGridViewCellStyle1.Format = "G"
DataGridViewCellStyle1.NullValue = Nothing
Me.DatabaseDate.DefaultCellStyle = DataGridViewCellStyle1
Me.DatabaseDate.HeaderText = "Database Date"
Me.DatabaseDate.Name = "DatabaseDate"
Me.DatabaseDate.ReadOnly = True
Me.DatabaseDate.Resizable = System.Windows.Forms.DataGridViewTriState.[False]
Me.DatabaseDate.SortMode = System.Windows.Forms.DataGridViewColumnSortMode.NotSortable
Me.DatabaseDate.Width = 185
'
'BackupDate
'
Me.BackupDate.DataPropertyName = "BackupDate"
DataGridViewCellStyle2.Format = "G"
DataGridViewCellStyle2.NullValue = Nothing
Me.BackupDate.DefaultCellStyle = DataGridViewCellStyle2
Me.BackupDate.HeaderText = "Back up Date"
Me.BackupDate.Name = "BackupDate"
Me.BackupDate.ReadOnly = True
Me.BackupDate.Resizable = System.Windows.Forms.DataGridViewTriState.[False]
Me.BackupDate.SortMode = System.Windows.Forms.DataGridViewColumnSortMode.NotSortable
Me.BackupDate.Width = 185
'
'Form1
'
Me.AutoScaleDimensions = New System.Drawing.SizeF(6.0!, 13.0!)
Me.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font
Me.ClientSize = New System.Drawing.Size(445, 281)
Me.Controls.Add(Me.ProgressBar1)
Me.Controls.Add(Me.DataGridView1)
Me.Controls.Add(Me.ButtonRestore)
Me.Controls.Add(Me.ButtonBackup)
Me.FormBorderStyle = System.Windows.Forms.FormBorderStyle.FixedSingle
Me.MaximizeBox = False
Me.MinimizeBox = False
Me.Name = "Form1"
Me.StartPosition = System.Windows.Forms.FormStartPosition.CenterScreen
Me.Text = "Form1"
CType(Me.DataGridView1, System.ComponentModel.ISupportInitialize).EndInit()
Me.ResumeLayout(False)
End Sub
Friend WithEvents ButtonBackup As System.Windows.Forms.Button
Friend WithEvents ButtonRestore As System.Windows.Forms.Button
Friend WithEvents DataGridView1 As System.Windows.Forms.DataGridView
Friend WithEvents BackgroundWorker1 As System.ComponentModel.BackgroundWorker
Friend WithEvents ProgressBar1 As System.Windows.Forms.ProgressBar
Friend WithEvents Index As System.Windows.Forms.DataGridViewTextBoxColumn
Friend WithEvents DatabaseDate As System.Windows.Forms.DataGridViewTextBoxColumn
Friend WithEvents BackupDate As System.Windows.Forms.DataGridViewTextBoxColumn
End Class
Form1.vb
Imports System.Data.OleDb
Imports System.IO
Public Class Form1
Private dbName As String
Private Sub Form1_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
dbName = "Nwind.accdb" ' เปลี่ยนชื่อ database ที่นี่
If Not File.Exists(String.Format("{0}\{1}", Application.StartupPath, dbName)) Then
MessageBox.Show("Cann't find database file.", "Error!", MessageBoxButtons.OK, MessageBoxIcon.Error)
Me.Close()
End If
Dim backupFolder As New DirectoryInfo(String.Format("{0}\backup", Application.StartupPath))
If Not backupFolder.Exists() Then
backupFolder.Create()
End If
BindData()
ButtonRestore.Enabled = False
End Sub
Private Sub ButtonBackup_Click(sender As System.Object, e As System.EventArgs) Handles ButtonBackup.Click
Dim result As DialogResult : result = MessageBox.Show("Do you want to back up database?", "Confrim", MessageBoxButtons.YesNo)
If result = Windows.Forms.DialogResult.Yes Then
ButtonBackup.Enabled = False
ButtonRestore.Enabled = False
DataGridView1.Enabled = False
BackgroundWorker1.RunWorkerAsync(Action.Backup)
End If
End Sub
Private Sub ButtonRestore_Click(sender As System.Object, e As System.EventArgs) Handles ButtonRestore.Click
Dim result As DialogResult : result = MessageBox.Show("Do you want to restore database?", "Confrim", MessageBoxButtons.YesNo)
If result = Windows.Forms.DialogResult.Yes Then
ButtonBackup.Enabled = False
ButtonRestore.Enabled = False
DataGridView1.Enabled = False
BackgroundWorker1.RunWorkerAsync(Action.Restore)
End If
End Sub
Private Sub BackgroundWorker1_DoWork(sender As System.Object, e As System.ComponentModel.DoWorkEventArgs) Handles BackgroundWorker1.DoWork
Dim act As Action : act = DirectCast(e.Argument, Action)
Select Case act
Case Action.Backup
BackUp()
Case Else
Restore()
End Select
End Sub
Private Sub BackgroundWorker1_ProgressChanged(sender As System.Object, e As System.ComponentModel.ProgressChangedEventArgs) Handles BackgroundWorker1.ProgressChanged
ProgressBar1.Value = e.ProgressPercentage
End Sub
Private Sub BackgroundWorker1_RunWorkerCompleted(sender As System.Object, e As System.ComponentModel.RunWorkerCompletedEventArgs) Handles BackgroundWorker1.RunWorkerCompleted
ButtonBackup.Enabled = True
ButtonRestore.Enabled = False
DataGridView1.Enabled = True
ProgressBar1.Value = 0
BindData()
End Sub
Private Sub DataGridView1_CellClick(sender As System.Object, e As System.Windows.Forms.DataGridViewCellEventArgs) Handles DataGridView1.CellClick
ButtonRestore.Enabled = True
End Sub
Private Sub BackUp()
Dim dbFile As New FileInfo(String.Format("{0}\{1}", Application.StartupPath, dbName))
Dim dbFolderPath As String : dbFolderPath = String.Format("{0}\backup\{1}", Application.StartupPath, dbFile.LastWriteTime.ToString("ddMMyyyyhhmmss"))
Dim dbFolder As New DirectoryInfo(dbFolderPath)
If Not dbFolder.Exists Then
dbFolder.Create()
BackgroundWorker1.ReportProgress(50)
dbFile.CopyTo(String.Format("{0}\{1}", dbFolderPath, dbFile.Name))
BackgroundWorker1.ReportProgress(100)
MessageBox.Show("Back up Complete!", "Complete")
Else
MessageBox.Show("Back up database is exist.", "Cann't back up!", MessageBoxButtons.OK, MessageBoxIcon.Error)
End If
End Sub
Private Sub Restore()
Dim dbFile As New FileInfo(String.Format("{0}\{1}", Application.StartupPath, dbName))
Dim selectedFolder As String : selectedFolder = Convert.ToDateTime(DataGridView1.SelectedRows.Item(0).Cells(1).Value).ToString("ddMMyyyyhhmmss")
Dim restoreFile As New FileInfo(String.Format("{0}\backup\{1}\{2}", Application.StartupPath, selectedFolder, dbName))
If Not DateTime.Compare(dbFile.LastWriteTime, restoreFile.LastWriteTime) = 0 Then
BackgroundWorker1.ReportProgress(50)
dbFile.CopyTo(String.Format("{0}\{1}", Application.StartupPath, dbFile.Name), True)
BackgroundWorker1.ReportProgress(100)
MessageBox.Show("Restore Complete!", "Complete")
Else
MessageBox.Show("Back up and restore database is same.", "Cann't restore!", MessageBoxButtons.OK, MessageBoxIcon.Error)
End If
End Sub
Private Sub BindData()
Dim dbFile As New FileInfo(String.Format("{0}\{1}", Application.StartupPath, dbName))
Me.Text = String.Format("Database: {0}", dbFile.LastWriteTime.ToString("dd/MM/yyyy hh:mm:ss"))
Dim backupFolder As New DirectoryInfo(String.Format("{0}\backup", Application.StartupPath))
Dim childFolder() As DirectoryInfo : childFolder = backupFolder.GetDirectories()
Dim dt As New DataTable
dt.Columns.Add("Index", GetType(Integer))
dt.Columns.Add("DatabaseDate", GetType(Date))
dt.Columns.Add("BackupDate", GetType(Date))
Dim count As Integer : count = 0
For Each child As DirectoryInfo In childFolder
count = count + 1
Dim backupFile As New FileInfo(String.Format("{0}\{1}", child.FullName, dbName))
Dim dr As DataRow : dr = dt.NewRow()
dr("Index") = count
dr("DatabaseDate") = backupFile.LastWriteTime
dr("BackupDate") = child.CreationTime
dt.Rows.Add(dr)
Next
DataGridView1.DataSource = dt
DataGridView1.ClearSelection()
End Sub
Private Enum Action
Backup
Restore
End Enum
End Class