2

我是一个初学者程序员。我的 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() 方法中尝试过,但我不完全确定我是否正确执行。

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

4

1 回答 1

0

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

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

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

于 2014-06-21T18:01:34.640 回答