Problems encountered by time-unique serial numbers on PPC and Their Solutions

Source: Internet
Author: User
In. NET Compact Framework 1.0, when you use datetime. now. when millisecond obtains the millisecond of the current time, you will find that its value is always 0, that is, we cannot get the millisecond value of the time.
This raises a problem. When developing a database application based on SQL Server ce, if you want to obtain the value of the current time to generate the primary key value of the table, the probability of duplicate primary key values is very high, but we can use the following Code To generate a unique primary key value.

1 ///   <Summary>
2 /// Generate a unique ID class on the Pocket PC.
3 ///   </Summary>
4 Public   Class Recordidmaker
5 {
6 ///   <Summary>
7 /// The time when the serial number was obtained.
8 ///   </Summary>
9 Private   Static Datetime lasttime = Datetime. minvalue;
10
11 ///   <Summary>
12 /// The pseudo-millisecond value calculated based on the number generated last time.
13 ///   </Summary>
14 Private   Static   Int Lastmillisecond =   0 ;
15
16 ///   <Summary>
17 /// The unique number that generates the time style.
18 ///   </Summary>
19 Public   Static   String Newid ()
20 {
21 String Newid;
22
23 Datetime timenow = Datetime. now;
24 If (Lasttime = Timenow)
25 {
26 Lastmillisecond ++ ;
27 }
28 Else
29 {
30 Lastmillisecond =   0 ;
31 }
32
33 // Generate a new number using the current time
34 Newid = Timenow. tostring ( " Yyyymmddhhmmss " ) + Lastmillisecond. tostring ( " 000 " );
35
36 Lasttime = Timenow;
37
38 Return Newid;
39 }
40 }

The above method can only ensure that the generated primary key value is unique in the SQL Server ce database on the local PPC. If your PPCProgramIt is just a front-end program for data collection. You must consider that multiple PPC data may have the same primary key. If you have encountered such a requirement, it is best to add other characters after the generated primary key value to reduce the probability of duplication, such as: Login User number, the hardware Number of the PPC device. In this way, try to avoid two identical primary key values.
The method described in this article applies to distributed application systems. For Single-host PPC applications that do not require data synchronization with SQL Server, the identity column can be used to ensure the uniqueness of the primary key value.

Sample source code

The above is my summary of development experience during this period. If something is wrong, I hope you can correct it!

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.