Windows Phone 7中的SqlCeException

时间:2013-09-18 00:04:48

标签: windows-phone-7 sql-server-ce

我一直在使用Microsoft Live API上传和播放下载数据库。但下载或上传后,如果我试图访问数据库,我的应用程序提供SqlCeException未处理&退出。 如果我在访问数据库之前重新启动应用程序,它不会给出任何错误,所以现在解决方案是

重新启动应用程序

这是我的代码

    IsolatedStorageFileStream fileStream = null;

    private void Upload_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? This Will Overwrite Your Old Backup File!", "Backup?", MessageBoxButton.OKCancel) == MessageBoxResult.OK)
            {
                UploadDatabase();
            }
        }
    }

    public void UploadDatabase()
    {
        if (SDFolderID != string.Empty)
        {
            WLInfo.Text = "Uploading Backup...";

            this.client.UploadCompleted += new EventHandler<LiveOperationCompletedEventArgs>(ISFile_UploadCompleted);

            try
            {
                using (IsolatedStorageFile store = IsolatedStorageFile.GetUserStoreForApplication())
                {
                    fileStream = store.OpenFile("DB.sdf", FileMode.Open, FileAccess.Read);
                    client.UploadAsync(SDFolderID, "DB.sdf", fileStream, OverwriteOption.Overwrite);
                    WLInfo.Text = "Upload Complete.";
                }
            }
            catch
            {
                WLInfo.Text = "Error: Restart Application.";
            }
        }
    }

    private void ISFile_UploadCompleted(object sender, LiveOperationCompletedEventArgs args)
    {
        if (args.Error == null)
        {
            client = new LiveConnectClient(session);
            client.GetCompleted += new EventHandler<LiveOperationCompletedEventArgs>(GetFiles_GetCompleted);
            client.GetAsync(SDFolderID + "/files");
        }
        else
        {
            this.WLInfo.Text = "Error Uploading Backup File.";
        }
        fileStream.Close();
    }

    void GetFiles_GetCompleted(object sender, LiveOperationCompletedEventArgs e)
    {
        List<object> data = (List<object>)e.Result["data"];

        foreach (IDictionary<string, object> content in data)
        {
            if (((string)content["name"]).Equals(FileName))
            {
                FileID = (string)content["id"];
            }
        }

        if (FileID != null)
        {
            WLInfo.Text = "Backup Found On Sky Drive.";
        }
        else
        {
            WLInfo.Text = "Backup Not Found On Sky Drive.";
        }
    }

1 个答案:

答案 0 :(得分:0)

我猜它可能是Stream没有正确关闭所以你的数据库文件仍然被锁定。上传或下载数据库文件时,请确保在所有一次性对象上使用using语句,以便自动正确处理所有Stream

在你的代码中没有处理文件流,这可能是造成问题的原因(你应该在本地文件中“保存”这个变量并在ISFile_UploadCompleted中调用dispose)。

此外,当您使用using时,无需在对象上调用dispose(无需store.Dispose();,当您退出使用范围时,它会自动完成)