SSCE_M_DATABASECORRUPTED

  • Thread starter Thread starter Nathan
  • Start date Start date
N

Nathan

A customer has sent an SQL CE file that appears to be corrupt. .

Calls to the database return Native code 25017: SSCE_M_DATABASECORRUPTED.

I am not sure how it gets corrupted though I suspect there is a problem with
my software application.

What can cause a database file, presumably created correctly, to get
corrupt?

Can it happen if the program execution is terminated abnormally and a
connection is still open?

Can it happen if you write too much data to a field, etc?

Can it happen if you have a flaky storage card?

Any ideas?

Nathan
 
Nathan,
I too occasionally end up with a corrupted database. I've never been about
to track down the cause for certain, bit in my case I think it's caused by
the user accidentally ejecting the SD card when using my app.

To counter this, if I detect a corrupted database when initially opening the
connection, I give the user the option to try and repair it. Compacting the
database often fixes the corruption (but not always).

const int SSCE_M_DATABASECORRUPTED = 25017;

try
{
connection.Open();
}
catch (SqlCeException e)
{
// Check if database if corrupted
if (e.NativeError == SSCE_M_DATABASECORRUPTED && MessageBox.Show("The
database file " + connection.DataSource + " is corrupted; try and repair?",
"Error", MessageBoxButtons.YesNo, MessageBoxIcon.Exclamation,
MessageBoxDefaultButton.Button2) == DialogResult.Yes)
{
// Call DoEvents to make sure the MessageBox is no longer visible.
Application.DoEvents();
Cursor.Current = Cursors.WaitCursor;

try
{
string compactedFile =
System.IO.Path.GetDirectoryName(connection.DataSource) + @"\$" +
System.IO.Path.GetFileName(connection.DataSource);

if (System.IO.File.Exists(compactedFile))
{
System.IO.File.Delete(compactedFile);

}

using (System.Data.SqlServerCe.SqlCeEngine engine = new SqlCeEngine("Data
Source = " + connection.DataSource))
{
engine.Compact("Data Source = " + compactedFile);
}


if (System.IO.File.Exists(compactedFile))
{
// Delete the existing corrupted database file.
if (System.IO.File.Exists(connection.DataSource))
{
System.IO.File.Delete(connection.DataSource);
}


// Rename the compacted/repaired database file. Move is the same as rename.
System.IO.File.Move(compactedFile, connection.DataSource);

// Try and open the connection again.
connection.Open();
}
}
catch (SqlCeException e2)
{
}
finally
{
Cursor.Current = Cursors.Default;
}
}

Best Regards,
Phil.
 
Back
Top