I used to use RNGCryptoServiceProvider to generate string-based Order ID’s, but, there were 4 instances where ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789!@$%^*()_- would generate an already-existing Order ID.
And it got me thinking…
Why can’t we just use something like:
<html>
...
<p>@GenerateOrderId()</p>
...
</html>
and:
public string GenerateOrderId()
{
return "OID" +
DateTime.Now.Year + DateTime.Now.Month +
DateTime.Now.Day +
DateTime.Now.Hour +
DateTime.Now.Minute +
DateTime.Now.Second +
DateTime.Now.Millisecond;
}
I’ve been told that it’s a bad idea, but without any explanation, or reason, it’s still stuck in my head.
… The reason I’d like to use this, is because it will always be unique.
Computers work at nanosecond speeds. You would be guaranteed to generate a duplicate order ID almost immediately.
Your best bet would be to use a GUID [MSDN-C#] [MSDN-SQL] (a.k.a.
UNIQUEIDENTIFIERin the SQL world).