Click here to Skip to main content
11,480,626 members (64,476 online)
Rate this: bad
good
Please Sign up or sign in to vote.
Hi all

Below is my restore command

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 8-Apr-13 1:33am
Code-Hunt7.1K
Edited 10-Apr-13 2:05am
v2

1 solution

Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

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).
  Permalink  
Comments
Snehasish Nandy at 12-Apr-13 2:49am
   
Ya can u give me some idea of the code...
tarhex at 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 at 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)

  Print Answers RSS
0 Sergey Alexandrovich Kryukov 294
1 OriginalGriff 190
2 CPallini 150
3 Richard MacCutchan 150
4 CHill60 130
0 Sergey Alexandrovich Kryukov 8,184
1 OriginalGriff 7,556
2 Sascha Lefèvre 3,114
3 Maciej Los 2,491
4 Richard Deeming 2,335


Advertise | Privacy | Mobile
Web01 | 2.8.150520.1 | Last Updated 11 Apr 2013
Copyright © CodeProject, 1999-2015
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100