Using SQL Local Database in Mobile Applications

27
Using SQL Local Database in Mobile Applications Microsoft SQL Server Compact (SQL CE) is a compact relational database produced by Microsoft for applications that run on mobile devices and desktops. With Windows Phone OS you can store relational data in a local database.

Transcript of Using SQL Local Database in Mobile Applications

Page 1: Using SQL Local Database in Mobile Applications

Using SQL Local Database in Mobile ApplicationsMicrosoft SQL Server Compact (SQL CE) is a compact relational database produced by Microsoft for applications that run on mobile devices and desktops.

With Windows Phone OS you can store relational data in a local database.

Page 2: Using SQL Local Database in Mobile Applications

About me

Mihail Mateev is a Senior Technical Evangelist, Team Lead at Infragistics Inc.

Mihail worked in various areas related to technology Microsoft: Silverlight, WPF, Windows Phone 7, Visual Studio LightSwitch, WCF RIA Services, ASP.Net MVC, Windows Metro Applications, MS SQL Server and Windows Azure

2 |

Page 3: Using SQL Local Database in Mobile Applications

Agenda

Windows Phone Windows Phone Mango Local Database(SQL CE) Local Database Maintenance

Creating the Database Database Queries with LINQ Inserting Data Updating Data Deleting Data

Local Database Tools SQL Compact Code Generator CompactView Isolated Storage Explorer

3 |

Page 4: Using SQL Local Database in Mobile Applications

Windows Phone Platform

Goals Simple End User

Personalization Enable Cloud

Experiences Help Developers be

Profitable

4 |

Page 5: Using SQL Local Database in Mobile Applications

Windows Phone Application Platforms

Silverlight Vector XAML Graphics Rich Media Rich Video Visual Studio

Tooling

XNA Fast 3D Graphics

Engine 2D Sprites

Good Tooling

5 |

Page 6: Using SQL Local Database in Mobile Applications

Local Database for Windows Phone

With Windows Phone OS 7.1, you can store relational data in a local database that resides in your application’s isolated storage container.

6 |

Page 7: Using SQL Local Database in Mobile Applications

Local Database for Windows Phone

Good to know Isolated storage is used to store database filesLINQ to SQL is used as the ORM engineLINQ is used to query dataOnly System.Data.Linq assembly must be added

to the project

7 |

Page 8: Using SQL Local Database in Mobile Applications

Local Database for Windows Phone

LimitationsADO.NET objects are not supported Skip() and Take() require an ordered list and

constant typesOnly Microsoft SQL CE data types are supportedBinaryFormatter is not supported by defaultExcecuteCommand is not supported – NO T-

SQL, NO DML statement, NO DDL

8 |

Page 9: Using SQL Local Database in Mobile Applications

Local Database vs. SQL Server

A local database runs in the Windows Phone application’s process. Unlike a client-server database such as Microsoft SQL Server, it does not run continuously as a background service.

A local database can be accessed only by the corresponding Windows Phone application. Because the database file resides in isolated storage, no other applications can access that data.

A local database can be accessed only with LINQ to SQL; Transact-SQL is not supported.

9 |

Page 10: Using SQL Local Database in Mobile Applications

Local Database Maintenance

Creating the Database Database Queries with LINQ Inserting Data Updating Data Deleting Data

10 |

Page 11: Using SQL Local Database in Mobile Applications

Local Database(SQL CE): Connection Strings

A special format of the connection string must be used like for example:

"Data Source='isostore:/DIRECTORY/FILE.sdf'"

private const string ConnectionString = @"isostore:/CountryDB.sdf";

public MainPage()

{

InitializeComponent();

using (CountryDataContext context = new CountryDataContext(ConnectionString))

{

if (!context.DatabaseExists())

{

// create database if it does not exist

context.CreateDatabase();

}

}

}

11 |

Page 12: Using SQL Local Database in Mobile Applications

Data Context It inherits from the "System.Data.Linq.DataContext"

class The data context constructor must call the

base(connectionString) constructor The data context exposes the tables from the database

through properties of type Table<TEntity>. The Table class implements IQueriable<TEntity> and enables you to write LINQ queries against the database.

12 |

Page 13: Using SQL Local Database in Mobile Applications

Creating a Local Database

After you create the DataContext object, you can create the local database and perform a number of additional database operations.

using (CountryDataContext context = new CountryDataContext(ConnectionString))

{

if (!context.DatabaseExists())

{

// create database if it does not exist

context.CreateDatabase();

}

}

13 |

Page 14: Using SQL Local Database in Mobile Applications

Creating a Local Database

CountryDataContext is implemented in the following way:

14 |

Page 15: Using SQL Local Database in Mobile Applications

Database Queries with LINQ

15 |

Page 16: Using SQL Local Database in Mobile Applications

Inserting Data

16 |

Page 17: Using SQL Local Database in Mobile Applications

Inserting Data

17 |

Page 18: Using SQL Local Database in Mobile Applications

Updating Data

18 |

Page 19: Using SQL Local Database in Mobile Applications

Deleting Data

19 |

Page 20: Using SQL Local Database in Mobile Applications

Local Database Tools

SQL Compact Code Generator CompactView Isolated Storage Explorer

20 |

Page 21: Using SQL Local Database in Mobile Applications

SQL Server Compact Toolbox

SQL Server Compact Toolbox is a Visual Studio 2010 Pro or higher add-in (for 3.5/4.0) and standalone app (for 4.0), that adds scripting, import, export, migrate, rename, run script, manage replication and more to your SQL Server Compact Data Connections in VS Server Explorer.

21 |

http://sqlcetoolbox.codeplex.com/

Page 22: Using SQL Local Database in Mobile Applications

CompactView

CompactView is a viewer for Microsoft® SQL Server® Compact Edition (SQLCE) database files (*.sdf).

CompactView can open database files of versions 3.1, 3.5 and 4.0.

22 |

http://sourceforge.net/p/compactview/home/Home/

Page 23: Using SQL Local Database in Mobile Applications

Isolated Storage Explorer

Add-In for Visual Studio 2010 to import, export or modify data from the isolated storage of the phone or the emulator Windows Phone

Import data from Isolated Storage

Export data to Isolated Storage

automatic retrieval of the target (emulator / phone) and the identifier of the current project

23 |

Page 24: Using SQL Local Database in Mobile Applications

Isolated Storage Explorer

24 |

http://istool.codeplex.com/

Page 25: Using SQL Local Database in Mobile Applications

DEMO

Page 26: Using SQL Local Database in Mobile Applications

QUESTIONS

Q & A

26 |

Page 27: Using SQL Local Database in Mobile Applications

Sponsors