开发者

Possible to generate sequential numbers without database table?

开发者 https://www.devze.com 2023-03-01 14:20 出处:网络
I\'m writing a simple web form for a client in ASP.NET. The form sends an e-mail when submitted. One of the requirements is that each e-mail should have a sequential ticket number.

I'm writing a simple web form for a client in ASP.NET. The form sends an e-mail when submitted. One of the requirements is that each e-mail should have a sequential ticket number.

The two options I've come up with for accomplishing this involve writing the last number to a database, then selecting it out and incrementing it, or doing a similar operation with a text file.

I'd like to avoid an external dependency if possible, and creating a database table for a single value seems like overkill. The text file approach is really hacky, though, and prone to f开发者_StackOverflow中文版ailure if the text file got deleted or overwritten.

Are there any other options I might consider?


I would probably use a database with an identity (auto-increment) column, mostly because it takes care of locking between multiple threads or distributed versions of the same app. It also recovers nicely from restarts and other system events.

Most databases provide methods that return the incremented id during an insert operation so you shouldn't have to select the record back out to get the incremented id.


You can put a counter in you application state. However, such a thing would be lost on application restart (e.g. IIS restarrt) and not be shared across multiple webservers in a farm. I don't recommend this


You could serialize an object to disk, then deserialize when you need it. Place the serialized file in a protected location. There are concurrency issues with this method though.

0

精彩评论

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