10 Replies - 4470 Views - Last Post: 17 April 2014 - 09:14 AM Rate Topic: -----

#1 saturnuk  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 5
  • Joined: 08-August 11

Backup/Restore - SQL Compact Edition

Posted 31 October 2012 - 04:46 AM

Hi Guys,

I'm not English so I'll try to explain my problems as simple as possible :D
I've been trying to backup/restore SQLce database for one of my latest applications.
This is how my code looks like:

This button opens location in which user can save .sdf (backup):
private void button2_Click(object sender, EventArgs e)
        {
            DialogResult ofd = folderBrowserDialog1.ShowDialog();

            if (ofd == DialogResult.OK)
            {
                string backupPath = folderBrowserDialog1.SelectedPath;
                textBox4.Text = backupPath;
            }
        }


... then file is being saved in chosen location (backup):
private void button4_Click(object sender, EventArgs e)
        {
            string fileName = "Database.sdf";
            string sourcePath = Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location);
            string backupPath = textBox4.Text;

            string sourceFile = System.IO.Path.Combine(sourcePath, fileName);
            string destFile = System.IO.Path.Combine(backupPath, fileName);

            System.IO.File.Copy(sourceFile, destFile, true);
        }


To restore database user opens location with .sdf file:
 DialogResult ofd = folderBrowserDialog1.ShowDialog();

            if (ofd == DialogResult.OK)
            {
                string backupPath = folderBrowserDialog1.SelectedPath;
                textBox5.Text = backupPath;
            }
        }


...and restore file using this code:
try
            {
                string fileName = "Database.sdf";
                string sourcePath = textBox5.Text;
                string restorePath = Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location);
                //string restorePath = @"";

                string sourceFile = System.IO.Path.Combine(sourcePath, fileName);
                string destFile = System.IO.Path.Combine(restorePath, fileName);

                System.IO.File.Copy(sourceFile, destFile, true);
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }


I've checked all of this locally I mean after debugging and works perfectly.
When my application is published and than installed this code doesn't work. Throws an error 'Could not find file C:\Users\Lion\AppData\Local\Apps\2.0\J98YQDCL.P3L\3TX9ONZ9.XO3D\Database.sdf'

So its working before application is being published. Once is published and installed doesn't work.

Any ideas why? Could anyone please help me with this problem?
Thank you in advance for your help.

Is This A Good Question/Topic? 0
  • +

Replies To: Backup/Restore - SQL Compact Edition

#2 BigR1983  Icon User is offline

  • D.I.C Head

Reputation: 57
  • View blog
  • Posts: 221
  • Joined: 12-April 10

Re: Backup/Restore - SQL Compact Edition

Posted 31 October 2012 - 06:10 AM

I assume that the error you pasted here is from the messagebox?

I assume that Database.sdf exists in your source path?
Was This Post Helpful? 0
  • +
  • -

#3 RexGrammer  Icon User is offline

  • Coding Dynamo
  • member icon

Reputation: 182
  • View blog
  • Posts: 783
  • Joined: 27-October 11

Re: Backup/Restore - SQL Compact Edition

Posted 31 October 2012 - 07:01 AM

First off you should rename your controls. Button1 doesn't seem very informative, you should name your controls so that their name should give out their purpose. An example: instead of using Button1 you can use ButtonBackup.

If it works while debugging it, and crashes when you publish it, the only reason why that is happening (the only obvious one) is that the database isn't where the program thinks it is. So double-check that.
Was This Post Helpful? 0
  • +
  • -

#4 Curtis Rutland  Icon User is offline

  • (╯□)╯︵ (~ .o.)~
  • member icon


Reputation: 4577
  • View blog
  • Posts: 8,019
  • Joined: 08-June 10

Re: Backup/Restore - SQL Compact Edition

Posted 31 October 2012 - 07:23 AM

Well, you have a path there. Have you followed it to see if there's a database there? If not, that means you didn't set the database to be part of your application's output, and it won't be copied over.
Was This Post Helpful? 0
  • +
  • -

#5 saturnuk  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 5
  • Joined: 08-August 11

Re: Backup/Restore - SQL Compact Edition

