I am using the following code snippet to copy/backup database
Public Sub backupData()
Try
Dim s As StreamWriter
Dim portfolioPath As String = Environment.CurrentDirectory
'MsgBox(Application.UserAppDataPath)
If Not Directory.Exists(LIC.My.Settings.BackupDirectory) Then
Directory.CreateDirectory(LIC.My.Settings.BackupDirectory)
File.Create(LIC.My.Settings.BackupDirectory & "\LIC.Mdf").Close()
File.Create(LIC.My.Settings.BackupDirectory & "\Backup log.rtf").Close()
s = New StreamWriter(LIC.My.Settings.BackupDirectory & "\Backup log.rtf", True)
s.WriteLine("This backup was initially taken on - " & Date.Now)
s.Flush()
s.Close()
FileCopy(portfolioPath & "\LIC.mdf", LIC.My.Settings.BackupDirectory & "\LIC.Mdf")
s = New StreamWriter(LIC.My.Settings.BackupDirectory & "\Backup log.rtf", True)
MsgBox("New directory and backup file created")
Else
FileCopy(portfolioPath & "\LIC.mdf", LIC.My.Settings.BackupDirectory & "\LIC.Mdf")
s = New StreamWriter(LIC.My.Settings.BackupDirectory & "\Backup log.rtf", True)
s.WriteLine("This backup was latest updated on - " & Date.Now)
s.Flush()
s.Close()
MsgBox("Back up completed successfully")
End If
Catch ex As Exception
Dim MessageString As String = "Report this error to the system administrator: " & ControlChars.NewLine & ex.Message
Dim TitleString As String = "Data Backup Failed"
MessageBox.Show(MessageString, TitleString, MessageBoxButtons.OK, MessageBoxIcon.Error)
End Try
End Sub
But i get an error *”The Process Cannot Access The File Because It Is Being Used By Another Process”*Any Idea How Can I Copy/Backup The Files??
You will not be able to copy the file unless you first shut down the SQL Server service.
Having said that, there is really no reason to use SQLDMO or the above method to back up the database; it can be done with a simple Transact SQL command:
This will back the database up to SQL Server’s default backup directory (which I believe you specify at installation).
There are large number of options available with the backup command and this MSDN entry is a very good starting point for learning more.