Click here to Skip to main content
15,887,386 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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.
Posted
Comments
Maciej Los 24-Apr-13 10:51am    
Do you want to that on server side using stored procedure?

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

C#
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:

C#
//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.

C#
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);
        }
    }
}
 
Share this answer
 
Comments
musiw 21-Apr-13 21:16pm    
date will automatically get from the system.the station id also get from system.only the sequence number...how to append and increasing
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[^]
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900