开发者

How to create an "activation code" and store it in sql server 2005 database

I am making a web application that when user registered would randomly create an activation code and put it in a database. The web application would then send it in an emai开发者_如何转开发l to the user. How would Ido this? and what sql server data type would I use? I want the activation code to be no longer than 12 characters of number. please can any one help me


Probably the most common approach would be to use a Guid. It won't fit in 12 characters though, base 64 it would be 22 characters, which doesn't meet your needs. If you really need it short what I'd probably do is base64 encode a new Guid, and simply take the left 12 characters, then check that it is unique.

var guid = Guid.NewId();
string str = ConvertToBase64(guid.ToByteArray());
return str.Substr(0, 12);

Note, you must ensure that this code is unique by searching for it in your database. The range is to small to ensure uniqueness. Probably you'd be better off simply relaxing your requirement on 12 characters, but I don't know your app.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