SQL插入语句执行两次
防止用户双击或刷新页面导致执行 SQL 插入语句两次的最佳方法是什么,我尝试在单击后禁用该按钮,但结果并不是很好。我希望可以从代码隐藏中做到这一点。更像是 SQL 提交和回滚
what is the best way to prevent user double click or refresh page that would cause execute SQL insert statement twice, I've tried to disable the button after click, but the result is not really good. I am expecting that it is possible to do it from code-behind. something more like SQL commit and rollback
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
也许PRG 维基百科文章可以帮助你:
Perhaps PRG wikipedia article can help to you:
如果您希望防止这种情况发生,您将需要服务器知道当前用户已经开始操作,并且在满足条件之前无法再次开始操作。
您需要能够从众多可能访问您网站的用户中识别出该用户。使用 SessionState 可以最简单地完成此操作,但如果您对 SessionState 没有其他需要并且希望大规模扩展您的应用程序,则可以使用一个简单的随机 cookie 来识别用户,作为您用于将项目放入 SessionState 的任何键的前缀。服务器缓存。
假设您使用了 SessionState。您将执行类似以下操作(伪):
上面的内容不考虑以下内容:
finally
块中的任何连接If you wish to protect against this you're going to need the server to be aware that the current user has already begun an action, and can't begin it again until a condition is met.
You need to be able to identify that user amongst the many that are potentially visiting your site. This is most simply done using SessionState, but if you have no other need for SessionState and wish to scale your application massively, a simple random cookie to identify the user can be used as a prefix for any keys that you use to place items into the server cache.
Let's say you used SessionState. You'd do something like the following (pseudo):
The above does not account for the following:
finally
block