How to backup开发者_StackOverflow中文版 ms access database in vb.net? We're gonna make a system for a certain company but our teacher doesn't teach us at all, please help. Any idea on how to do it?
The simplest way to make a backup of an Access-Database (or any one-file-databases) is to simply copy the file. But watch out for Exception because the file might be locked.
System.IO.File.Copy( _
"C:\Your\original\database.mdb", _
String.Format("D:\BackUps\{0:yyyyMMdd}.mdb", Date.Today) _
)
Additionally you could allow the user to specify the location and filename to which the file should be copied to. That's a rather trivial approach with simply utilizing the System.Windows.Forms
controls OpenFileDialog
and SaveFileDialog
.
Using openDialog As New OpenFileDialog()
openDialog.CheckFileExists = True
openDialog.CheckPathExists = True
openDialog.Filter = "Microsoft Access Database (*.mdb)|*.mdb"
openDialog.RestoreDirectory = True
Using saveDialog As New SaveFileDialog()
saveDialog.CheckFileExists = False
saveDialog.CheckPathExists = True
saveDialog.FileName = Date.Now.ToString("yyyyMMdd") & ".mdb"
saveDialog.Filter = "Microsoft Access Database (*.mdb)|*.mdb"
saveDialog.RestoreDirectory = True
If openDialog.ShowDialog() = Windows.Forms.DialogResult.OK AndAlso saveDialog.ShowDialog() = Windows.Forms.DialogResult.OK Then
If File.Exists(openDialog.FileName) Then
File.Copy(openDialog.FileName, saveDialog.FileName)
End If
End If
End Using
End Using
In case you wonder what those RestoreDirectory
property is doing: Those two dialogs are moving the current directory of the application to the designated paths, which might yield interesting effects later on if you assume a still unchanged current directory. To prevent this behavior, we set this property.
Please, refer this: how to make backup and restore in vb[^]
I'd suggest to use the MS Access compact database method. See: How to compact a Microsoft Access database by using Visual Basic .NETenter link description here
If File.Exists(openDialog.FileName) Then
File.Copy(openDialog.FileName, saveDialog.FileName)
End If I GOT ERROR ON THIS
精彩评论