PHP User Warning: fetch_template() calls should be replaced by the vB_Template class. Template name: bbcode_highlight in ..../includes/functions.php on line 4197

PHP User Warning: fetch_template() calls should be replaced by the vB_Template class. Template name: bbcode_highlight in ..../includes/functions.php on line 4197

PHP User Warning: fetch_template() calls should be replaced by the vB_Template class. Template name: bbcode_highlight in ..../includes/functions.php on line 4197
VS 2008 [RESOLVED] Finding local database for click once application and making a backup copy-VBForums
Results 1 to 9 of 9

Thread: [RESOLVED] Finding local database for click once application and making a backup copy

  1. #1

    Thread Starter
    Member
    Join Date
    Aug 2016
    Posts
    43

    Resolved [RESOLVED] Finding local database for click once application and making a backup copy

    Hi everyone, I need to backup my local.sdf database that will go on a client machine.
    I have spent quite a while searching for the database but cannot find it.
    Please can someone give advise on this problem.

    I also need to be able to make a copy of the database and send it to the desktop for emailing to another person.
    Any idea's that may point me in the right direction, thanks.

  2. #2
    .NUT jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    100,038

    Re: Finding local database for click once application and making a backup copy

    For a ClickOnce app, you should be able to use ApplicationDeployment.CurrentDeployment.DataDirectory to get that folder path.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  3. #3

    Thread Starter
    Member
    Join Date
    Aug 2016
    Posts
    43

    Re: Finding local database for click once application and making a backup copy

    Thanks, that gets me to the file, most grateful, now just need to work out how to make copy and send it to the desktop.

  4. #4

    Thread Starter
    Member
    Join Date
    Aug 2016
    Posts
    43

    Re: Finding local database for click once application and making a backup copy

    OK, here is what I have, the problem is it throws an error because the database is still in use. I have tried to close it but it still gives the same error.
    Any Idea's??

    Code:
    Private Sub PeopleBindingNavigatorSaveItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles PeopleBindingNavigatorSaveItem.Click
            Me.Validate()
            Me.PeopleBindingSource.EndEdit()
            Me.TableAdapterManager.UpdateAll(Me.Service1DataSet1)
    
        End Sub
    
        Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
            'TODO: This line of code loads data into the 'Service1DataSet1.people' table. You can move, or remove it, as needed.
            Me.PeopleTableAdapter.Fill(Me.Service1DataSet1.people)
    
        End Sub
    
        Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
            TextBox1.Text = ApplicationDeployment.CurrentDeployment.DataDirectory
    
    
        End Sub
    
        Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
            My.Computer.FileSystem.CopyFile(TextBox1.Text & "\" & "Service1.mdf", "C:\Desktop\Backup.sdf", overwrite:=False)
        End Sub
    
        Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Click
            Me.TableAdapterManager.Connection.Close()
            Me.PeopleTableAdapter.Connection.Close()
        End Sub
    End Class

  5. #5
    .NUT jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    100,038

    Re: Finding local database for click once application and making a backup copy

    Firstly, why write this:
    vb.net Code:
    1. My.Computer.FileSystem.CopyFile(TextBox1.Text & "" & "Service1.mdf", "C:\Desktop\Backup.sdf", overwrite:=False)
    rather than this:
    vb.net Code:
    1. My.Computer.FileSystem.CopyFile(TextBox1.Text & "\Service1.mdf", "C:\Desktop\Backup.sdf", overwrite:=False)
    It drives me nuts when I see people concatenating multiple literals.

    That said, it's considered good practice to use the Path.Combine or My.Computer.FileSystem.CombinePath method to join partial paths:
    vb.net Code:
    1. My.Computer.FileSystem.CopyFile(My.Computer.FileSystem.CombinePath(TextBox1.Text, "Service1.mdf"), "C:\Desktop\Backup.sdf", overwrite:=False)
    Secondly, why are you using a path with a ".mdf" extension as the source and one with a ".sdf" extension as the destination? One is for SQL Server databases and the other is for SQL Server CE databases so, despite the similarity in name, very much not the same thing.

    If your file is an MDF file then you are presumably using SQL Server Express, in which case your data file is probably still attached to the database server. Closing an ADO.NET connection won't cause the file to be detached. I'm not aware of how to force the file to be detached but I'd expect that there would be one, so that gives you something to search for. You could also wait for the underlying database connection to timeout by not using it for a while but I'm not sure how long that would actually take.

    If there's no other way, you could probably set a Boolean somewhere to indicate that a backup is pending and then call Application.Restart to restart your app, which should hopefully detach the file. You can then add some code at startup to check whether a backup is pending and process it there.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  6. #6

    Thread Starter
    Member
    Join Date
    Aug 2016
    Posts
    43

    Re: Finding local database for click once application and making a backup copy

    Thanks for your reply, first sorry for the mistake in the file, types they should have been both .mdf, your other point is noted.

    I am at a bit of a loss as to where to go from here, what do other application writers do to backup there most important data?

    I could use the following to find and open the file and give instructions to copy the file manually then close the application, but that seems a bit messy.
    Code:
    TextBox1.Text = ApplicationDeployment.CurrentDeployment.DataDirectory
            Process.Start("explorer.exe", (TextBox1.Text))

  7. #7
    .NUT jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    100,038

    Re: Finding local database for click once application and making a backup copy

    SQL Server Express is not a file-based database so you don't usually create a backup by copying a file. SQL Server has backup functionality build in so you can use that. I suggest that you do some research and, if you need more info on that specifically, post in the Database Development forum. You might start here.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  8. #8

    Thread Starter
    Member
    Join Date
    Aug 2016
    Posts
    43

    Re: Finding local database for click once application and making a backup copy

    Quote Originally Posted by jmcilhinney View Post
    SQL Server Express is not a file-based database so you don't usually create a backup by copying a file. SQL Server has backup functionality build in so you can use that. I suggest that you do some research and, if you need more info on that specifically, post in the Database Development forum. You might start here.
    Thank you for reply and advise.
    I do not fully understand databases in vb, but can usually manage to get things done.
    What I have discovered is that I can find the data file and copy it now, so that will properly do.
    The client will copy the file and send it to another location.

    Just to explain further, the "backup copy" of the data is required incase of either a computer malfunction, theft of computer, or perhaps flood, fire. So it needs to be kept at another location.
    From what I understand you are saying SQL can back up it's self, but I assume that it would be on the same machine, and therefore not safeguard against the above.

    Hope this makes sense.

  9. #9
    .NUT jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    100,038

    Re: Finding local database for click once application and making a backup copy

    SQL Server is a database server. It manages multiple databases and more. One of the functions it provides is database backup and restore. Invoking the SQL Server backup functionality generally produces a file with a ".bak" extension. That file is not a copy of the database but rather contains data that can be used to reconstruct the database so, for one thing, it will be smaller than the actual data file. You still end up with a file so you can do anything with that file that you can do with any other, e.g. store off-site.

    That said, if you want to restore a BAK file then you need a database to restore it into so you would need an attached MDF file in order to do it. Any MDF file would do but, if you need an MDF file anyway, having a copy of your original file is not necessarily a bad thing.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Featured


Click Here to Expand Forum to Full Width