-3
    using (EntityDataContext amdb = new EntityDataContext(StrConnectionString))
            {
                if (amdb.DatabaseExists())
                {
                    using (IsolatedStorageFile isoStore = IsolatedStorageFile.GetUserStoreForApplication())
                    {
                        if (!isoStore.FileExists(databaseName))
                        {
                            copyDatabase = true;
                        }
                        else
                        {
                            using (IsolatedStorageFileStream databaseStream = isoStore.OpenFile(databaseName, FileMode.Open, FileAccess.Read)) // error here
                            {
                                using (Stream db = Application.GetResourceStream(new Uri(databaseName, UriKind.Relative)).Stream)
                                {
                                    if (databaseStream.Length < db.Length)
                                        copyDatabase = true;
                                }
                            }
                        }
                    }
                }
                else
                {
                    //error with the database that has been packaged
                }
                if (copyDatabase)
                    new Worker().Copy(databaseName);
            }
4

2 回答 2

0

据我所知,您正在尝试读取数据库文件,而您仍然打开了数据库连接。由于 a DataContext 锁定了数据库(因此锁定了文件),因此不允许您同时读取它。

为了关闭数据库连接尝试关闭EntityDataContext对象(通过调用amdb.Close()或关闭 using 语句

尝试这样的事情:

bool shouldCopyDatabase = false;
bool databaseExists = false;

using (EntityDataContext amdb = new EntityDataContext(StrConnectionString))
{
    databaseExists = amdb.DatabaseExists();
}

if (databaseExists == true)
{
    using (IsolatedStorageFile isoStore = IsolatedStorageFile.GetUserStoreForApplication())
    {
        if (!isoStore.FileExists(databaseName))
        {
            copyDatabase = true;
        }
        else
        {
            using (IsolatedStorageFileStream databaseStream = isoStore.OpenFile(databaseName, FileMode.Open, FileAccess.Read)) // error here
            {
                using (Stream db = Application.GetResourceStream(new Uri(databaseName, UriKind.Relative)).Stream)
                {
                    if (databaseStream.Length < db.Length)
                        copyDatabase = true;
                }
            }
        }
    }
}

if (copyDatabase)
    new Worker().Copy(databaseName);

通过将隔离存储访问功能移到using (EntityDataContext amdb = new EntityDataContext(StrConnectionString))范围之外,您可以首先关闭数据库连接。

于 2012-11-05T18:06:28.117 回答
0

检查您是否准确,在隔离存储访问模式参数中,在其中写入数据而不是仅仅能够读取的可能性。

你用设备测试过吗?

于 2012-11-05T12:23:58.883 回答