469,640 Members | 1,579 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,640 developers. It's quick & easy.

Problem in copying SQLite file in C# code

I 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.

Oct 26 '07 #1
0 1378

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

9 posts views Thread by Zoo Keeper | last post: by
12 posts views Thread by John Salerno | last post: by
4 posts views Thread by Jim Carlock | last post: by
3 posts views Thread by ricardo.turpino | last post: by
4 posts views Thread by mahdaeng | last post: by
2 posts views Thread by Joe Goldthwaite | last post: by
reply views Thread by Guilherme Polo | last post: by
reply views Thread by Joe Goldthwaite | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.