Windows Phone 8.1(appx)数据库解决方案

时间:2014-06-18 13:45:47

标签: database vb.net windows-phone-8.1

我试图找到在我的WP8.1应用程序中拥有本地数据库的最佳解决方案。

我使用标准WP8.1(非SL)和Visual Studio 2013。

我已经看过SQLite,但我无法让它在我的应用程序/ Visual Studio上运行。

如果我可以使用SQLite,我需要有人指出我要走的路。另外,请向我推荐最佳解决方案。

提前致谢!

1 个答案:

答案 0 :(得分:0)

这是一个利用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
    {
        get
        {
            if (_repository == null)
            {
                _repository = new ContactsRepository();
            }

            return _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 to 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>)] as ObservableCollection<Contact>;

            var phoneContact = phoneContacts.Where(c => c.Id == contact.ContactId).FirstOrDefault();
            Debug.Assert(phoneContact != null);

            if (phoneContact != null)
            {
                result.Add(phoneContact);
            }
        }

        return 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);
        }

        return result;
    }

    private async Task EnsureTableExist<T>(SQLiteAsyncConnection connection) where T : new()
    {
        bool noTableExists = false;

        try
        {
            var query = await connection.Table<T>().FirstOrDefaultAsync();
        }
        catch (SQLiteException ex)
        {
            if (ex.Message.Contains("no such table"))
            {
                noTableExists = true;
            }
        }

        if (noTableExists)
        {
            await connection.CreateTableAsync<T>();
        }
    }
}