【问题标题】:Windows Phone 8.1 (appx) Database solutionWindows Phone 8.1 (appx) 数据库解决方案
【发布时间】:2014-09-09 10:18:53
【问题描述】:

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

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

我研究了 SQLite,但无法让它在我的应用程序/Visual Studio 上运行。

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

提前致谢!

【问题讨论】:

  • 首先,我尝试了 System.Data.Linq(我之前在 SL 上使用过),但我找不到让它在 WP8 上工作的方法。 1个其次,我什至无法让 SQLite 工作。我试图找到一个包装器,但无法测试任何包装器,因为它说不支持。
  • 因为我无法让任何形式的数据库正常工作,所以我考虑过 XML 或 JSON。但我肯定更喜欢数据库,因为我需要关系使用。
  • 我想知道为什么我被否决而不是帮助...
  • 我不知道为什么有人对你投了反对票,但这可能是因为你还没有分享到目前为止你到底尝试了什么以及什么没有成功(这就是我在评论,以帮助您提出更详细和适当的问题)
  • 我试过的包装器之一是SQLitePCL url

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


【解决方案1】:

这是一个利用 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>();
        }
    }
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-11-01
    • 1970-01-01
    • 1970-01-01
    • 2014-06-10
    • 2016-01-24
    • 1970-01-01
    相关资源
    最近更新 更多