Problem in copying SQLite file in C# code
am 27.10.2007 01:09:33 von mahdaengI have a little Windows application written in C# with a SQLite back-
end. I'm using the System.Data.SQLite provider.
One of the features the application provides is a database back-up,
which just basically copies the S3DB file to a specified location. See
the code below:
//------------------------------------------------
System.IO.File.Copy(srcPath, destPath, true);
//------------------------------------------------
The file is copied without any problems, but when the application
attempts to access the original S3DB file again, the following
exception is thrown:
--------------------------------------------------
System.Data.SQLite.SQLiteException was unhandled
Message="Unable to open the database file"
Source="System.Data.SQLite"
ErrorCode=-2147467259
StackTrace:
....etc...
--------------------------------------------------
If I shut the application down and then start it up again, I can
access the database again with no problems.
Suspicious of what the File.Copy() method might be doing behind the
scenes, I tried using the following approach instead to hopefully
ensure that the files were being released:
//------------------------------------------------
using (FileStream fsSrc = new FileStream(srcPath, FileMode.Open))
{
//Get the source length once so we don't have to keep retrieving it
later
int srcLen = (int)fsSrc.Length;
//Create the destination file (this will overwrite the destination
file if it already exists)
using (FileStream fsDest = File.Create(destPath, srcLen))
{
//Buffer the contents of the source file
Byte[] buffer = new Byte[srcLen];
fsSrc.Read(buffer, 0, srcLen);
//Write buffered contents to the new file
fsDest.Write(buffer, 0, srcLen);
}
}
//------------------------------------------------
Unfortunately, the same problem occurs. I even tried executing the
code in its own thread, but with the same results.
Any ideas why this is happening and how to fix it?
Thank you.