Contact Windows Support

  • Subscribe to our RSS feed.
  • Twitter
  • StumbleUpon
  • Reddit
  • Facebook
  • Digg

Wednesday, 6 February 2013

Easy SQL CE on Windows Phone

Posted on 14:29 by Unknown
In Windows Phone is very simple to add and use SQL CE database.
What you need? Nothing... it's built in the Windows Phone Runtime.

Let's start...

If not exists, add the reference System.Data.Linq in your project.

Create the class to map the entity to the table:
[Table(Name="Utenti")]
public class Utente
{
[Column(IsPrimaryKey = true, IsDbGenerated = true)]
public int Id { get; set; }

[Column(CanBeNull = false)]
public string Name { get; set; }
}
You can see the attributes for the table and columns: table name, primary key, etc..

Now add a class for DataContext with the reference to the table Utenti. The DataContext wraps all operations you can do with database:
public class MyDataContext: DataContext
{
public const string ConnectionString = "isostore:/mydatabase.sdf";

public Table<Utente> Utenti { get; set; }

public MyDataContext(string connectionString)
: base(connectionString)
{
this.Utenti = this.GetTable<Utente>();
}
}
ConnectionString has the special path to locate the database file in the isolated storage.

The classes for SQL CE are ready.
The last thing is create the physical .sdf file when the application start.
private void Application_Launching(object sender, LaunchingEventArgs e)
{
using (var context = new MyDataContext(MyDataContext.ConnectionString))
{
if (!context.DatabaseExists())
context.CreateDatabase();
}
}

And now enjoy with your LinqToSql queries!
using (var context = new MyDataContext(MyDataContext.ConnectionString))
{
var utenti = context.Utenti.OrderByDescending(u => u.Name).ToList();
}

Remember the "using" statement, because you need to dispose your DataContext to prevent high memory usage. The cost to create a new istance is very small.

NOTE: if you want to build an application for Windows 8/RT, maybe SQL CE is not your way. Why? Unfortunately Windows 8/RT don't support it. In this case you can use SQLITE.
Email ThisBlogThis!Share to XShare to FacebookShare to Pinterest
Posted in c#, csharp, developers, microsoft, silverlight, sql ce, sqlite, tips, windows phone | No comments
Newer Post Older Post Home

0 comments:

Post a Comment

Subscribe to: Post Comments (Atom)

Popular Posts

  • Happy New Year!
    Posted by Lawrence Chang, Product Marketing Manager, Google mobile team When I first asked the mobile team to send me pictures of how they r...
  • XMAS TIME - Get a Lumia 925 for free
    Do you want a Lumia 925 for Christmas? Thanks to DVLUP you can get it easy :) Check the new xmas challenge ! You just need to create 3 new ...
  • Our 5 favourite new Windows Phone apps of the week
    Today myMoneyBook is featured in Nokia's blog:  Our 5 favourite new Windows Phone apps of the week . Thank you so much  Nokia !
  • Mobile Web Calendar for iPhone and Android, now with event management
    It's probably no news to all you parents out there, but as a new dad I only recently found out just how busy life gets with kids. So I w...
  • Windows Phone 8 - Map and Clusters
    This code example demonstrates how to dynamically group pushpins in the map control. There is a lot of code for Windows Phone 7, then I merg...
  • Places Directory app for Android
    As an engineer based in Taipei, I frequently travel to Google headquarters in Mountain View. When I'm there, I'm always looking for...
  • The Iterative Web App - Faster Address Auto-complete and Keyboard Shortcuts
    On April 7th, we announced a new version of Gmail for mobile for iPhone and Android-powered devices. Among the improvements was a complete...
  • Our newest Mobile Search feature: Where am I?
    Many of our improvements to search quality come from analyzing actual search queries. We’re constantly trying to surface more relevant sea...
  • [ITA] Dal 920 al Lumia 1020
    Che dire... Il Lumia 1020 subentra di prepotenza con il suo colore giallo. La prima impressione è sicuramente molto positiva, non solo per l...
  • Impressions of CES
    Posted by Lawrence Chang, Product Marketing Manager, Google mobile team As the 2008 Consumer Electronics Show winds to a close, some of you...

