Help in creating database backup backup for my system

ljpv14

Active member
Joined
Nov 28, 2011
Messages
44
Programming Experience
3-5
My code searches all system databases and back it up to a specific path.

Here is the code:


Imports System.Data
Imports System.Data.SqlClient


Public Class Backup
Dim con, con1 As SqlConnection
Dim cmd As SqlCommand
Dim dread As SqlDataReader


Private Sub Form3_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
server()
End Sub
Sub server()
con = New SqlConnection("Data Source=.\SQLEXPRESS;Integrated Security=True;User Instance=True")
con.Open()
cmd = New SqlCommand("select * from sys.Databases", con)
dread = cmd.ExecuteReader
While dread.Read
cmbserver.Items.Add(dread(0))
End While
dread.Close()
End Sub


Private Sub cmbserver_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmbserver.SelectedIndexChanged


End Sub
Sub query(ByVal que As String)
On Error Resume Next
cmd = New SqlCommand(que, con)
cmd.ExecuteNonQuery()
End Sub
Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick
If ProgressBar1.Value = 100 Then
Timer1.Enabled = False
ProgressBar1.Visible = False
MsgBox("Successfully Done")
Else
ProgressBar1.Value = ProgressBar1.Value + 5
End If
End Sub
Sub blank(ByVal str As String)

If str = "backup" Then
If cmbserver.Text = "" Then
MsgBox("Server Name & Database Blank Field")
Exit Sub
Else
SaveFileDialog1.ShowDialog()
Timer1.Enabled = True
ProgressBar1.Visible = True
Dim s As String
s = SaveFileDialog1.FileName
query("backup database " & cmbserver.Text & " to disk='" & s & "'")
End If
ElseIf str = "restore" Then
OpenFileDialog1.ShowDialog()
Timer1.Enabled = True
ProgressBar1.Visible = True
query("RESTORE DATABASE master FROM disk='" & OpenFileDialog1.FileName & "'")
End If
End Sub
Private Sub cmbbackup_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmbbackup.Click
blank("backup")
End Sub


Private Sub cmdrestore_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdrestore.Click
blank("restore")
End Sub
End Class


I can back up the master database but the database of my system doesn't seem to backed up. I don't what's wrong. The search for systems databases display my current database in a path. C:\Users\Luke\Desktop\WindowsApplication1\WindowsApplication1\Avis.mdf but it won't back up. I will also use the backup for restoration of database. HELP PLEASE!
 

Latest posts

Back
Top