1
我试图找到在我的WP8.1应用程序中拥有本地数据库的最佳解决方案。Windows Phone 8.1(appx)数据库解决方案
我使用的是标准的WP8.1(非SL)和Visual Studio 2013年
我看着SQLite的,但我不能设法得到它在我的应用程序工作/ Visual Studio中。
如果我可以使用SQLite,我需要有人指出我要走的路。否则,请转介我最佳解决方案。
在此先感谢!
我试图找到在我的WP8.1应用程序中拥有本地数据库的最佳解决方案。Windows Phone 8.1(appx)数据库解决方案
我使用的是标准的WP8.1(非SL)和Visual Studio 2013年
我看着SQLite的,但我不能设法得到它在我的应用程序工作/ Visual Studio中。
如果我可以使用SQLite,我需要有人指出我要走的路。否则,请转介我最佳解决方案。
在此先感谢!
这里有一个仓储类,它利用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>();
}
}
}
首先,我已经试过将System.Data.Linq(这是我在以前SL使用),我不能找到一种方法,使它适用于WP8.1 appx。其次,我无法让SQLite工作。我试图找到一个包装,但无法测试,因为它说不支持。 – silentw
因为我无法获得任何形式的数据库工作,所以我想到了XML或JSON。但我肯定会更喜欢数据库,因为我需要一个关系用法。 – silentw
我想知道为什么我得到了downvoted而不是帮助... – silentw