Using SQL Local Database in Mobile Applications

Post on 09-Jun-2015

3.800 views 3 download

Tags:

Transcript of 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.

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 |

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 |

Windows Phone Platform

Goals Simple End User

Personalization Enable Cloud

Experiences Help Developers be

Profitable

4 |

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 |

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 |

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 |

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 |

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 |

Local Database Maintenance

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

10 |

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 |

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 |

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 |

Creating a Local Database

CountryDataContext is implemented in the following way:

14 |

Database Queries with LINQ

15 |

Inserting Data

16 |

Inserting Data

17 |

Updating Data

18 |

Deleting Data

19 |

Local Database Tools

SQL Compact Code Generator CompactView Isolated Storage Explorer

20 |

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/

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/

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 |

Isolated Storage Explorer

24 |

http://istool.codeplex.com/

DEMO

QUESTIONS

Q & A

26 |

Sponsors