On those rare occasions when you have been able to reproduce the problem,
were you attempting to update from another thread? I'm still not clear how
your app works. It would be nice if changing the connection string solves
the issue, but I'd feel more comfortable if we knew what the cause really
is.

Signature
Ginny Caughey
Device Application Development MVP
> The majority of the time(98%) it is straight reads on the database
> with only the one thread. I have checked with the users when we have
> gotten the errors and they said they weren't updating. I have tried
> this myself updating each time and can only re-create the problem
> occasionally. I will try changing the connection string however to see
> if it helps. Thanks.
damiengoofy@hotmail.com - 26 Aug 2008 13:26 GMT
The program is a high volume entry program. It will do 60,000 to
80,000 reads on this database in an hour.The update is using a thread
to not interrupt the flow. I have been suspecting this is the problem,
but the users tell me they haven't always been updating.The only time
I've been able to recreate the problem is when I was updating, but
even then it doesn't always happen. I have changed the connection
string on one PC as a test, but I would be surprised if this solves
the problem. My next change would be change the update to not run in a
thread. I have searched through the code a number of times and can't
find any open connections or locks on the file.
Ginny Caughey MVP - 26 Aug 2008 14:16 GMT
I think you're right about what's happening. Removing the worker thread is a
good next step.

Signature
Ginny Caughey
Device Application Development MVP
> The program is a high volume entry program. It will do 60,000 to
> 80,000 reads on this database in an hour.The update is using a thread
[quoted text clipped - 6 lines]
> thread. I have searched through the code a number of times and can't
> find any open connections or locks on the file.
damiengoofy@hotmail.com - 26 Aug 2008 20:58 GMT
Removing the thread didn't help. Here is my update code.
I pass a string with the update query.
int ReturnValue = 0;
OpenMaster();//Check for shared connection
SqlCeCommand cmd = new SqlCeCommand(sql);
try
{
ReturnValue = MasterDatabase.ExecuteNonQuery(cmd);
}
catch (SqlCeException ex)
{
ReturnValue = ex.NativeError;
}
finally
{
cmd.Dispose();
}
return ReturnValue;
Is there something I should be doing differently here.
After I copy over a new database, I call Verify on the database, which
if it fails, I try Compact. This is where it fails on a sharing
violation.
Ginny Caughey MVP - 26 Aug 2008 21:28 GMT
I'm not seeing anything necessarily wrong here - perhaps somebody else will
spot something.
For performance reasons you'd do better caching your update statement and
just refreshing the SQL parameters. You'd also get better performance using
SqlCeResultSet than a SQL statement. I think at this point I'd just
experiment with different update techniques and see if there's some other
approach that prevents the problem.

Signature
Ginny Caughey
Device Application Development MVP
> Removing the thread didn't help. Here is my update code.
> I pass a string with the update query.
[quoted text clipped - 23 lines]
> if it fails, I try Compact. This is where it fails on a sharing
> violation.
Doug - 26 Aug 2008 21:43 GMT
I don't see anything unusual either. I'm curious about this statement
in your first post:
"During periods of inactivity, a new copy of the database is copied
over writing the previous copy"
Can you explain in more detail how you're overwriting/copying the
database? I'm also curious as to why this is done.
Doug
damiengoofy@hotmail.com - 27 Aug 2008 12:42 GMT
The database contains master detail which is updated on another server
during the day. New masters are added and others changed and they want
the entry stations to have the most up to date data. They don't want
the operators to have to exit out of program.There is a windows
service that runs every 2 hours to strip the data into an sqlserver ce
database on a shared location. When the screen saver on the PC is
activated it triggers the entry program to copy a new database from
the shared location. Originally I was using File.Copy but changed it
to use filestream to copy to see if that would help. After the copy I
call verify to check for corruption. When I try to compact it, it
fails because it says the file is locked, however when I copy the
backup over it, it copies without any locking errors.
Doug - 27 Aug 2008 12:56 GMT
I'm thinking that the corruption occurs when you are copying the file,
not from your internal transactions. Perhaps a connection is being
attempted before the file is completely copied.
Maybe I'm not completely understanding your system, but why wouldn't
you just update/add/delete the necessary records in your existing
database instead of comletely overwriting the file?
Doug
damiengoofy@hotmail.com - 27 Aug 2008 13:53 GMT
We didn't do the update because we will have a minimum of 30 PCs
updating at the same time over the network and we were concerned about
the time and locking issues, however it may be one of the ideas to
consider if all else fails. There would be no connection during the
error. There are no other processes accessing the database at the
time.
Ginny Caughey MVP - 27 Aug 2008 14:17 GMT
I agree with Doug - using SqlCE inserts/updates/deletes makes more sense to
me too. You might want to look at Sync Services for ADO.NET for Devices:
http://www.microsoft.com/downloads/details.aspx?FamilyId=75FEF59F-1B5E-49BC-A21A
-9EF4F34DE6FC&displaylang=en

Signature
Ginny Caughey
Device Application Development MVP
> We didn't do the update because we will have a minimum of 30 PCs
> updating at the same time over the network and we were concerned about
> the time and locking issues, however it may be one of the ideas to
> consider if all else fails. There would be no connection during the
> error. There are no other processes accessing the database at the
> time.
damiengoofy@hotmail.com - 27 Aug 2008 14:24 GMT
We'll look into it. Thanks.
Doug - 27 Aug 2008 14:25 GMT
It still seems to me that the most likely source of the corruption is
in that copy process. Try copying the file to the device under a
"temporary" name that your app won't find. Then once it's completely
copied, rename it.
Also, are you certain that the file you're copying isn't corrupt to
start with?
damiengoofy@hotmail.com - 27 Aug 2008 19:37 GMT
Interesting idea. Would be quite simple to try. I am sure the file I
am copying is OK. I compact it when I create it and when I test I
usually can copy it without trouble.
I also know that first thing in the morning when the users login they
get a new file that works every time. I also know that if they exit
out and log back in they get a new file no problem. It does sound like
an open connection problem. I am also getting another set of eyes here
to look for an open connection I am not seeing. Does
CloseSharedConnections not close any open connections??
Ginny Caughey MVP - 27 Aug 2008 20:58 GMT
I think you'd be safer closing each connection explicitly yourself. You
could also check after doing that whether the connection's State is Open
after that as a sanity check.

Signature
Ginny Caughey
Device Application Development MVP
> Interesting idea. Would be quite simple to try. I am sure the file I
> am copying is OK. I compact it when I create it and when I test I
[quoted text clipped - 5 lines]
> to look for an open connection I am not seeing. Does
> CloseSharedConnections not close any open connections??
damiengoofy@hotmail.com - 28 Aug 2008 13:22 GMT
I sheepishly reply this morning to say after the 20th search through
my code I found an open local connection in a method I didn't think
was being used.If it had teeth it would have bit me. Thank you all for
your time and suggestions. I will be implementing your suggestions on
speed. Unfortunately, our main database is not SQLServer, so i don't
think we could easily implement Sync Services. I will put my changes
in production tonight and hopefully you won't hear from me again.
Doug - 28 Aug 2008 13:39 GMT
lol... I think we've all been there.
Doug