现在有个这样的产品编号 0610010001
061001当前日期,就此格式,怎样得到这样的日期格式?
0001(0001-9999)自动编号,当日期是同一天时,每插入一条数据,编号变量加一,如果日期到了第二天,则将编号变量清为0,然后每插入一条数据,编号变量加一,就这样循环
怎样把以上2个组合成0610010001,我是超级菜鸟,麻烦各位前辈指点,麻烦说详细点,谢谢了
我用的是vs.2003(VB)
061001当前日期,就此格式,怎样得到这样的日期格式?
0001(0001-9999)自动编号,当日期是同一天时,每插入一条数据,编号变量加一,如果日期到了第二天,则将编号变量清为0,然后每插入一条数据,编号变量加一,就这样循环
怎样把以上2个组合成0610010001,我是超级菜鸟,麻烦各位前辈指点,麻烦说详细点,谢谢了
我用的是vs.2003(VB)
你所要的日期=str.SubString(0,6);
private DateTime _lastTimeDate;private int GetSN()
{
lock(this)
{
if (_lastTimeDate != DateTime.Now.Date)
{
sn = 0
_lastTimeDate = DateTime.Now.Date;
}
else
{
sn++;
}
}public string GetId()
{
string.Format("{0,6:yyMMdd}{1,4:0000)", DateTime.Now, this.GetSN());
}
private DateTime _lastTimeDate;private int GetSN()
{
lock(this)
{
if (_lastTimeDate != DateTime.Now.Date)
{
_sn = 0;
_lastTimeDate = DateTime.Now.Date;
}
else
{
_sn++;
}
return _sn;
}
}public string GetId()
{
return string.Format("{0,6:yyMMdd}{1,4:0000)", DateTime.Now, this.GetSN());
}
在ID类型为Bigint默认值为GetNum() GetNum是用户自定义函数返回根据日期得到的Bigint
GO
/****** 对象: UserDefinedFunction [dbo].[GetNum] 脚本日期: 10/24/2006 11:21:41 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
-- =============================================
-- Author: lxsfg
-- Create date: 2006/10/24
-- Description: 返回唯一编号
-- =============================================
CREATE FUNCTION [dbo].[GetNum]
(
-- Add the parameters for the function here
)
RETURNS bigint
AS
BEGIN
-- Declare the return variable here
DECLARE @Result bigint
DECLARE @StrDate varchar(20)
DECLARE @StrNum varchar(10) SET @StrDate=cast(YEAR(GETDATE()) as varchar(10))+cast(month(GETDATE()) as varchar(10))+cast(day(GETDATE()) as varchar(10))
--你可以建一个表存放每天的编号通过读表然后将值赋给@StrNum
--SET @StrNum=读取Num表
--SET @StrDate=@StrDate+@StrNum SELECT @Result = cast(@StrDate as bigint)
-- Return the result of the function
RETURN @ResultEND
在这个例子里GetDate是一个系统函数,OK 那么你现在需要一个自动编号而且有一定的业务逻辑那么你自己就定义一个函数这个函数会根据你的业务逻辑返回一个Bigint的编号,你要做的是在你的自动编号列把默认值设为这个函数 如:GetNum()
Public Class SN
Private _lastTimeDate As DateTime
Private _sn As Integer Public Sub New()
End Sub Public Function GetId() As String
Return String.Format("{0,6:yyMMdd}{1,4:0000)", DateTime.Now, Me.GetSN)
End Function
Private Function GetSN() As Integer
SyncLock Me
If (Me._lastTimeDate <> DateTime.Now.Date) Then
Me._sn = 0
Me._lastTimeDate = DateTime.Now.Date
Else
Me._sn += 1
End If
Return Me._sn
End SyncLock
End Function
End Class
CREATE PROCEDURE [return_Code]
AS
BEGIN
DECLARE @intID INT, @strID CHAR(16), @intY INT, @intM INT, @intD INT, @myDate datetime, @xtDate datetime
SET @myDate=GETDATE()
SET @intY=YEAR(@myDate)
SET @intM=MONTH(@myDate)
SET @intD=DAY(@myDate)
SELECT @xtDate =SaveDate ,@intID=Code FROM JXC_Code
IF ((@intY!=YEAR(@xtDate)) OR (@intM!=MONTH(@xtDate)) OR (@intD!=DAY(@xtDate)))
BEGIN
UPDATE JXC_Code SET Code='100002'
SET @intID='100001'
END
ELSE
BEGIN
UPDATE JXC_Code SET Code=Code+1
END
SET @intY=YEAR(@myDate)
SET @intM=MONTH(@myDate)+100
SET @intD=DAY(@myDate)+100
SET @strID=STR(@intY,4,1)+SUBSTRING(STR(@intM,3,1),2,2)+SUBSTRING(STR(@intD,3,1),2,2)+SUBSTRING(STR(@intID,6,1),2,5)
SELECT @strID as code
END
GO//取得单号
string InPutCode="0";
SqlDataReader myReader = Database.ExecuteReader(Database.connString,CommandType.StoredProcedure,"return_Code",null);
while(myReader.Read())
{
InPutCode=Convert.ToString(myReader["code"]);
}
myReader.Close();
Database.CloseConnection();
这样就能取得自动编码。
要得新编号,可以将当前时间作为参数传进去,比如为061023 -- 功能:自动递增生成公告编号
-- 作者:job_2006
-- 时间:
-- 修改人 修改日期 修改内容create PROCEDURE [dbo].[funCreateBulletinNo]
@charDate char(8), --当前日期
@varDocNo varchar(15) output --返回新编号
AS
/* SET NOCOUNT ON */
SELECT @varDocNo=CONVERT(BIGINT,ISNULL(MAX(DocNo),@charDate+'0000'))+1
FROM funBulletinM
WHERE DocNo like @charDate + '%'
RETURN
#region 获取新增文档编号
/// <summary>
/// 获取新增文档编号,川川、2006-06-28、修改人、修改日期
/// </summary>
/// <param name="strDate">当天日期</param>
/// <returns>
/// 文档编号
/// </returns>
//这里的strDate你要获取这样的样子:061023
private string GetNewDocNo(string strDate)
{
SqlParameter[] pamDocNo = {
new SqlParameter("@charDate",SqlDbType.Char,8,ParameterDirection.Input,true,0,0,null,DataRowVersion.Current,strDate),
new SqlParameter("@varDocNo",SqlDbType.VarChar,15)
};
pamDocNo[1].Direction = ParameterDirection.Output;
bool Flag = objDB.ExecuteProcedure("funCreateBulletinNo", pamDocNo);
if (!Flag)
{
Response.Write("<script language='javascript'>alert('获取编号失败!')</script>");
return "";
}
return pamDocNo[1].Value.ToString().Trim();
}
#endregion