生成分类表中分类编号的方法

2017-05-22  本文已影响29人  niunan

用到了dapper,分类表结构:id,createtime,caname,bh,pbh,remark

/// <summary>生成分类表中分类编号</summary> 
    /// <param name="pbh">父编号</param>
    /// <param param name="x">每一级编号的位数</param>
    /// <returns></returns>
    public string GenBH(string pbh, int x)
    {
        string sql = "select right(max(bh)," + x + ") from category where pbh='" + pbh+"'";
        using (var connection = ConnectionFactory.GetOpenConnection())
        { 
            string res = connection.QuerySingle<string>(sql);
            if (string.IsNullOrEmpty(res))
            {
                int a = 1;
                if (pbh != "0")
                {
                    return pbh + a.ToString("d" + x);
                }
                return a.ToString("d" + x);
            }

            else
            {
                int a = int.Parse(res) + 1;
                int b = (int)Math.Pow(10, x);
                if (a <= b)
                {
                    throw new Exception("编号超过限制!");
                }
                if (pbh != "0")
                {
                    return pbh + a.ToString("d" + x);
                }
                return a.ToString("d" + x);
            }
        } 
    }
上一篇 下一篇

猜你喜欢

热点阅读