Click here to Skip to main content
15,868,016 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
Hi all

Below is my restore command

SQL
RESTORE DATABASE " & cmbdatabase.Text & " FROM disk='" & OpenFileDialog1.FileName & "'  WITH MOVE 'a' TO 'C:\Backup\a.mdf', Move 'a_log' TO 'C:\Backup\a.ldf',REPLACE"


Now as you can see i am moving the .mdf and .ldf file to a location.so for the first time it's fine to move but from the second time it will not allow me to do so.although it's not giving any error ,but it's not giving the desired output

Please tell me the solution for this..

Thank you
Posted
Updated 10-Apr-13 1:05am
v2

1 solution

I think it has to do with the file names. If the files you want to replace has the same name with the ones you are using to replace them then it will prevent you from doing so.

From my own practice, when backing-up, I normally store the file names with dates and when restoring, I rename them to the file name that I used initially. This prevents names conflict and help to be exact with which files to restore (more like a restore point with dates).
 
Share this answer
 
Comments
[no name] 12-Apr-13 2:49am    
Ya can u give me some idea of the code...
tarhex 12-Apr-13 8:04am    
'backup database


Try
Dim backupdate, newname As String

'backup database
backupdate = DateString
newname = "C:\backup\dbase" & "" + backupdate + "" & ".mdf"


'check if a current day backup exist
If System.IO.File.Exists(newname) Then


'if yes, delete the former backup and backup the new one the new one

System.IO.File.Delete(newname)
System.IO.File.Copy(Application.StartupPath & "\db\dbase.mdf", newname)

Else

'backup database
System.IO.File.Copy(Application.StartupPath & "\db\dbase.mdf", newname)


End If

Dim mymsg As New myMsgbox

'show user where to click to upload logo
With mymsg

.txtError.Text = "Database Backup Successful!"
.ShowDialog()

End With


Catch ex As Exception

Dim mymsg As New myMsgbox

'show user where to click to upload logo
With mymsg

.txtError.Text = "Error Backing Up Database, try again later!"
.ShowDialog()

End With

End Try
tarhex 12-Apr-13 8:12am    
Try
'string to hold file name and stream

Dim fileStream As Stream

openDialogBox.InitialDirectory = "c:\backup\" 'initial directory
openDialogBox.Filter = "Software Database files (*.mdf)|*.mdf" 'file filters
openDialogBox.RestoreDirectory = True




' If user selects OK, store file name and stream.
If (openDialogBox.ShowDialog() = DialogResult.OK) Then
fileName = openDialogBox.FileName
fileStream = openDialogBox.OpenFile()
fileStream.Close()

System.IO.File.Delete(Application.StartupPath & "\db\dbase.mdf")
System.IO.File.Copy(fileName, Application.StartupPath & "\db\dbase.mdf")

Dim mymsg1 As New myMsgbox

'show user where to click to upload logo
With mymsg1

.txtError.Text = "Database Restored Successfully!"
.ShowDialog()

End With

End If

Catch ex As Exception


Dim mymsg1 As New myMsgbox


With mymsg1

.txtError.Text = "Error! Could Not Restore Database, try again!"
.ShowDialog()

End With


End Try

openDialogBox.Dispose()

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900