获取数据库表中的最后一个值

编程入门 行业动态 更新时间:2024-10-10 12:23:51
本文介绍了获取数据库表中的最后一个值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

大家好 请帮帮我 我有下表 FileCode ...名称 125 .. A 126 .. B 127 .. C 我有文本框,我希望用表格中的最后一个值填充它,然后增加一个像128 .. 129 ..等等 我得到了它,但是当我尝试在FileCode列中插入文本框的值128 它出错了,因为其他的已经在我之前插入数据库并取了数字128 当两个同时插入同一个表时,我怎么能避免这些错误

Hi all Please Help me I have the Following table FileCode ... Name 125 .. A 126 .. B 127 .. C and I have textbox and I want to fill it with the last value in table and increase by one like 128 .. 129 .. etc I got it but when I try to insert in "FileCode" Column the textbox's value "128" it got error because other one already inserted in database before me and took the number 128 how can I avoid these errors when two inserting in the same table at the same time

推荐答案

你必须处理这个问题...... 最简单的方法是让数据库担心使用标识列(序列)并在插入时输入它而不让它用户在插入完成之前看到了这个数字。 第二个更难,因为你有并发性ry about,你必须接受你的FileCode序列中可能有漏洞 有一个表(比如COUNTER)保持FileCode的当前最大值 获得当前值后,立即将其增加1。下一个为这个数字而来的人会得到更多的数字,也会增加它。 这些漏洞来自那些因任何原因没有完成插入的用户 - 他们仍然用完了这个号码。 还有第三种方法就是向用户显示一个号码,但在插入之前检查它是否存在然后更改它在运行中,然后刷新用户视图,但我认为这是糟糕的用户体验。 祝你好运! You have to ways of handling this... Easiest is to let the database worry about it by using identity column (sequence) and enter it at the insert time not letting users see the number before insert is done. Second one is harder as you have concurrency to worry about and you have to accept that you might have "holes" in your FileCode sequence Have one table (say COUNTER) hold current max value of the FileCode When you get current value, increase it by one immediately. Next person that comes for the number will get that increased number and will also increase it. The holes come from users that don't finish the insert for whatever reason - they still "used up" the number. There is third way and that is to show the user one number, but check if it exists before the insert and then change it "on the fly" and then refresh user view, but I think this is bad user experience. Good luck!

试试这样: 1)点击 [ ^ ] 2)创建一个触发器来更新在Sql server中插入行时的FileCode

更多推荐

获取数据库表中的最后一个值

本文发布于:2023-10-19 02:05:20,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1506066.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:数据库

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!