2017-05-23 28 views
0

我在我的項目中安裝了SQLite-net-pcl軟件包。現在我想用它來進行簡單的CRUD操作。問題是我讀的所有文檔都讓我感到困惑。任何人都可以提示我在Xamarin.Forms中執行CRUD操作的正確步驟,以接受值表單並將其存儲在數據庫中?如何在xamarin表單中使用SQLite-Net-PCL?

+0

你有沒有試過這個:https://developer.xamarin.com/guides/xamarin-forms/application-fundamentals/databases/?因爲這個文檔很好啓動。我建議你嘗試上面的指南,並更新你遇到的任何問題的原始帖子。 – apineda

+0

網上有很多教程。在這裏你有另一個:https://code.tutsplus.com/tutorials/an-introduction-to-xamarinforms-and-sqlite--cms-23020嘗試這些,然後回到SO,當你有一個特定的問題 – Joehl

回答

0

我在我的工作應用程序中使用此方法。

  1. 安裝SQLite-net-pcl

  2. 我使用異步方法。爲了排除鎖,我使用這個類:

    public sealed class AsyncLock 
    { 
        private readonly SemaphoreSlim _semaphore = new SemaphoreSlim(1, 1); 
        private readonly Task<IDisposable> _releaser; 
    
    public AsyncLock() 
    { 
        _releaser = Task.FromResult((IDisposable)new Releaser(this)); 
    } 
    
    public Task<IDisposable> LockAsync() 
    { 
        var wait = _semaphore.WaitAsync(); 
        return wait.IsCompleted ? 
         _releaser : 
         wait.ContinueWith((_, state) => (IDisposable)state, 
         _releaser.Result, CancellationToken.None, 
         TaskContinuationOptions.ExecuteSynchronously, TaskScheduler.Default); 
    } 
    
    private sealed class Releaser : IDisposable 
    { 
        private readonly AsyncLock m_toRelease; 
    
        internal Releaser(AsyncLock toRelease) 
        { 
         m_toRelease = toRelease; 
        } 
    
        public void Dispose() 
        { 
         m_toRelease._semaphore.Release(); 
        } 
    } 
    } 
    
  3. 創建域(表):

    //base class for table 
    public class Entity 
    { 
        [PrimaryKey, AutoIncrement] 
        public int Id { get; set; } 
    } 
    
    //your table 
    public class Data :Entity 
    { 
        public string Prop1 {get;set;} 
        ...... 
    } 
    
    public class Data2 :Entity 
    { 
        public string Prop2 {get;set;} 
        ...... 
    } 
    
  4. 創建倉庫:

    public class DataRepository 
    { 
        private SQLiteAsyncConnection _db; 
        private static readonly AsyncLock Mutex = new AsyncLock(); 
    
        public async Task CreateDatabaseAsync(string path) 
        { 
        using (await Mutex.LockAsync().ConfigureAwait(false)) 
        { 
         _db= new SQLiteAsyncConnection(path); 
         await _db.CreateTableAsync<Data>(); 
         //create other tables 
        } 
    
        public async Task Save<T>(T entity) where T : Entity, new() 
        { 
         using (await Mutex.LockAsync().ConfigureAwait(false)) 
         { 
         await _db.InsertAsync(entity); 
         } 
        } 
    
        public async Task Delete(Entity item) 
        { 
         using (await Mutex.LockAsync().ConfigureAwait(false)) 
         { 
         await _db.DeleteAsync(item); 
         } 
        } 
    
        public async Task Update<T>(T entity) where T : Entity, new() 
        { 
         using (await Mutex.LockAsync().ConfigureAwait(false)) 
         { 
          await _db.UpdateAsync(entity); 
         } 
        }   
        ........ 
        //other base method 
        } 
    
  5. 在App類中爲DataRepository創建靜態字段。在你的代碼中使用這個 App.Repo。

    App.Repo.Save(new Data 
          { 
           ... 
          }) ; 
    

這是利用的簡化示例。

相關問題