备份数据库(.sdf)文件通过将其更改为.txt来skydrive

时间:2012-08-10 19:16:05

标签: backup isolatedstorage onedrive

我是初学程序员。我的Windows phone芒果应用程序中有一个数据库文件(MyDatabase.sdf)。我想要完成的是将MyDatabase.sdf文件复制并转换为MyDatabaseBackup.txt在隔离存储中,然后将其作为备份上传到skydrive。由于skydrive不支持上传.sdf文件,因此有些人建议使用此转换方法并使其工作。 所以我试图做同样的事情,但我无法将.sdf文件复制到隔离存储中的.txt文件中。这是我的代码......

//START BACKUP
    private void Backup_Click(object sender, RoutedEventArgs e)
    {
        if (client == null || client.Session == null)
        {
            MessageBox.Show("You must sign in first.");
        }
        else
        {
            if (MessageBox.Show("Are you sure you want to backup? This will overwrite your old backup file!", "Backup?", MessageBoxButton.OKCancel) == MessageBoxResult.OK)
                UploadFile();
        }
    }

    public void UploadFile()
    {
        if (skyDriveFolderID != string.Empty) //the folder must exist, it should have already been created
        {
            this.client.UploadCompleted
                += new EventHandler<LiveOperationCompletedEventArgs>(ISFile_UploadCompleted);

            infoTextBlock.Text = "Uploading backup...";
            dateTextBlock.Text = "";

            using (AppDataContext appDB = new AppDataContext(AppDataContext.DBConnectionString))
            {
                appDB.Dispose();
            }

            try
            {
                using (IsolatedStorageFile myIsolatedStorage = IsolatedStorageFile.GetUserStoreForApplication())
                {
                    if (myIsolatedStorage.FileExists("MyDatabase.sdf"))
                    {
                        myIsolatedStorage.CopyFile("MyDatabase.sdf", "MyDatabaseBackup.txt"); //This is where it goes to the catch statement.
                    }

                    this.client.UploadAsync(skyDriveFolderID, fileName, true, readStream , null);
                }
            }

            catch
            {
                MessageBox.Show("Error accessing IsolatedStorage. Please close the app and re-open it, and then try backing up again!", "Backup Failed", MessageBoxButton.OK);
                infoTextBlock.Text = "Error. Close the app and start again.";
                dateTextBlock.Text = "";
            }
        }
    }

    private void ISFile_UploadCompleted(object sender, LiveOperationCompletedEventArgs args)
    {
        if (args.Error == null)
        {
            infoTextBlock.Text = "Backup complete.";

            dateTextBlock.Text = "Checking for new backup...";

            //get the newly created fileID's (it will update the time too, and enable restoring)
            client = new LiveConnectClient(session);
            client.GetCompleted += new EventHandler<LiveOperationCompletedEventArgs>(getFiles_GetCompleted);
            client.GetAsync(skyDriveFolderID + "/files");
        }
        else
        {
            this.infoTextBlock.Text =
                "Error uploading file: " + args.Error.ToString();
        }
    }

以下是我在app.xaml.cs文件中创建数据库的方法。

// Specify the local database connection string.
        string DBConnectionString = "Data Source=isostore:/MyDatabase.sdf";

        // Create the database if it does not exist.
        using (AppDataContext appDB = new AppDataContext(AppDataContext.DBConnectionString))
        {
            if (appDB.DatabaseExists() == false)
            {
                //Create the database
                appDB.CreateDatabase();

                appDB.SubmitChanges();
            }
        }

有些人建议确保“没有进程/函数/线程打开sdf文件”。 我在UploadFile()方法中试过这个,但我不完全确定我是否正确完成了。

有人可以就这两个问题提供一些代码帮助。谢谢你的帮助!

1 个答案:

答案 0 :(得分:0)

首先,使用File.Copy方法创建本地副本,如下所示,然后上传.txt文件:

File.Copy (Path.Combine ([DbFileDir], MyDatabase.sdf), Path.Combine ([SomeLocalDir], MyDatabaseBackup.txt), true)

注意:您必须拥有原始/新本地文件夹的适当访问权限。 希望这会有所帮助。 RGDS,