.NET ------ 新增时编号自动给编码,(编码可以看出日期)
如图:
在逻辑层进行调用
/// <summary> /// 根据当天的序号,且是同一个前缀类型的单据进行编码,iFlag小于0时无Flag逻辑分隔 /// </summary> public string Get_Ex_Mark(string sFront, string sTableName, string sFiledName, string sQiyeGuid, int iFlag) { string sDate1 = DateTime.Now.ToString("yyyy-MM-dd 00:00:00"); string sDate2 = DateTime.Now.ToString("yyyy-MM-dd 23:59:59"); int ilen = sFront.Length; string strsql = "select max(cast(right(" + sFiledName + ",4) as int)) as iNumber from " + sTableName + " where PATINDEX('%[^0-9]%', right(" + sFiledName + ",4))=0 and QiyeGuid='" + sQiyeGuid + "' and CreateTime between '" + sDate1 + "' and '" + sDate2 + "' and left(" + sFiledName + "," + ilen + ")='" + sFront + "'"; if (iFlag >= 0) strsql += " and Flag=" + iFlag; object obj = GetScalarByTx(strsql); if (obj == DBNull.Value) { return sFront + DateTime.Now.ToString("yyyyMMdd") + "0001"; } else { int k = Convert.ToInt32(obj); k++; return sFront + DateTime.Now.ToString("yyyyMMdd") + k.ToString("0000"); } }
也可进行调整,控制后缀长度,控制前缀
/// <summary> /// 根据当日yyMMdd0001编码,默认最后4位编码,sFront可以是DeptCode,系统自动简化Code /// </summary> public string Get_Ex_Mark(string sFront, string sTableName, string sFiledName, string sQiyeGuid, int iLastLen, int iFlag) { try { if (sFront.Length >= 9) { string sNumber = string.Empty; for (int i = 0; i <= sFront.Length - 1; i = i + 9) { if (i == 0) sNumber += Convert.ToInt32(sFront.Substring(i, 9)).ToString("0"); else sNumber += Convert.ToInt32(sFront.Substring(i, 9)).ToString("00"); } sFront = sNumber; } } catch { } string sDate1 = DateTime.Now.ToString("yyyy-MM-dd 00:00:00"); string sDate2 = DateTime.Now.ToString("yyyy-MM-dd 23:59:59"); int ilen = sFront.Length; string strsql = "select max(cast(right(" + sFiledName + "," + iLastLen + ") as int)) as iNumber from " + sTableName + " where PATINDEX('%[^0-9]%', right(" + sFiledName + "," + iLastLen + "))=0 and QiyeGuid='" + sQiyeGuid + "' and CreateTime between '" + sDate1 + "' and '" + sDate2 + "' and left(" + sFiledName + "," + ilen + ")='" + sFront + "'"; if (iFlag >= 0) strsql += " and Flag=" + iFlag; object obj = GetScalarByTx(strsql); if (obj == DBNull.Value) { int k = 1; string s = string.Empty; if (iLastLen == 1) s = k.ToString("0"); else if (iLastLen == 2) s = k.ToString("00"); else if (iLastLen == 3) s = k.ToString("000"); else if (iLastLen == 4) s = k.ToString("0000"); else if (iLastLen == 5) s = k.ToString("00000"); else if (iLastLen == 6) s = k.ToString("000000"); else if (iLastLen == 7) s = k.ToString("0000000"); else if (iLastLen == 8) s = k.ToString("00000000"); else s = k.ToString("000000000"); return sFront + DateTime.Now.ToString("yyMMdd") + s; } else { int k = Convert.ToInt32(obj); k++; string s = string.Empty; if (iLastLen == 1) s = k.ToString("0"); else if (iLastLen == 2) s = k.ToString("00"); else if (iLastLen == 3) s = k.ToString("000"); else if (iLastLen == 4) s = k.ToString("0000"); else if (iLastLen == 5) s = k.ToString("00000"); else if (iLastLen == 6) s = k.ToString("000000"); else if (iLastLen == 7) s = k.ToString("0000000"); else if (iLastLen == 8) s = k.ToString("00000000"); else s = k.ToString("000000000"); return sFront + DateTime.Now.ToString("yyMMdd") + s; } }