0

I have a C# application that writes to an encrypted SQL Server Compact database. This is the code:

conn = new SqlCeConnection(connectionString);
ConnectionOpen();

SqlCeCommand cmd = conn.CreateCommand();
cmd.CommandText = "INSERT INTO TableName ([Date],[ParentURL],[ChildURL],[DateLongFormat]) VALUES (@EntryDate ,@parentUrl,@childUrl,@EntryDateLong)";

cmd.Parameters.AddWithValue("@EntryDate", EntryDate);
cmd.Parameters.AddWithValue("@parentUrl", parentUrl);
cmd.Parameters.AddWithValue("@childUrl", childUrl);
cmd.Parameters.AddWithValue("@EntryDateLong", EntryDate.ToString("yyyy/MM/dd hh:mm:ss.fff"));

int count = cmd.ExecuteNonQuery();

cmd.Dispose();
ConnectionClose();

Very occasionally (roughly 1/500 times) the EXE will crash throwing the following two errors (retrieved via Event Viewer Application Log):

[Source = .NET Runtime]
Description: The process was terminated due to an unhandled exception.
Exception Info: System.AccessViolationException
Stack:
   at System.Data.SqlServerCe.SqlCeCommand.ReleaseNativeInterfaces()
   at System.Data.SqlServerCe.SqlCeCommand.Dispose(Boolean)
   at System.Data.SqlServerCe.SqlCeCommand.Finalize()

[Source = Application Error]
Exception code: 0xc0000005
Fault offset: 0x000000000005030e
Faulting module path: C:\Program Files\Microsoft SQL Server Compact Edition\v4.0\sqlcese40.dll

Originally I thought that the error was caused by two threads trying to write simultaneously to the database, but SQL Server Compact supports multi-threading. I also changed the SQL Server Compact database schema to support string values with huge character limits, so it's not because its trying to write a string longer than the cell value either.

All of my C# code is executed within a try/catch, anyone know why the application is crashing, or what these stack traces signify?

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
  • Do you have any unmanaged code block within this sequence? https://msdn.microsoft.com/en-us/library/system.accessviolationexception.aspx "In programs consisting entirely of verifiable managed code, all references are either valid or null, and access violations are impossible. An AccessViolationException occurs only when verifiable managed code interacts with unmanaged code or with unsafe managed code." – Jean Nov 11 '15 at 23:06
  • @Jean Some of ms sql ce libraries are in unmanaged code. OP shoukd try to reinstall ms sql ce from scratch. – Shadow Nov 11 '15 at 23:10
  • Yeah, and its an unmanaged code, there is not much to do since they are not running under your CLR, they have their own runtime. maybe just handling the exception in the program instead of a total crush is the best thing to do. – Jean Nov 11 '15 at 23:13
  • Possible duplicate of [Access Violation Exception in SqlCeConnection dispose](http://stackoverflow.com/questions/32307252/access-violation-exception-in-sqlceconnection-dispose) – cbr Nov 11 '15 at 23:16
  • @Jean unmanaged code would be methods: CreateCommand, CommandText, Parameters.AddWithValue, and ExecuteNonQuery(). Also, creating the SqlCeConnection(connectionString). These are all located within a try/catch though, so I'm not sure exactly what is causing the crash. – WinterIsComing Nov 13 '15 at 14:36

0 Answers0