C # create a unique order number. Considering the time,

Source: Internet
Author: User

C # create a unique order number. Considering the time,

 

We mainly want to consider the date and other factors.

 

Use the RNGCryptoServiceProvider class to create a unique maximum of 8-digit string.

 

        private static string GetUniqueKey()        {            int maxSize = 8;            int minSize = 5;            char[] chars = new char[62];            string a;            a = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890";            chars = a.ToCharArray();            int size = maxSize;            byte[] data = new byte[1];            RNGCryptoServiceProvider crypto = new RNGCryptoServiceProvider();            crypto.GetNonZeroBytes(data);            size = maxSize;            data = new byte[size];            crypto.GetNonZeroBytes(data);            StringBuilder result = new StringBuilder(size);            foreach (byte b in data)            {                result.Append(chars[b % (chars.Length - 1)]);            }            return result.ToString();        }

 

Then,

 

            for(int i = 0; i < 10; i++)            {                string str = string.Format("{0}{1}", DateTime.Now.ToString("yyyyMMddHHmmss"), GetUniqueKey());                Console.WriteLine(str);            }            Console.ReadKey();

 

Related Article

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.