问题描述
c#做个流水号,流水号日期+001在添加一个数据后是日期+002判断是否过了一天,如果过了一天流水号重新是日期+001添加数据后是日期+002最好来个完整的代码,
解决方案
解决方案二:
这样不好做判断不过给你推荐一种方式可以用年月日时分秒的方式来做唯一标识
解决方案三:
privatevoidbutton1_Click(objectsender,EventArgse){MessageBox.Show(this.GetNewNum());}privatestringGetNewNum(){DateTimenewDay=DateTime.Now;intnewNum=1;stringlastNum=this.GetLastNum();if(!string.IsNullOrEmpty(lastNum)){string[]splits=lastNum.Split('+');DateTimeday=DateTime.Now;intnum=1;DateTime.TryParse(splits[0],outday);int.TryParse(splits[1],outnum);if(day.Date.CompareTo(DateTime.Today)==0){newDay=day;newNum=num+1;}}returnstring.Format("{0}+{1:000}",newDay.ToShortDateString(),newNum);}privatestringGetLastNum(){//这函数里获取最后/近的流水号returnDateTime.Now.ToShortDateString()+"+002";}
解决方案四:
http://www.cnblogs.com/gdjlc/archive/2010/03/05/2086913.html
解决方案五:
有t-sql的,稍稍超出了你的要求。如你的要求的话,请输入:selectdbo.Get_CodePreview(null,null,null,'yyyymmdd-#3','yymmdd')IFEXISTS(SELECT*FROMsysobjectsWHEREid=OBJECT_ID(N'Get_CodePreview')ANDOBJECTPROPERTY(id,N'ISSCALARFUNCTION')=1)DROPFUNCTIONGet_CodePreviewGO/*****标量值函数:获取编码字符串示例*****/CREATEFUNCTIONGet_CodePreview(@SchemeIdUNIQUEIDENTIFIER,--编码方案ID@DeptIdUNIQUEIDENTIFIER,--部门ID@UserIdUNIQUEIDENTIFIER,--用户ID@CodeFormatNVARCHAR(64),@SerialFormatNVARCHAR(16))RETURNSNVARCHAR(64)ASBEGINDECLARE@UserCodeNVARCHAR(16),@DatetimeDATETIME,@SerialVARCHAR(8),@NumberINT,@DigINT,@CountINTset@Datetime=getdate()set@UserCode=''/*****日期字段*****/if(charindex('yyyy',@CodeFormat)>0)set@CodeFormat=replace(@CodeFormat,'yyyy',datename(yy,@Datetime))if(charindex('yy',@CodeFormat)>0)set@CodeFormat=replace(@CodeFormat,'yy',right(datename(yy,@Datetime),2))if(charindex('mm',@CodeFormat)>0)set@CodeFormat=replace(@CodeFormat,'mm',right('0'+convert(varchar,datepart(mm,@Datetime)),2))if(charindex('dd',@CodeFormat)>0)set@CodeFormat=replace(@CodeFormat,'dd',right('0'+datename(dd,@Datetime),2))/*****自定义字段*****/if(charindex('@',@CodeFormat)>0)set@CodeFormat=replace(@CodeFormat,'@','(X)')/*****用户/部门/编码机构字段*****/if(charindex('$0',@CodeFormat)>0)beginselect@UserCode=CodefromMasterDatawhereID=@UserIdset@CodeFormat=replace(@CodeFormat,'$0',@UserCode)endif(charindex('$1',@CodeFormat)>0)beginselect@UserCode=CodefromSYS_OrganizationwhereID=@DeptIdset@CodeFormat=replace(@CodeFormat,'$1',@UserCode)endif(charindex('$2',@CodeFormat)>0)beginselect@UserCode=CodefromSYS_OrganizationwhereID=dbo.Get_SupOrg(@DeptId,1)set@CodeFormat=replace(@CodeFormat,'$2',@UserCode)endif(charindex('$3',@CodeFormat)>0)beginselect@UserCode=CodefromSYS_OrganizationwhereID=dbo.Get_SupOrg(@DeptId,0)set@CodeFormat=replace(@CodeFormat,'$3',@UserCode)end/*****计算流水码分组标识*****/if(charindex('yyyy',@SerialFormat)>0)set@SerialFormat=replace(@SerialFormat,'yyyy',datename(yy,@Datetime))if(charindex('yy',@SerialFormat)>0)set@SerialFormat=replace(@SerialFormat,'yy',right(datename(yy,@Datetime),2))if(charindex('mm',@SerialFormat)>0)set@SerialFormat=replace(@SerialFormat,'mm',right('0'+convert(varchar,datepart(mm,@Datetime)),2))if(charindex('dd',@SerialFormat)>0)set@SerialFormat=replace(@SerialFormat,'dd',right('0'+datename(dd,@Datetime),2))if(charindex('$0',@SerialFormat)>0)set@SerialFormat=replace(@SerialFormat,'$0',@UserCode)if(charindex('$1',@SerialFormat)>0)set@SerialFormat=replace(@SerialFormat,'$1',@UserCode)if(charindex('$2',@SerialFormat)>0)set@SerialFormat=replace(@SerialFormat,'$2',@UserCode)if(charindex('$3',@SerialFormat)>0)set@SerialFormat=replace(@SerialFormat,'$3',@UserCode)if(charindex('@',@SerialFormat)>0)set@SerialFormat=replace(@SerialFormat,'@','(X)')/*****流水码字段*****/if(charindex('#',@CodeFormat)>0)beginset@Dig=cast(substring(@CodeFormat,charindex('#',@CodeFormat)+1,1)asint)if@Dig>0beginselect@Number=isnull(max(SerialNumber),-1)+1fromSYS_Code_RecordwhereSchemeId=@SchemeIdandRelationChar=@SerialFormatif(@Number%power(10,@Dig)=0)set@Number=@Number+1set@Serial=right(replicate('0',@Dig)+cast(@Numberasvarchar),@Dig)endelseselect@Serial=min(AllotNumber)fromSYS_Code_AllotwhereSchemeId=@SchemeIdandOwnerId=@UserIdandBusinessIdisnullset@CodeFormat=replace(@CodeFormat,substring(@CodeFormat,charindex('#',@CodeFormat),2),@Serial)endRETURN@CodeFormatENDGO