代替数据库的自增主键的解决方案,解决了批量导数据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(); 
                } 
            } 
        }

数据库结构

本文转自博客园张占岭(仓储大叔)的博客,原文链接:代替数据库的自增主键的解决方案,解决了批量导数据ID出错的问题,如需转载请自行联系原博主。

上一篇:如何解决MFCO42D.dll 0xC0000005:Access Violation问题


下一篇:阿里云物联网平台数据转发到消息服务(MNS)示例