获取数据库表中的最后一个值 [英] get the last value in database table

查看:265
本文介绍了获取数据库表中的最后一个值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好

请帮帮我



我有下表



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


这篇关于获取数据库表中的最后一个值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