SQLite在VS 2019中的实现

问题描述 投票:0回答:1

我对编码还是很陌生,想做我的第一个WPF应用。我一直在尝试实现sqlite在过去像3天了。

我正在使用VS 2019。我已经下载了SQLite工具箱,并按照这里的说明进行操作--。 https:/github.comErikEJSqlCeToolboxwikiEF6-workflow-with-SQLite-DDEX-provider。. 我做了一个完整的安装。我是不是应该把它安装在我的项目目录下?因为现在我只有一堆文件,而Studio中似乎没有任何变化。我试着用Install.exe,但它返回了一个 "确认选项未启用 "的错误。看着类似的问题,我试着把文件放在项目的外部文件夹中,然后用VS开发控制台把System.Data.SQLite.EF6.dll安装到我的GAC中。工具箱看不到任何变化,也不识别这个dll,我很难找到我的版本的可靠信息。谢谢你的帮助,让我找到了正确的方向!

c# wpf sqlite visual-studio-2019 gac
1个回答
0
投票

我不知道你为什么要提到工具箱,据我所知,你通过编程访问SQLite功能。

我最近在一个Xamarin项目中使用的NuGet包(Frank Krueger的SQLite-net-pcl和支持的库)允许我使用相当简单的对象映射,而不求助于SQL像字符串。

我在代码中使用了很多接口,但这是我的全访问数据库类。

:

   public class AllAccessDataTableBaseSqLite<T> : IDataAccessRead<T>, IDataAccessWrite<T>,IDataAccessExpressionSearch<T>, IDataAccessDelete<T> where T: IDataRecord, new()
    {
        //Note that this static value will only apply to those classes based on the same generic type e.g. all DataTableBase<User> instances etc.
        public static SQLiteAsyncConnection DBConnection;

        /// <summary>
        /// Lock object to prevent multi-thread interruption of code segment.
        /// </summary>
        public static readonly object CollisionLock = new object();

        /// <summary>
        /// Constructor
        /// </summary>
        public AllAccessDataTableBaseSqLite()
        {
            lock (CollisionLock)
            {
                if (DBConnection != null)
                {
                    DBConnection.CreateTableAsync<T>().Wait();

                    return;
                }

                try
                {
                    string directory;

                    if (DeviceInfo.Platform != DevicePlatform.Unknown)
                    {
                        directory = FileSystem.AppDataDirectory;
                    }
                    else
                    {
                        directory = "DataStore";
                        var directoryInfo = Directory.CreateDirectory(directory);
                        directory = directoryInfo.FullName;
                    }

                    var path = Path.Combine(directory, $"{typeof(T).Name}.db");
                    if (!File.Exists(path))
                    {
                        using var fileStream = File.Create(path);

                        fileStream.Close();
                    }

                    DBConnection = new SQLiteAsyncConnection(path);
                    DBConnection.CreateTableAsync<T>().Wait();
                }
                catch (Exception ex)
                {
                    if (ex is UnauthorizedAccessException)
                    {

                    }
                }
            }
        }

        /// <summary>
        /// Create the data table
        /// </summary>
        /// <returns></returns>
        public async Task<CreateTableResult> CreateTableAsync()
        {
            if (DBConnection != null)
            {
                return await DBConnection.CreateTableAsync<T>();
            }

            return CreateTableResult.Migrated;
        }

        /// <summary>
        /// Create a new record entry
        /// </summary>
        /// <param name="entity">Data entity to enter</param>
        /// <param name="user">Current User information</param>
        /// <returns>New entry record if successful</returns>
        public async Task<T> CreateAsync(T entity, IUserRecord user)
        {
            if (entity == null)
            {
                return default(T);
            }

            if (DBConnection == null)
            {
                return default(T);
            }

            entity.CreatedDate = DateTime.UtcNow;
            entity.CreatedByUserId = user.Id;
            entity.Id = 0;
            try
            {
                await DBConnection.InsertAsync(entity);

            }
            catch (SQLiteException e)
            {
                if (e.Message == "Constraint")
                {
                    throw new InvalidConstraintException(e.Message, e.InnerException);
                }
            }            
            var result = entity;

            return result;
        }

        /// <summary>
        /// Update a collection of new entities of type T to the data table.
        /// All entities should be present within the data table
        /// </summary>
        /// <param name="entityList">Entity collection</param>
        /// <param name="user">user making the change</param>
        /// <returns>ID of entities successfully updated or added</returns>
        public async Task<int> UpdateAllAsync(IEnumerable<T> entityList, IUserRecord user)
        {
            var result = 0;
            foreach (var t in entityList)
            {
                if (null != await UpdateAsync(t, user))
                {
                    result++ ;
                }
            }

            return result;
        }

        /// <summary>
        /// Obtain the data record with the given Id
        /// </summary>
        /// <param name="id">Id value to select the record by</param>
        /// <returns>A valid record if found otherwise null</returns>
        public async Task<T> GetById(int id)
        {
            if (DBConnection == null)
            {
                return default(T);
            }

            return await DBConnection.Table<T>().Where(i => i.Id == id).FirstOrDefaultAsync();
        }

        /// <summary>
        /// This function returns all database entries that are not marked deleted or changed
        /// Warning: The data set may be very large
        /// </summary>
        /// <returns>A list of entries</returns>
        public async Task<List<T>> GetAll()
        {
            if (DBConnection != null)
            {
                return await DBConnection.Table<T>().Where(x=>x.ChangedDate==default && x.DeletedDate==default)
                    .ToListAsync();

            }
            return new List<T>();
        }

        /// <inheritdoc />
        public async Task<List<T>> GetAllHistoric() => await DBConnection.Table<T>().ToListAsync();


        /// <summary>
        /// This function is used to update the supplied record entry within the database.
        /// If the supplied record does not have a non-zero value Id field it is assumed to be a
        /// new record to be inserted into the database.
        /// </summary>
        /// <param name="entity">Record to update</param>
        /// <param name="user">User performing the action</param>
        /// <returns></returns>
        public async Task<T> UpdateAsync(T entity, IUserRecord user)
        {
            if (DBConnection == null)
            {
                return default(T);
            }

            if (entity == null)
            {
                return default(T);
            }

            var newRecord = (T) ((entity) as BaseRecord<T>)?.Clone();

            if (null == newRecord)
            {
                return default(T);
            }

            //if Id is zero assume that the record is new and to be added
            if (newRecord.Id == 0)
            {
                if (user != null)
                {
                    newRecord.CreatedByUserId = user.Id;
                }
                newRecord.CreatedDate = DateTime.UtcNow;
                newRecord.Id = await DBConnection.InsertAsync(newRecord);
                return newRecord;
            }

            // Id is not zero and thus a new record should be created linked to the old record.
            var oldRecord = await GetById(newRecord.Id);
            oldRecord.ChangedDate = DateTime.UtcNow;
            if (user != null)
            {
                oldRecord.ChangedByUserId = user.Id;
            }
            try
            {
                var result = await DBConnection.UpdateAsync(oldRecord);

            }
            catch (Exception e)
            {

               Debug.WriteLine($"UpdateAsync {e.Message}");
            }           

            newRecord.PreviousRecordId = oldRecord.Id;
            newRecord.Id = 0;

            return await CreateAsync(newRecord, user);

        }

        /// <inheritdoc />
        public async Task<int> DeleteAsync(T entity)
        {
            if (DBConnection == null)
            {
                return -1;
            }
            return await DBConnection.DeleteAsync(entity);
        }

        /// <inheritdoc />
        public async Task DeleteAll()
        {
            await DBConnection.DropTableAsync<T>();
            await CreateTableAsync();
        }

        /// <inheritdoc />
        public async Task<PagedResult<T>> GetAllPagedResult(int recordId, uint maxResults = 100)
        {
            if (DBConnection == null)
            {
                return null;
            }

            List<T> list;

            if (maxResults == 0)
            {
                list = await GetAll();
            }
            else
            {
                list = await DBConnection.Table<T>().Where(x => (x.Id >= recordId && x.ChangedDate == default && x.DeletedDate == default)).ToListAsync();
                if (list.Count() > maxResults)
                {
                    list = list.GetRange(0, (int) maxResults);
                }
            }

            return new PagedResult<T>(list, list.Count());

        }

        /// <inheritdoc />
        public async Task<IEnumerable<T>> FindAsyncOrdered<TValue>(Expression<Func<T, bool>> predicate = null,
            Expression<Func<T, TValue>> orderBy = null)
        {
            var query = DBConnection.Table<T>();
            if (predicate != null)
            {
                query = query.Where(predicate);
            }

            if (orderBy != null)
            {
                query = query.OrderBy<TValue>(orderBy);
            }

            return await query.ToListAsync();
        }

        /// <inheritdoc />
        public async Task<T> FindFirst(Expression<Func<T, bool>> predicate) => await DBConnection.FindAsync(predicate);
    }

