Click here to Skip to main content
15,891,762 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:

Hi guys
I have this code it works fine but I want to choose where to save the backup file

VB
Private Sub frmMain_FormClosing(ByVal sender As Object, ByVal e As System.Windows.Forms.FormClosingEventArgs) Handles Me.FormClosing
       Dim reply As DialogResult = MessageBox.Show("Δημιουργία αντιγράφου ασφαλείας ?", "Ερώτημα", _
             MessageBoxButtons.YesNo, MessageBoxIcon.Question, MessageBoxDefaultButton.Button1)
       If reply = DialogResult.Yes Then
           sqlcon.ConnectionString = "Server=......\SQLEXPRESS;database=" + dbname + ";uid=sa;pwd=........;"
           Dim destdir As String = "G:\RDMSbackup"
           If Not System.IO.Directory.Exists(destdir) Then
               System.IO.Directory.CreateDirectory("G:\RDMSbackup")
           End If
           Try
               sqlcon.ConnectionString = "Server=.....\SQLEXPRESS;database=" + dbname + ";uid=sa;pwd=.....;"
               sqlcon.Open()
               sqlcmd = New SqlCommand("backup database  " + dbname + " to disk='" + destdir + "\" + DateTime.Now.ToString("ddMMyyyy_HHmmss") + ".Bak'", sqlcon)
               sqlcmd.ExecuteNonQuery()
               sqlcon.Close()
               'MessageBox.Show("Το Αντίγραφο της Βάσης Δεδομένων Δημιουργήθηκε")
               MsgBox("Το Αντίγραφο της Βάσης Δεδομένων Δημιουργήθηκε!", MsgBoxStyle.Information, "Πληροφορία")
           Catch ex As Exception
               'MessageBox.Show("Αποτυχία Δημιουργίας Αντιγράφου Ασφαλείας!")
               MsgBox("Αποτυχία Δημιουργίας Αντιγράφου Ασφαλείας!", MsgBoxStyle.Information, "Πληροφορία")
           End Try
       Else
           sqlSTR = "UPDATE Audit_Log SET LOGOUT ='" & TimeOfDay & "' WHERE User_ID =" & xUser_ID & " AND LOG_ID=" & LOGID
           ExecuteSQLQuery(sqlSTR)
       End If
   End Sub


Please help
Posted
Comments
Shanu2rick 15-Jan-13 6:49am    
I assume you are using winforms, right?
Then,
You can easily ask the user where to save the file by using the SaveFileDialog control or class.
jomachi 15-Jan-13 6:52am    
Yes my friend I am using winforms. Can you write an example. Thanks
_Vitor Garcia_ 15-Jan-13 10:33am    
Do what Shanu2rick pointed, just be careful with SaveFileDialog as far as it browses in local HD, not in the SQL server HD

hmm....

VB
Private Sub frmMain_FormClosing(ByVal sender As Object, ByVal e As System.Windows.Forms.FormClosingEventArgs) Handles Me.FormClosing
       Dim reply As DialogResult = MessageBox.Show("Δημιουργία αντιγράφου ασφαλείας ?", "Ερώτημα", _
             MessageBoxButtons.YesNo, MessageBoxIcon.Question, MessageBoxDefaultButton.Button1)
       If reply = DialogResult.Yes Then
           sqlcon.ConnectionString = "Server=......\SQLEXPRESS;database=" + dbname + ";uid=sa;pwd=........;"

           Dim destdir As String
           Dim dlg As SaveFileDialog
           dlg = New SaveFileDialog
           
           If dlg.ShowDialog = Windows.Forms.DialogResult.OK Then
               destdir = (CType(dlg.FileName, String))
           Else
               Exit Sub
           End If
         
           'If Not System.IO.Directory.Exists(destdir) Then
           '    System.IO.Directory.CreateDirectory("G:\RDMSbackup")
           'End If
           Try
               sqlcon.ConnectionString = "Server=.....\SQLEXPRESS;database=" + dbname + ";uid=sa;pwd=.....;"
               sqlcon.Open()
               sqlcmd = New SqlCommand("backup database  " + dbname + " to disk='" + destdir + "\" + DateTime.Now.ToString("ddMMyyyy_HHmmss") + ".Bak'", sqlcon)
               sqlcmd.ExecuteNonQuery()
               sqlcon.Close()
               'MessageBox.Show("Το Αντίγραφο της Βάσης Δεδομένων Δημιουργήθηκε")
               MsgBox("Το Αντίγραφο της Βάσης Δεδομένων Δημιουργήθηκε!", MsgBoxStyle.Information, "Πληροφορία")
           Catch ex As Exception
               'MessageBox.Show("Αποτυχία Δημιουργίας Αντιγράφου Ασφαλείας!")
               MsgBox("Αποτυχία Δημιουργίας Αντιγράφου Ασφαλείας!", MsgBoxStyle.Information, "Πληροφορία")
           End Try
       Else
           sqlSTR = "UPDATE Audit_Log SET LOGOUT ='" & TimeOfDay & "' WHERE User_ID =" & xUser_ID & " AND LOG_ID=" & LOGID
           ExecuteSQLQuery(sqlSTR)
       End If
   End Sub
 
Share this answer
 

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