473,396 Members | 1,997 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

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
4 7942
Are you using the same connection to the SQL Lite database? Or are you
creating new objects?

I would recommend calling Dispose() on your original connection object
before doing the copy to see if it makes a difference.

--
Andrew Faust
andrew[at]andrewfaust.com
http://www.andrewfaust.com
"mahdaeng" <ma******@gmail.comwrote in message
news:11**********************@k79g2000hse.googlegr oups.com...
>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 #2
On Oct 26, 5:46 pm, "Andrew Faust" <and...@andrewfaust.comwrote:
Are you using the same connection to the SQL Lite database? Or are you
creating new objects?

I would recommend calling Dispose() on your original connection object
before doing the copy to see if it makes a difference.

--
Andrew Faust
andrew[at]andrewfaust.comhttp://www.andrewfaust.com

Thank you for your response, Andrew. I'm instantiating the connection
object on an as-needed basis, and I'm using the "using" keyword when I
do, so there shouldn't be any problems with the connection object.
Just to prove the point, I tried your suggestion and there wasn't any
change in the application's behavior.

At this point, I don't know if the culprit is System.IO or SQLite.

Thank you.

Oct 29 '07 #3
My gut feel is that it's a bug with the SQLite data provider. It sounds
like their implementation of Dispose isn't cleaning itself up correctly.
Perhaps they forgot to close a file. There are a series of tools you can
download from Microsoft
(http://www.microsoft.com/technet/sys...s/default.mspx) that lets you
delve in and see what's going on.

There is a tool called Filemon that will let you see all file activity in
real time. You can use that to find out what (if any) SQLite files are
still open. If that turns out to be the case you can log an issue in the
SQLite bug tracker. In addition to this, I recommend you actually fix the
issue and submit it back to the SQLite project. It's a good way to ensure
your bug gets fixed, and a way to give something back to the community.

--
Andrew Faust
andrew[at]andrewfaust.com
http://www.andrewfaust.com
"mahdaeng" <ma******@gmail.comwrote in message
news:11*********************@q5g2000prf.googlegrou ps.com...
On Oct 26, 5:46 pm, "Andrew Faust" <and...@andrewfaust.comwrote:
>Are you using the same connection to the SQL Lite database? Or are you
creating new objects?

I would recommend calling Dispose() on your original connection object
before doing the copy to see if it makes a difference.

--
Andrew Faust
andrew[at]andrewfaust.comhttp://www.andrewfaust.com


Thank you for your response, Andrew. I'm instantiating the connection
object on an as-needed basis, and I'm using the "using" keyword when I
do, so there shouldn't be any problems with the connection object.
Just to prove the point, I tried your suggestion and there wasn't any
change in the application's behavior.

At this point, I don't know if the culprit is System.IO or SQLite.

Thank you.
Oct 30 '07 #4

The generally recommended practice for backing up a SQLite database is
to first issue a "BEGIN EXCLUSIVE" transaction, do your backup, and
then rollback the transaction.

I haven't tried this with the System.Data.SQLite wrapper but it's
worth a try.

Another thing that could be interfering is connection pooling.. are
you using the latest wrapper with connection pooling turned on? If so
try turning it off.

And if you still have trouble you'll probably get a better response in
the SQLite specific forum here.

http://sqlite.phxsoftware.com/forums/default.aspx

HTH,

Sam

------------------------------------------------------------
We're hiring! B-Line Medical is seeking .NET
Developers for exciting positions in medical product
development in MD/DC. Work with a variety of technologies
in a relaxed team environment. See ads on Dice.com.

Oct 30 '07 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

9
by: Zoo Keeper | last post by:
For some reason, the Python preprocessing balks on the "\" line continuation character. Any ideas/thoughts? Is it just that my version of gcc is not supported? I don't "need" numeric in my...
12
by: John Salerno | last post by:
I've been looking around and reading, and I have a few more questions about SQLite in particular, as it relates to Python. 1. What is the current module to use for sqlite? sqlite3? or is that not...
4
by: Jim Carlock | last post by:
I added the following lines to PHP.INI. extension=php_pdo.dll extension=php_pdo_sqlite.dll extension=php_sqlite.dll specifically in that order. I noticed the extensions getting loaded are...
3
by: ricardo.turpino | last post by:
Hi, I've installed Mac Python 2.5. I'm running Mac OS X 10.4.10 on a Macbook 1.83GHz. I though that the python sqlite library was installed by default as part of Mac Python 2.5, however, I...
0
by: swathi123 | last post by:
hi all here im trying to create .csv file. my code is public class CSVFile { public static void main(String args) { String record=null; try {
1
by: swathi123 | last post by:
hi all here im trying to create .csv file. my code is public class CSVFile { public static void main(String args) { String record=null; try {
0
by: mahdaeng | last post by:
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...
2
by: Joe Goldthwaite | last post by:
I'm confused. (Not a new experience). I've got a web application running under Zope. I use the Wing IDE for testing and debugging. When trying to recreate problems that come up on the web, I...
0
by: Guilherme Polo | last post by:
On Tue, Jul 1, 2008 at 9:51 PM, Joe Goldthwaite <joe@goldthwaites.comwrote: You need sqlite itself (www.sqlite.org) and bindings, pysqlite 2 (http://oss.itsystementwicklung.de/trac/pysqlite/) or...
0
by: Joe Goldthwaite | last post by:
Thanks Guilherme. That helped. I guess I was thinking that pysqlite would automatically come with some version of sqlite. The fact that it doesn't is what was causing me to get the strange...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.