我使用的是基于数据类。

public interface IDataRecord 
{
    /// <summary>
    /// Identifier for record
    /// </summary>
    int Id { get; set; }

    /// <summary>
    /// Link to previous version of record
    /// </summary>
    int PreviousRecordId { get; set; }

    /// <summary>
    /// User Identity that made the change
    /// </summary>
    int ChangedByUserId { get; set; }

    /// <summary>
    /// Date when the data record was last changed
    /// </summary>
    DateTime ChangedDate { get; set; }

    /// <summary>
    /// Identity of User that deleted the record
    /// </summary>
    int DeletedByUserId { get; set; }

    /// <summary>
    /// Date when the data record was deleted
    /// </summary>
    DateTime DeletedDate { get; set; }

    /// <summary>
    /// Identity of User that created the record
    /// </summary>
    int CreatedByUserId { get; set; }

    /// <summary>
    /// Date when the data record was added
    /// </summary>
    DateTime CreatedDate { get; set; }

    object Clone();
}

很明显,你不一定要用这个,但是对于我的应用程序的实现来说 每种类型的数据记录都存储在它自己的数据文件中(因此每个文件有一个表) 这是在一开始就在构造函数中创建的。

  • SQLite db连接是使用数据文件路径创建的。

  • 表是使用dbconnection创建的。

编辑

我仔细看了一下你的代码,需要注意的地方是。

  • 你似乎并没有创建一个表格.如果你选择创建一个UWP而不是基本的WPF项目,那么对非应用程序文件夹的访问是受限制的。

  • 如果你选择创建一个UWP而不是基础WPF项目,对非应用程序文件夹的访问是受限制的--在运行应用程序时要注意文件夹访问权限,尤其是在发布模式下。

© www.soinside.com 2019 - 2024. All rights reserved.