Wednesday, April 14, 2010

Windows Phone 7 and Local Database Support

In this post I want to talk about the experiment and the effort I've done investigating the support of local database on a Window Phone 7.
  • First of all, I thought about using SQL Server Compact, since this was supported in Windows Phone 6.5. But, I didn't find any way to accomplish that.
  • Researching the web, I've faced the first frustration: Windows Phone 7 Doesn't support Local databases. Microsoft intends to adopt cloud services approach and push everything towards Windows Azure and SQL Azure !
  • Still, I didn't give up and looked and researched if there are any possible approaches.
  • One of the things I've found is the use of csharp-sqlite solution developed by noah.hart.
  • I've tried using that, but... The Emulator was not persisting data.I've lost the data after I've started the emulator. The data was persistent only when the emulator was running (we know that we can keep the emulator running, change the code and deploy again and our changes will be reflected to the emulator without restarting it).
  • I'm not sure, if this is reflected again to a real Windows Phone 7 device, since I didn't try that in reality.
  • I did another try. I've tried to use SQLite. You can find it here:
  • After the installation, try the following:
    Run Visual Studio 2010 Professional
    View the Server Explorer.Create a new database and name the file.
    1. Note: If you run Visual Studio 2010 Express you won't find the Server Explorer there! (Another proof of : "We do not support local db" )
    Create a new project (windows phone application using Silverlight)
    click on the references > Add Reference
    Notice that in the .Net tab you don't have any reference to System.Data (again ?)
    Browse tab > Navigate to SQLite.Net and add System.Data.SQLite.dll from the bin folder
    Check that the new reference is added.
    Now let's try to write some code in MainPage.xaml.cs.
    import both System.Data and System.Data.SQLite.
    now try using SQLiteConnection object.. WOW... It's there... I'm not getting errors.
    Now Build... and you will get this:
    1. Error 1 The type 'System.Data.Common.DbConnection' is defined in an assembly that is not referenced. You must add a reference to assembly 'System.Data, Version=, Culture=neutral, PublicKeyToken=b77a5c561934e089'.
    2. some resources assumed that adding the reference in the CompactFramework folder will fix this. Tried that, but it's still the same.
    3. I've also added the references from GAC (Global Assembly Cache).. Still the same ! :(
    4. Tried the same SQLite reference with a Windows Form Application. Worked !
    Conclusion: I think for now, SQLite is not supported for Windows Phone Applications development.
  • Next thing came to my mind is Serialization, POCOS, IsolatedStorage. Those were mentioned as an alternative approach for local data persistence on WP7. I'll talk about them in the next post.


  1. Thanks for the information. You saved lot of my time. I am also looking for smiler solution. Please post if you find any break threw.

  2. You can obtain news, weather, or live, up to date traffic reports.oukitel