Posted 31 October 2012 - 07:33 AM

Thanks for your replay.

I've checked this and this file doesn't exist in that directory but the question is why? Where I can find the Database.sdf file?
This is my first project and I'm still learning so could you please let me know where I supposed to look for this file?

System.IO.FileNotFoundException: Could not find file 'C:\Users\Lion\AppData\Local\Apps\2.0\J98YQDCL.P3L\3TX9ONZ9.XO3\cali..tion_b66fbc81589c75c7_0001.0000_012e4219e7fd7afb\Database.sdf'.
File name: 'C:\Users\Lion\AppData\Local\Apps\2.0\J98YQDCL.P3L\3TX9ONZ9.XO3\cali..tion_b66fbc81589c75c7_0001.0000_012e4219e7fd7afb\Database.sdf'

Thank you.
Was This Post Helpful? 0
  • +
  • -

#6 saturnuk  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 5
  • Joined: 08-August 11

Re: Backup/Restore - SQL Compact Edition

Posted 31 October 2012 - 07:47 AM

Quote

Well, you have a path there. Have you followed it to see if there's a database there? If not, that means you didn't set the database to be part of your application's output, and it won't be copied over.


Where I can include the database to be part of my application?
Properties -> Publish -> Application Files... -> and set Database.sdf to Include?

I've done it before and I can copy the database but when I copy this file back doesn't do any updates.

And if the application is removed and installed from scratch I get this error:
The database file cannot be found. Check the path to the database. [ Data Source = C:\Users\Lion\AppData\Local\Apps\2.0\Data\388E37H9.8D1\WNVXBCMA.8B4\cali..tion_b66fbc81589c75c7_0001.0000_012e421ae87c8abb\Data\Database.sdf ]
Was This Post Helpful? 0
  • +
  • -

#7 Curtis Rutland  Icon User is offline

  • (╯□)╯︵ (~ .o.)~
  • member icon


Reputation: 4577
  • View blog
  • Posts: 8,019
  • Joined: 08-June 10

Re: Backup/Restore - SQL Compact Edition

Posted 31 October 2012 - 07:52 AM

In your Solution, you should be able to right-click your database file and select properties. The properties window may already be up, so it'll just be focused. There should be a property called "Build Action". Set it to "Content" and it should become part of the output when you build. Then, I don't know what installer you used, so hopefully it will work when you rebuild and reinstall.
Was This Post Helpful? 0
  • +
  • -

#8 saturnuk  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 5
  • Joined: 08-August 11

Re: Backup/Restore - SQL Compact Edition

Posted 31 October 2012 - 08:01 AM

Built Action has been already set to Content.
I go to Build -> Publish, this is how I do my installation files.

Any ideas? Cheers!
Was This Post Helpful? 0
  • +
  • -

#9 Curtis Rutland  Icon User is offline

  • (╯□)╯︵ (~ .o.)~
  • member icon


Reputation: 4577
  • View blog
  • Posts: 8,019
  • Joined: 08-June 10

Re: Backup/Restore - SQL Compact Edition

Posted 31 October 2012 - 08:06 AM

Perhaps this will help. Follow the instructions here:

http://msdn.microsof...y/aa983326.aspx
Was This Post Helpful? 0
  • +
  • -

#10 saturnuk  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 5
  • Joined: 08-August 11

Re: Backup/Restore - SQL Compact Edition

Posted 31 October 2012 - 08:56 AM

I've done exactly how it is explained on this website. I've had everything set up like the first method shows and didn't work. When I've done the second method I get some errors after the application was installed.
My SQL Compact Edition is v3.5 not v4.0. Can this be the cause of my problems? Is it safe for my application to be updated to the latest version of SQLce? I really don't know what to do now :(.
Was This Post Helpful? 0
  • +
  • -

#11 Revival  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 1
  • Joined: 17-April 14

Re: Backup/Restore - SQL Compact Edition

Posted 17 April 2014 - 09:14 AM

For anyone with the same issue.

Delete |DataDirectory| from connection string.
Data Source=|DataDirectory|\<dbname>.sdf -> Data Source=<dbname>.sdf
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1