Ejemplo n.º 1
0
 /// <summary>
 /// 获取SQL执行操作对象
 /// </summary>
 /// <param name="db"></param>
 /// <returns></returns>
 public static ISqlProvider GetSqlProvider(this DbExecutor db)
 {
     return(db.DBType switch
     {
         DatabaseType.SqlServer => new SqlServerProvider(db),
         DatabaseType.MySql => new MySqlProvider(db),
         DatabaseType.SQLite => new SqliteProvider(db),
         _ => throw new NotSupportedException(db.DBType.ToString())
     });
Ejemplo n.º 2
0
        /// <summary>
        /// 公共保存数据库排序的方法(从大到小)
        /// </summary>
        /// <typeparam name="TModel">实体类</typeparam>
        /// <typeparam name="TValue">主键</typeparam>
        /// <param name="index">主键</param>
        /// <param name="sort">排序字段</param>
        /// <param name="indexList">主键列表</param>
        protected virtual void SaveSort <TModel, TValue>(Expression <Func <TModel, TValue> > indexField, Expression <Func <TModel, short> > sortField, params TValue[] indexList) where TModel : class, new()
        {
            short sort = (short)indexList.Length;

            if (sort == 0)
            {
                return;
            }
            string indexName = null;

            if (indexField == null)
            {
                ColumnProperty[] columns = typeof(TModel).GetKey();
                if (columns.Length != 1)
                {
                    throw new Exception("Primary key error");
                }
                indexName = columns[0].Name;
            }
            else
            {
                indexName = indexField.GetColumn().Name;
            }
            string sortName = sortField == null ? "Sort" : sortField.GetColumn().Name;

            using (DbExecutor db = NewExecutor(IsolationLevel.ReadUncommitted))
            {
                foreach (TValue value in indexList)
                {
                    db.ExecuteNonQuery(CommandType.Text, $"UPDATE [{typeof(TModel).GetTableName()}] SET [{sortName}] = @Sort WHERE [{indexName}] = @Value", new
                    {
                        Sort  = sort,
                        Value = value
                    });
                    sort--;
                }
                db.Commit();
            }
        }