• 代替数据库的自增主键的解决方案,解决了批量导数据ID出错的问题


    namespace Entity
    {
        /// <summary>
        /// 自定义主键编码
        /// </summary>
        public enum PkIdRecordCode
        {
            /// <summary>
            /// OrderID
            /// </summary>
            OR = 10,
            /// <summary>
            /// ProductID
            /// </summary>
            PR = 30,
            /// <summary>
            /// StoreID
            /// </summary>
            ST = 40,
            /// <summary>
            /// UserID
            /// </summary>
            US = 20,
        }
        /// <summary>
        /// 含有自增主键性质的表枚举
        /// </summary>
        public enum PKIDTable
        {
            /// <summary>
            ///     地区信息
            /// </summary>
            Areas = 1,
            /// <summary>
            ///   银行信息
            /// </summary>
            BankInfos = 2,
            /// <summary>
            /// 商品虚拟分类属性
            /// </summary>
            CategoryPropertys = 3,

        }

    }

            /// <summary>
            /// 代替数据库的自增主键的解决方案,解决了批量导数据ID出错的问题
            /// </summary>
            /// <param name="code"></param>
            /// <returns></returns>
            public int GetNewPKID(Entity.PKIDTable code) //由后后台也要建立用户,所以把它改为public -zhangzhanling
            {
                string _code = null;

                int idNum = 0;
                using (TransactionScope transactionScope = new TransactionScope())
                {
                    try
                    {
                        _code = Enum.GetName(typeof(Entity.PKIDTable), code);
                        idNum = base._db.ExecuteQuery<int>("UPDATE PkIdTable SET IDNum= IDNum+1 WHERE Code={0};select IDNum from PkIdTable WHERE Code = {0}", _code).Single();
                        transactionScope.Complete();
                        transactionScope.Dispose();
                        return idNum;
                    }
                    catch (Exception ex)
                    {
                        throw ex;
                    }
                    finally
                    {
                        transactionScope.Dispose();
                    }
                }
            }
          
      /// <summary>
            /// 一些特殊的主键,需要有固定的位数,当位数的数字不够时,在前面补0
            /// </summary>
            /// <param name="code"></param>
            /// <returns></returns>
            public string GetNewID(Entity.PkIdRecordCode code) //由后后台也要建立用户,所以把它改为public -zhangzhanling
            {
                string _code = null;

                int idNum = 0;
                using (TransactionScope transactionScope = new TransactionScope())
                {
                    try
                    {
                        _code = Enum.GetName(typeof(Entity.PkIdRecordCode), code);
                        idNum = base._db.ExecuteQuery<int>("UPDATE PkIdRecords SET IDNum= IDNum+1 WHERE Code={0};select IDNum from PkIdRecords WHERE Code = {0}", _code).Single();
                        transactionScope.Complete();
                        transactionScope.Dispose();
                        return string.Format("{0}{1}", (int)code, idNum.ToString().PadLeft(6, '0'));
                    }
                    catch (Exception ex)
                    {
                        throw ex;
                    }
                    finally
                    {
                        transactionScope.Dispose();
                    }
                }
            }

    数据库结构

  • 相关阅读:
    Vue3源码系列之触发更新的实现
    Vue3源码系列之依赖收集的实现
    Vue3源码系列之reactiveApi实现
    删除链表的倒数第n个节点
    Shared_ptr 参考实现
    linux 目录结构 比较老
    C++11 bind function
    状态机DP
    尾递归
    秒杀系统的构建(2)
  • 原文地址:https://www.cnblogs.com/lori/p/2051237.html
Copyright © 2020-2023  润新知