Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: VB.NET

Hi guys
I have this code it works fine but I want to choose where to save the backup file
 
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 15-Jan-13 1:45am
jomachi1.6K
Comments
Shanu2rick at 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 at 15-Jan-13 6:52am
   
Yes my friend I am using winforms. Can you write an example. Thanks
VitorHugoGarcia at 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
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 2

hmm....
 
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
  Permalink  

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 425
1 OriginalGriff 165
2 DamithSL 130
3 Dave Kreskowiak 120
4 Suvendu Shekhar Giri 110
0 OriginalGriff 7,645
1 DamithSL 5,614
2 Sergey Alexandrovich Kryukov 5,344
3 Maciej Los 4,961
4 Kornfeld Eliyahu Peter 4,539


Advertise | Privacy | Mobile
Web04 | 2.8.141223.1 | Last Updated 17 Jan 2013
Copyright © CodeProject, 1999-2014
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