Quantcast
Channel: .NET Framework Class Libraries forum
Viewing all articles
Browse latest Browse all 8156

Backup LocalDB using C#

$
0
0

Hi,

    I have written an application and in the application there is an option to backup a localDb database to the users "MyDocuments" directory.

Unfortunately I am having issues with the backup as it keeps failing during execution, here is some of my code;

Backup Code:

        private void CreateBackup(string database)
        {
            try
            {
                string appPath = Application.StartupPath;

                // Set backup folder
                var backupFolder = string.Format("{0}\\", Environment.GetFolderPath(Environment.SpecialFolder.Windows));
                var moveFileFolder = string.Format("{0}\\", Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments));
                backupFolder += string.Format("{0}", "Temp\\");
                var sqlConStrBuilder = new SqlConnectionStringBuilder(ConnString);

                // set backupfilename
                var backupFileName = String.Format("{0}{1}_{2}.bak", backupFolder, "WorkTracker", DateTime.Now.ToString("yyyyMMdd"));
                var moveFileName = String.Format("{0}{1}_{2}.bak", moveFileFolder, "WorkTracker", DateTime.Now.ToString("yyyyMMdd"));

                using (var connection = new SqlConnection(sqlConStrBuilder.ConnectionString))
                {
                    var dbLoc = string.Format("{0}\\AppData\\WorkTracker.mdf", appPath);
                    var query = String.Format("BACKUP DATABASE {0} TO DISK='{1}'", dbLoc, backupFileName);
                    using (var command = new SqlCommand(query, connection))
                    {
                        connection.Open();
                        command.ExecuteNonQuery();
                    }

                    if (File.Exists(moveFileName))
                    {
                        File.Delete(moveFileName);

                        // Move the file.
                        File.Move(backupFileName, moveFileName);
                        meBackupResults.Text += string.Format("Backup Completed: {0}", moveFileName);
                    }
                    else
                    {
                        // Move the file.
                        File.Move(backupFileName, moveFileName);
                        meBackupResults.Text += string.Format("Backup Completed: {0}", moveFileName);
                    }
                }
            }
            catch (Exception ex)
            {
                meBackupResults.Text += string.Format("Backup failure: {0}{1}{2}", ex.Message, Environment.NewLine, ex.StackTrace);
            }

        }
    }

Connection String: (Using dbBackup)

<connectionStrings><add name="WorkTracker" connectionString="XpoProvider=MSSqlServer;Data Source=(LocalDB)\V11.0;AttachDbFilename=\AppData\WorkTracker.mdf;Integrated Security=True"/><add name="dbBackup" connectionString="Data Source=(LocalDB)\V11.0;AttachDbFilename=\AppData\WorkTracker.mdf;Integrated Security=True" /></connectionStrings>

The error message I get is:

Backup failure: An attempt to attach an auto-named database for file \AppData\WorkTracker.mdf failed. A database with the same name exists, or specified file cannot be opened, or it is located on UNC share.

Any help would be greatly appreciated.

Regards..

Peter.


Viewing all articles
Browse latest Browse all 8156

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>