如何生成包含日期的序列号 [英] how to generate sequence number containing date

查看:84
本文介绍了如何生成包含日期的序列号的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,



i想问这个问题的帮助。我不知道如何生成包含日期的序列号并保存到数据库。



例如:TicketId:130422229000000001



130422 - 今日(机票已发行)

22900 - 车站ID

0000001 - 序列号



之后我想存储到数据库。



请帮我这个



提前感谢

musiw。

解决方案

将您要开始序列的日期转换为刻度。



 DateTime date =  new  DateTime( 2013  4  21 ); 
date.Ticks;





转换为您希望按序列到达的最大日期和您想要的步骤增加。例如,你想增加一天:



  //  转换步骤 
DateTime date2 = new DateTime( 2013 4 22 );
long step = date2.Ticks - date.Ticks;





最后,创建序列,例如作为结构或更好的类。



  public   class 序列
{
long 最小值{获取; set ;}
long maximum { get < /跨度>; set ;}
long step { get < /跨度>; set ;}
long actual { get < /跨度>; set ;}
...
public DateTime Next
{
get
{
actual + = step;
return new DateTime(actual);
}
}
}


如果这是服务器代码,并且必须连续运行,可以获得来自内存的序列,但这太冒险了。



我会在数据库中使用3列表,

Column1:Date

第2列:电台ID

第3列:连续数字



在insert语句中查询数据库类似于;



插入tblTest(DateVal,StationID,Seq)VALUES(@ p1,@ p2,(选择MAX(Seq)+1从tblTest到DateVal = @ p3) )$ / b $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ )





如果您打算使用Sql2012,请查看;

http://msdn.microsoft.com/en-us/library/ff878091.aspx [ ^ ]

hi all,

i want to ask help on this problem.i dont know how to generate sequence number containing date and save to database.

Example: TicketId: 130422229000000001

130422 - date today (ticket issued)
22900 - station id
0000001 - sequence number

after that i want to store to database.

please help me on this

thanks in advance
musiw.

解决方案

Convert the date you want to start the sequence with to ticks.

DateTime date = new DateTime(2013,4,21);
date.Ticks;



Than convert to ticks the maximal date you want to reach by the sequence and a step you want to increment with. For example you want to increment by one day:

//Converting step
DateTime date2 = new DateTime(2013,4,22);
long step = date2.Ticks - date.Ticks;



At the end, create the sequence for example as a struct or better as a class.

public class Sequence
{
    long minimum {get; set;}
    long maximum {get; set;}
    long step {get; set;}
    long actual {get; set;}
...
    public DateTime Next
    {
        get
        {
            actual += step;
            return new DateTime(actual);
        }
    }
}


If this is a server code, and must run continuously, It is fine to get sequence from memory, but this is too risky.

I would use a 3 column table in database,
Column1 : Date
Column2 : Station ID
Column3 : Sequential number

And query database in insert statement something like;

insert into tblTest (DateVal, StationID, Seq) VALUES ("@p1","@p2",(select MAX(Seq)+1 From tblTest Where DateVal = @p3))
@p1 = Now.ToString("yyMMdd")
@p2 = 22900
@p3 = Now.ToString("yyMMdd")


If you are planning to use Sql2012 then take a look at;
http://msdn.microsoft.com/en-us/library/ff878091.aspx[^]


这篇关于如何生成包含日期的序列号的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