Categories

  • 100th post
  • 3D
  • 6210 navigator
  • 6220 classic
  • adsense
  • adsense for mobile
  • alexandra's mobile [ad]itude
  • Amber
  • android
  • android market
  • android widget
  • app
  • appdeals
  • apphub
  • apple
  • apps
  • att
  • autocomplete
  • best buy mobile
  • better know your mobile
  • biking directions
  • BlackBerry
  • BlackBerry Storm
  • blackjack
  • blogger
  • brightpoint
  • bug
  • buxfer
  • Buzz
  • buzz for mobile
  • c#
  • cab4me
  • Calendar
  • Caliburn
  • canada
  • CES
  • check-in
  • chi-2008
  • clearwire
  • cloud print
  • Clusters
  • Coding4fun
  • Contacts
  • convenience key
  • countdown to 2009
  • coupons
  • csharp
  • culture
  • dennis woodside
  • developers
  • Docs
  • Doodle
  • doodles
  • dotorg
  • doubleclick mobile
  • droid
  • DVLUP
  • e-series
  • ebook
  • election
  • enterprise
  • feature phones
  • france
  • free
  • g1
  • geo
  • geolocation api
  • germany
  • Gesture search
  • gmail
  • gmail for android
  • gmail for mobile
  • GOOG-411
  • googe search
  • Google
  • Google Africa
  • google analytics
  • Google Apps
  • Google Apps Blog
  • google apps device policy
  • google apps for mobile
  • google book search
  • google buzz
  • google buzz for mobile
  • Google Custom Search
  • google docs
  • google earth
  • google finance
  • google gears for mobile
  • google goggles
  • Google I/O
  • google instant
  • google latitude
  • google local search
  • Google Location Alerts
  • google location history
  • Google Locaton History
  • google maps
  • google maps for mobile
  • google maps navigation
  • google mobile
  • google mobile ads
  • google mobile help
  • google mobile help forum
  • Google Mobile Search
  • google mobile tips
  • google mobile tricks
  • google moderator
  • Google News
  • google offers
  • google product search
  • Google profile
  • Google public location badge
  • google search
  • google search app
  • google search by voice
  • google search for mobile
  • google shopper
  • google sites
  • google sky map
  • Google SMS
  • google suggest
  • google sync
  • google talk
  • google toolbar
  • google translate
  • google translate for animals
  • google voice
  • google wallet
  • google+
  • googlenew
  • gps
  • hangouts
  • history
  • honeycomb
  • hot
  • hotpot
  • html 5
  • i-mode
  • igoogle
  • image ads
  • image search
  • inside search
  • Interative web app
  • iOS
  • ipad
  • iphone
  • ipod touch
  • italy
  • iterative web app
  • Iterative Webapp
  • J2ME
  • jason spero
  • lambda
  • latitude api
  • layers
  • Listen
  • Local Business Center
  • local inventory
  • local search
  • locale
  • location based search
  • location tag
  • Lumia 1020
  • Lumia 925
  • mac
  • macworld
  • Mail
  • Maps
  • marketplace
  • mary meeker
  • meow me now
  • microsoft
  • mobile
  • mobile [ad]itude
  • mobile advertising
  • mobile calendar
  • mobile tricks
  • mobile world congress
  • mobile.google.com
  • moneybook
  • motorola
  • movies
  • music
  • MVVM
  • my location
  • my tracks
  • myBattery
  • mymoneybook
  • n-series
  • n78
  • n95
  • n96
  • navigation
  • new york city
  • nexus
  • nfc
  • nokia
  • Nokia Pro Camera
  • NowPlaying
  • ntt docomo
  • NuGet; Visual Studio
  • nyc
  • open handset alliance
  • opera
  • opera mini
  • opera mobile
  • orkut
  • outbox
  • palm
  • palm webos
  • Panoramio
  • personalized suggest
  • Picasa web albums
  • Place Pages for mobile
  • Places
  • Places Directory
  • pre
  • product ideas
  • product search
  • produt search for mobile
  • quick search box
  • Reader
  • registration
  • research
  • s60
  • samsung
  • santa
  • search
  • search by voice
  • Search Options
  • sharing
  • shortcut
  • sidekey
  • silverlight
  • sky lab
  • smart navigation
  • social
  • Sony
  • sony ericsson
  • spain
  • Spreadsheeets
  • sprint
  • sql ce
  • sqlite
  • starring
  • stars
  • street view
  • Summer Games
  • symbian
  • Sync
  • Syncfusion
  • t-mobile
  • tablet
  • tasks
  • TechNet
  • TechNet wiki
  • telerik
  • Thomson
  • TileView
  • tips
  • transit
  • uiq
  • uk
  • universal search
  • verizon
  • visual search
  • voice actions
  • voice search
  • vote
  • walking directions
  • walking navigation
  • web app
  • wep app
  • wikininjas
  • windows 8
  • windows mobile
  • windows phone
  • Windows Phone 7.8
  • Windows phone 8
  • windows RT
  • Windows Store
  • wireless week
  • xaml
  • xna
  • youtube
  • youtube channel
  • YouTube for mobile
  • zoho

Blog Archive

  • ▼  2013 (21)
    • ►  November (3)
    • ►  October (1)
    • ►  September (3)
    • ►  August (3)
    • ►  July (2)
    • ►  June (4)
    • ►  April (1)
    • ►  March (1)
    • ▼  February (2)
      • PeriodicTask bug in Windows Phone 8
      • Easy SQL CE on Windows Phone
    • ►  January (1)
  • ►  2012 (32)
    • ►  December (2)
    • ►  November (3)
    • ►  October (3)
    • ►  September (6)
    • ►  August (1)
    • ►  June (1)
    • ►  May (2)
    • ►  April (3)
    • ►  March (5)
    • ►  February (5)
    • ►  January (1)
  • ►  2011 (98)
    • ►  December (8)
    • ►  November (9)
    • ►  October (6)
    • ►  September (7)
    • ►  August (2)
    • ►  July (12)
    • ►  June (7)
    • ►  May (11)
    • ►  April (8)
    • ►  March (12)
    • ►  February (9)
    • ►  January (7)
  • ►  2010 (122)
    • ►  December (18)
    • ►  November (10)
    • ►  October (8)
    • ►  September (10)
    • ►  August (10)
    • ►  July (4)
    • ►  June (11)
    • ►  May (7)
    • ►  April (14)
    • ►  March (13)
    • ►  February (10)
    • ►  January (7)
  • ►  2009 (109)
    • ►  December (7)
    • ►  November (14)
    • ►  October (14)
    • ►  September (6)
    • ►  August (7)
    • ►  July (9)
    • ►  June (13)
    • ►  May (10)
    • ►  April (7)
    • ►  March (7)
    • ►  February (11)
    • ►  January (4)
  • ►  2008 (92)
    • ►  December (11)
    • ►  November (7)
    • ►  October (9)
    • ►  September (6)
    • ►  August (6)
    • ►  July (11)
    • ►  June (12)
    • ►  May (4)
    • ►  April (8)
    • ►  March (5)
    • ►  February (5)
    • ►  January (8)
  • ►  2007 (9)
    • ►  December (6)
    • ►  November (3)
Powered by Blogger.

About Me

Unknown
View my complete profile