如何在没有数据库的情况下保留自动增量/标识值?
我收到了一个紧急项目(asp.net c#框架v2),当然要在周一早上截止。这是一个非常简单的项目——向现有站点添加“请求报价”页面。基本上,收集一些信息,然后通过电子邮件将表格内容发送给公司的某人,并向用户显示“谢谢”页面。就像馅饼一样简单......直到我刚刚阅读了最后一个要求。
每一份提交的表单都应该有一个“确认编号”,从 10000 开始,每次提交都会将数字依次递增 1。
听起来很容易吧?嗯,我在这个网站上没有数据库。不知道为什么,除了满足要求所需的信息之外,我真的不知道有关该网站的任何其他信息。
因此,考虑到这一点,并意识到这不是最佳的,我想我唯一的解决方案是创建一个文本文件(恶心)并读入它,获取最后使用的数字,添加一个,然后将其写回文本文件。当然,在文件“锁定”方面还有很多需要改进的地方,这样我就不会得到重复的数字。
有人对我有什么建议吗?此时我对任何事情都持开放态度...
I've received a rush project (asp.net c# framework v2), of course due on Monday morning. It's a very simple project -- add a "Request Quote" page to an existing site. Basically, collect some info and then email someone at the company the contents of the form, and show the user a "Thank You" page. Simple as pie...until I just read the last requirement.
Every form submission is supposed to have a "Confirmation Number", starting with 10000, and each one successively increments the number by one.
Sounds so easy right? Well, I don't have a database in this site. No idea why, I really don't know anything else about the site other than the info I need to fill the requirements.
So, with this in mind, and realizing that this is less than optimal, I guess my only solution is to make a text file (yuck) and read it in, get the last number used, add one, and write that back to the text file. Which of course leaves a lot to be desired in respect to "locking" of the file so I don't get duplicate numbers.
Anyone have any suggestions for me? I'm open to anything at this point...
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
首先,我假设每个进程都有某种可用的唯一标识符。这可以是一个进程 ID,或者是在没有竞争条件的情况下保证唯一的东西。将此值写入具有相同名称的文件,这样就不会出现创建该文件的竞争。然后,将其移动到名为“lock”的文件(如果不存在)。通过查看内容来检查是否成功发生。现在从确认号文件中读取值。检查“锁定”文件中是否包含您的唯一标识符 - 这可以确保不会有两个人试图将其文件移动到锁定位置。如果是您的,请将增加的号码和您的唯一 ID 写回确认号码文件。再次检查您是否持有锁,并且确认号码文件中有您的 ID。然后删除锁定文件。
如果失败,只需等待锁定文件消失,然后重试。
这应该允许您以无竞争的方式写入文件,并确保序列号始终加一。
First, I'm assuming each process has some sort of unique identifier available. This could be a process ID, or something that is guaranteed to be unique without race conditions. Write this value to a file with the same name, so that you don't have a race creating that file. Then, move it to a file called "lock" if it doesn't exist. Check that this happened successfully by looking at the contents. Now read the value from the Confirmation number file. Check that the "lock" file has your unique identifier in it -- this ensures there wasn't a race from two people trying to move their file to lock. If it is yours, write back the incremented number and your unique ID to the Confirmation number file. Check again you hold the lock, and that the Confirmation number file has your ID. Then remove the lock file.
Should this fail, just sit around waiting for the lock file to disappear, then try again.
This should allow you write to a file in a race-free manner, and ensures that the sequence number always increases by one.
您可以跟踪 Windows 注册表中的确认号,但如果有该选项,按照您的建议使用简单的文本文件可能会更容易。确保每次都清理(即处置)文件句柄以释放任何文件锁。
You could keep track of the confirmation number in the Windows Registry, but given the option it's probably easier to use a simple text file as you suggest. Make sure you clean up (i.e. Dispose) the file handles each time to release any file locks.
我认为 XML 是一个很好的解决方案。
此帖子可能会让您更轻松地使用 xml。
I would think of XML as a good solution.
This post might make xml even easier for you.