Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Where is the SQL Server Compact "repair utility?"

I have used C# System.Data.SqlServerCe version 3.5.1.0 to create a .SDF database file. On opening the connection after creation, I sometimes receive the following error:

The database file may be corrupted. Run the repair utility to check the database file. [ Database name = \?\C:\SomeDatabase.sdf ]

Where is the "repair utility?" How do we use it?

I have searched Google and StackOverflow for answers:

  • SQL Server Compact repair utility
  • SDF repair utility

This MSDN article talks about doing it programmatically. Is there a command line method?

like image 286
Shaun Luttin Avatar asked Apr 11 '14 00:04

Shaun Luttin


2 Answers

Here is a small C#.NET Console App that works:

class Program
{
    static void Main(string[] args)
    {
        SqlCeEngine engine = 
             new SqlCeEngine("Data Source = C:\\Users\\SomeUser\\Documents\\SomeDB.sdf");
        if (false == engine.Verify())
        {
            Console.WriteLine("Database is corrupted.");
            try
            {
                engine.Repair(null, RepairOption.DeleteCorruptedRows);
            }
            catch(SqlCeException ex)
            {
                Console.WriteLine(ex.Message);
            }
        }
        Console.WriteLine("Press any key to continue.");
        Console.ReadLine();
    }
}

Find further options in this MSDN Article. Execute in the command prompt with ScriptCs.

like image 135
Shaun Luttin Avatar answered Oct 21 '22 23:10

Shaun Luttin


You can use my sqlcecmd Tool to do this without writing any code. https://github.com/erikej/sqlcecmd

like image 5
ErikEJ Avatar answered Oct 21 '22 21:10

ErikEJ