//需绑定数据库
//code 定义的前缀名称,tableName 表名,columnName 列名,number 流水号位数 左边补0;
public static string GetMaxNum(string Code,string TableName,string columnName,int number)
{
int max =1;//定义流水号最大值
string curCode = string.Empty;//定义当前 前缀格式
string sql ="select MAX("+columnName+") from " + TableName ;
int len =0; //定义字符长度
if(!string.isNullOrEmpty(Code))
{
len = Code.length + num;
sql+= " where "+columnName+" like '"+Code+"%' and len ("+columnName+")="+len;
}
DataTable dt = new SqlHelper().GetTable(sql);//sqlhelper是连接数据中调用的相关方法。需要根据你的项目自行改造
if(dt.Rows.Count>0)
{
curCode = dt.Rows[0][0].ToString();
if(!string.IsNullOrEmpty(curCode))
{
max = Convert.ToInt32(curCode.subString(curCode.Length-num,num))+1;
}
}
Code = Code + max.ToString().PadLeft(num,'0');
return Code;
}