If you are looking for a step-by-step procedure, you can see this post .
You can use it with:
- Windows 10: SQLite for Universal Application Platform
- Windows Phone 8.1: SQLite for Windows Phone 8.1
- Windows 8.1: SQLite for Windows Runtime (Windows 8.1)
After configuring packages and extensions, you can create a database and model classes using:
using System.IO; using System.Threading.Tasks; using SQLiteModernApp.DataModel; using SQLite.Net.Async; using System; using SQLite.Net; using SQLite.Net.Platform.WinRT; namespace SQLiteModernApp.DataAccess { public class DbConnection : IDbConnection { string dbPath; SQLiteAsyncConnection conn; public DbConnection() { dbPath = Path.Combine(Windows.Storage.ApplicationData.Current.LocalFolder.Path, "Sample.sqlite"); var connectionFactory = new Func<SQLiteConnectionWithLock>(() => new SQLiteConnectionWithLock(new SQLitePlatformWinRT(), new SQLiteConnectionString(dbPath, storeDateTimeAsTicks: false))); conn = new SQLiteAsyncConnection(connectionFactory); } public async Task InitializeDatabase() { await conn.CreateTableAsync<Department>(); await conn.CreateTableAsync<Employee>(); await conn.CreateTableAsync<EmployeeDepartment>(); } public SQLiteAsyncConnection GetAsyncConnection() { return conn; } } }
Now you can create a CRUD, following the example:
using System.Collections.Generic; using System.Threading.Tasks; using SQLite.Net.Async; using SQLiteModernApp.DataAccess; using SQLiteModernApp.DataModel; using SQLiteNetExtensionsAsync.Extensions; namespace SQLiteModernApp.Repository { public class EmployeeRepository : IEmployeeRepository { SQLiteAsyncConnection conn; public EmployeeRepository(IDbConnection oIDbConnection) { conn = oIDbConnection.GetAsyncConnection(); } public async Task InsertEmployeeAsync(Employee employee) { await conn.InsertOrReplaceWithChildrenAsync(employee); } public async Task UpdateEmployeeAsync(Employee employee) { await conn.UpdateWithChildrenAsync(employee); } public async Task DeleteEmployeeAsync(Employee employee) { await conn.DeleteAsync(employee); } public async Task<List<Employee>> SelectAllEmployeesAsync() { return await conn.GetAllWithChildrenAsync<Employee>(); } public async Task<List<Employee>> SelectEmployeesAsync(string query) { return await conn.QueryAsync<Employee>(query); } } }
source share