Quick Start for WinRT Database

 

I have a CodePlex project for file based database for WinRT / Windows 8. You can check out this project at http://winrtdatabase.codeplex.com/

First step is to download the project and build it on your computer. Visit the download page for the latest recommended download. Unzip the source code on your machine, open it up in Visual Studio 11 (Developer preview) and compile to create a DLL.

Next, create new Windows Metro style application project using C# and add a reference to the DLL from step 1.  You will end up with a XAML / C# based project.

Next, in your View Model (or elsewhere in your application) check for database existence and create if it does not exist along with tables. See sample code below

public async void Initialise()
        {

            var exists = await Database.DoesDatabaseExistsAsync(DatabaseName);
            if (!exists)
            {
                _database = await Database.CreateDatabaseAsync(DatabaseName);
                _database.CreateTable<Person>();
                var table = await _database.Table<Person>();
                table.AddRange(new Person[]
                {
                    new Person()
                    {
                        PersonID = Guid.NewGuid(),
                        FirstName = "Sergey",
                        LastName = "Barskiy",
                        Age=19
                    },
                    new Person()
                    {
                        PersonID = Guid.NewGuid(),
                        FirstName = "Michelle",
                        LastName = "Barskiy",
                        Age=18
                    }
                });
                await _database.SaveAsync();
                People = table;
            }
            else
            {
                _database = await Database.OpenDatabaseAsync(DatabaseName, true);
                var table = await _database.Table<Person>();
                People = table;
            }
        }

In the sample above I am testing if database exists, then create it if it does not, Then I am adding a table to it based on POCO class Person. I am adding two people to the tables.  You can add a single instance by calling Add instead of AddRange.  For example, Person class would look like

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Windows.UI.Xaml.Data;

namespace WinRTDbQuickStart
{
    public class Person : BaseObject
    {
        private Guid personID;

        public Guid PersonID
        {
            get { return personID; }
            set { personID = value; OnPropertyChanged("PersonID"); }
        }

        private string firstName;

        public string FirstName
        {
            get { return firstName; }
            set { firstName = value; OnPropertyChanged("FirstName"); }
        }

        private string lastName;

        public string LastName
        {
            get { return lastName; }
            set { lastName = value; OnPropertyChanged("LastName"); }
        }

        private int age;

        public int Age
        {
            get { return age; }
            set { age = value; OnPropertyChanged("Age"); }
        }
       
       
    }
}

To remove, just issue remove command:

public void OnDelete(Person parameter)
      {
          if (parameter != null)
          {
              People.Remove(parameter);
              People.SaveAsync();
          }
      }

You can also remove and add a range of items based on condition.

People.RemoveRange((person) => { return (person.Salary >= 2); });

Make sure to call SaveAsync() on either database or a specific table to commit your changes.

You can also look in unit test project and quick start project that is distributed as part of the source code download for other API samples. There are a number of issue with the sample app due to differences between WinRT and .NET.  For example,ListBox is not listening to Observable Collection changes, instead there is IObservableVector<T> interface.  I am planning to implement those changes next.

Update 11/25/2011 – Issue with binding to ListBox has been fixed.  All tables are now bindable to list based controls.

3 Comments

  1. Pingback: Road to Windows 8 (v2) - Developer Preview - Pagina 69

  2. Pingback: Win8 (Metro style) app development – getting started « RaSor's Tech Blog

Leave a Reply

Your email address will not be published. Required fields are marked *