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);
}
2 に答える
0
分離ストレージ アクセス モードのパラメーターで、データを読み取るだけでなく、そこに書き込む可能性を正確に確認してください。
デバイスでテストしましたか?
于 2012-11-05T12:23:58.883 に答える
0
データベース接続を開いている間に、データベースファイルを読み込もうとしていることがわかります。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 に答える