生成像这样得编号 XX2007032100001
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockStart.gif)
/**//// <summary>
/// 最后一个编号 XX200703060001
/// </summary>
/// <param name="fieldName">字段ID号</param>
/// <param name="tableName">表得名字</param>
/// <returns></returns>
public static object GetLastCode(string fieldName, string tableName, int autoBuildCodeLength)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockStart.gif)
...{
string executeString = "SELECT " + fieldName + " FROM " + tableName + " WHERE LEN(" + fieldName + ") = " + autoBuildCodeLength + " ORDER BY ID DESC";
return Common.GetDatabase().GetFirstData(executeString, CommandType.Text);
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockStart.gif)
/**//// <summary>
/// 第一位为0
/// </summary>
private static int _autoBuildCount = 0;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockStart.gif)
/**//// <summary>
/// 自动生成编号 如 "XXXX" + 2007030600001 这样的
/// "XXXX" 传来的
/// </summary>
/// <param name="codeFormat">编号得格式</param>
/// <param name="fieldName">字段ID号</param>
/// <param name="tableName">表得名字</param>
/// <param name="autoBuildCodeLength">自动生成编号得长度</param>
/// <returns></returns>
public static string GetAutomatismBuildCode(string codeFormat, string fieldName, string tableName,int autoBuildCodeLength)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockStart.gif)
...{
string yearMonthDay1 = "";
string yearMonthDay2 = codeFormat + DateTime.Now.Year.ToString() +
NumberIsLessThan10(DateTime.Now.Month) +
NumberIsLessThan10(DateTime.Now.Day);
object autoBuildCode;
string AutomatismNumber = "";
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
autoBuildCode = GetLastCode(fieldName, tableName, autoBuildCodeLength);
if (autoBuildCode == null)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
...{
_autoBuildCount = 1;
}
else
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
...{
yearMonthDay1 = autoBuildCode.ToString().Substring(0, autoBuildCodeLength-5);
if (yearMonthDay2 == yearMonthDay1)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
...{
_autoBuildCount = int.Parse(autoBuildCode.ToString().Substring(autoBuildCodeLength-5, 5));
}
++_autoBuildCount;
}
AutomatismNumber = yearMonthDay2 + _autoBuildCount.ToString("00000");
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
_autoBuildCount = 0;
return AutomatismNumber;
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockStart.gif)
/**//// <summary>
/// 判断月日是一位还是两位数
/// </summary>
/// <param name="aa"></param>
/// <returns></returns>
private static string NumberIsLessThan10(int monthOrDay)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockStart.gif)
...{
if (monthOrDay < 10)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
...{
return "0" + monthOrDay.ToString();
}
return monthOrDay.ToString();
}
XX2007032100001
这个每添加完一条记录时最后一位都自动加一
原文地址:https://www.cnblogs.com/xiaotuni/p/2365766.html