backup/restore sql server database in 'C:\programFiles\StudentDatabase\' using vb.net & sql command.

EDN Admin

Well-known member
Joined
Aug 7, 2010
Messages
12,794
Location
In the Machine
hi...
i have a vb.net application having a Student database on .sqlexpress server. i have written codes to make a backup and restore using sql commands... Codes are below and working fine for directories other than C: . i want to
backup and restore my database using sql commands in C:Program FilesStudentDatabase_Setup. My codes are bellow...
<span style="text-decoration:underline Backup Code:-
<pre class="prettyprint lang-vb" style=" Private Sub BtnBackup_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles BtnBackup.Click

Dim SaveFileDialog1 As New SaveFileDialog
SaveFileDialog1.InitialDirectory = "C:Program FilesStudentDatabase_Setup"
SaveFileDialog1.Filter = "Backup Files (*.bak)|*.bak"
SaveFileDialog1.RestoreDirectory = True
SaveFileDialog1.Title = "Create Database Backup"
SaveFileDialog1.FileName = "StudentBackup@" + Now.ToString("dd-mm-yyyy_hh.mm.ss")
If SaveFileDialog1.ShowDialog() = System.Windows.Forms.DialogResult.OK Then
getting backup file path.
Dim path As String
path = System.IO.Path.GetFullPath(SaveFileDialog1.FileName)

Code to backup database.
Try
Backup In progress...
cmd = New SqlCommand("USE master; BACKUP DATABASE Student TO DISK = " & path.ToString & "", con)
con.Open()
cmd.ExecuteNonQuery()
MsgBox("Backup Succeed!", MsgBoxStyle.Information)
con.Close()
Catch ex As Exception
MsgBox(ex.Message)
con.Close()
End Try
End If
End Sub [/code]
<span style="text-decoration:underline Restore Code:-
<pre class="prettyprint lang-vb" style=" Private Sub BtnRestore_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles BtnRestore.Click

Dim OpenFileDialog1 As New OpenFileDialog
OpenFileDialog1.InitialDirectory = "C:Program FilesLulaSoftStudentDatabase_Setup"
OpenFileDialog1.Filter = "Backup Files (*.bak)|*.bak"
OpenFileDialog1.RestoreDirectory = True
OpenFileDialog1.Title = "Open backup file"
If OpenFileDialog1.ShowDialog() = System.Windows.Forms.DialogResult.OK Then
getting restore file path.
Dim path As String
path = System.IO.Path.GetFullPath(OpenFileDialog1.FileName)

Code to restore database.
Try
cmd = New SqlCommand("USE master ALTER DATABASE Student SET SINGLE_USER WITH ROLLBACK IMMEDIATE RESTORE DATABASE Student FROM DISK = " & path & " ALTER DATABASE Student SET MULTI_USER ;", con)
con.Open()
cmd.ExecuteNonQuery()
MsgBox("Data restored successfully.", MsgBoxStyle.Information)
con.Close()
Catch ex As Exception
MsgBox(ex.Message)
con.Close()
End Try
End If
End Sub[/code]
<br/>
<span style="text-decoration:underline B ut in both processes (backup and restore), while performing operation in <span style="text-decoration:underline C drive, i got an error as:-
C:Program FilesStudentDatabase_SetupStudentBackup1.bak. You dont have permission to save in this location. Contact the administrator to obtain permission.

.
how could i backup and restore from C:program files studentDatabasestudentDatabase.bak???

any help is appreciated... waiting 4 ur valuable response.

<br/>

View the full article
 
Back
Top