22

I need to update my SqlCE3.5 database to 4.0.

I get the following error message when I'm trying to access my 3.5 sdf/database using the new CE4.0 connection provide :-

System.Data.EntityException: The underlying provider failed on Open. ---> System.Data.SqlServerCe.SqlCeInvalidDatabaseFormatException: The database file has been created by an earlier version of SQL Server Compact. Please upgrade using SqlCeEngine.Upgrade() method.

Anyone know how I can do this? Obviously I need to do it programatcially but i'm not sure what I should do / what references I need to add, etc.

Cheers!

ErikEJ
  • 40,951
  • 5
  • 75
  • 115
Pure.Krome
  • 84,693
  • 113
  • 396
  • 647
  • 1
    Is it not obvious? Call `SqlCeEngine.Upgrade()`! Or is there a problem? Also note incompatibilities between the different versions. – leppie Jan 20 '11 at 07:50
  • Hint: some code examples, please. When i tried some code, it couldn't compile. – Pure.Krome Jan 20 '11 at 10:02
  • @leppie: i) Where? In what context? With what arguments? ii) SqlCeEngine doesn't contain a method named Upgrade(). The message is useless. – Tom W Apr 06 '12 at 17:10
  • 2
    See my blog post here for sample code: http://erikej.blogspot.com/2010/08/how-to-upgrade-version-3x-database-file.html – ErikEJ Jan 20 '11 at 12:06
  • where do i find the SqlCE 4.0 dll i need to reference? (it's not listed in the .NET references). Can i NuGet it? – Pure.Krome Jan 21 '11 at 00:28
  • @ErikEJ your blog is awesome. Simple and yummy cake. Just eat it. – Maheep Jan 21 '13 at 17:25
  • Will this work in upgrading from 2.0 to 4.0? I maintain a midievel app that uses version 2.0. I would like to at least be able to view the data in a utility so that I can easily see what's in those tables. – B. Clay Shannon-B. Crow Raven Apr 12 '13 at 15:54

2 Answers2

14

Call System.Data.SqlServerCe.SqlCeEngine.Upgrade. See MSDN, which contains sample code.

Timores
  • 14,439
  • 3
  • 46
  • 46
10

You needn't upgrade when you see this error:

The database file has been created by an earlier version of SQL Server Compact. Please upgrade using SqlCeEngine.Upgrade() method.

Simply:

  1. Right-click your Project in Solution Explorer
  2. Choose properties, go to References tab
  3. You'll see a reference called: System. Data.SqlServerCe Type/net version/4.0 - delete this item (select item and then click remove in the bottom of this window).
  4. Click on the "add..." button left of the "remove" button
  5. In the .NET tab, find System.Data.SqlServerCe 3.5 and add this.
  6. Compile your project!

I think this will work.

Danny Beckett
  • 20,529
  • 24
  • 107
  • 134
zviad
  • 586
  • 7
  • 18
  • 4
    Yes, no need to upgrade, unless you need to. If you don't find System.Data.SqlServerCe in .NET Tab, add the reference from C:\Program Files\Microsoft SQL Server Compact Edition\v3.5\Desktop – Brij Feb 20 '14 at 05:08
  • This advice is quite presumptuous: Many people starting with Visual Studio 2015 won't likely have the v3.5 version of SqlServerCe on their machine. If you're going to suggest adding a reference to v3.5, any ideas for people on how to obtain that version if they do not have it on their system? – Jazimov Aug 25 '16 at 13:17
  • 1
    @Jazimov you are right, but I answered this question 3 years ago before your comment and in 2013 it was quite normal approach. – zviad Dec 28 '17 at 15:53