如何在xamarin表单中使用SQLite-Net-PCL?

时间:2017-05-23 06:11:29

标签: c# sqlite xamarin xamarin.forms

我在我的项目中安装了SQLite-net-pcl包。现在我想将它用于简单的CRUD操作。问题是我读到的所有文档都让我感到困惑。任何人都可以提示我在Xamarin.Forms中执行CRUD操作的正确步骤,以接受值表单Entry并将其存储在数据库中吗?

1 个答案:

答案 0 :(得分:1)

我在我的工作应用中使用此方法。

  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
      }
    
    1. 在App类中为DataRepository创建静态字段。用这个 代码中的App.Repo。

         App.Repo.Save(new Data
                  {
                     ...
                  }) ;
      
    2. 这是一个简化的使用示例。