用到了dapper,分類表結(jié)構(gòu):id,createtime,caname,bh,pbh,remark
/// <summary>生成分類表中分類編號(hào)</summary>
/// <param name="pbh">父編號(hào)</param>
/// <param param name="x">每一級(jí)編號(hào)的位數(shù)</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("編號(hào)超過限制!");
}
if (pbh != "0")
{
return pbh + a.ToString("d" + x);
}
return a.ToString("d" + x);
}
}
}