vb.net - Windows Phone 8.1 (appx) Database solution -
i'm trying find best solution have local database in wp8.1 application.
i'm using standard wp8.1 (non-sl) , visual studio 2013.
i've looked sqlite couldn't manage work on application/visual studio.
if can utilize sqlite, need point me out way go. else, please refer me best solution.
thanks in advance!
here's repository class leverages sqlite:
public class contactsrepository : icontactsrepository { sqliteasyncconnection _connection = null; static contactsrepository _repository = null; private contactsrepository() { } public async task initialize() { _connection = new sqliteasyncconnection(constants.database_file_name); await ensuretableexist<contactreference>(_connection); } public static contactsrepository instance { { if (_repository == null) { _repository = new contactsrepository(); } homecoming _repository; } } public async task add(category category, contact contact) { var result = await _connection.table<contactreference>().where(c => c.contactid == contact.id).firstordefaultasync(); if (result != null) { result.categoryname = category.name; await _connection.updateasync(result); } else { await _connection.insertasync(new contactreference() { categoryname = category.name, contactid = contact.id }); } } public async task update(category category, contact contact) { var result = await _connection.table<contactreference>().where(c => c.contactid == contact.id).firstordefaultasync(); debug.assert(result != null); if (result == null) { throw new exception("unable update category. candidate not found"); } if (result != null) { result.categoryname = category.name; await _connection.updateasync(result); } } public async task<observablecollection<contact>> get(string categoryname) { var result = new observablecollection<contact>(); var query = _connection.table<contactreference>().where(c => c.categoryname == categoryname); var queryresult = await query.tolistasync(); foreach(var contact in queryresult) { var phonecontacts = resourcelocator.instance[typeof(observablecollection<contact>)] observablecollection<contact>; var phonecontact = phonecontacts.where(c => c.id == contact.contactid).firstordefault(); debug.assert(phonecontact != null); if (phonecontact != null) { result.add(phonecontact); } } homecoming result; } public async task<observablecollection<contactreference>> get() { var result = new observablecollection<contactreference>(); var query = _connection.table<contactreference>(); var queryresult = await query.tolistasync(); foreach (var contact in queryresult) { result.add(contact); } homecoming result; } private async task ensuretableexist<t>(sqliteasyncconnection connection) t : new() { bool notableexists = false; seek { var query = await connection.table<t>().firstordefaultasync(); } grab (sqliteexception ex) { if (ex.message.contains("no such table")) { notableexists = true; } } if (notableexists) { await connection.createtableasync<t>(); } } } database vb.net windows-phone-8.1
No comments:
Post a Comment