[英]Adding +1 in a auto increment value in SQL using vb
I want to generate an id in my vb program it will get the latest auto incremented number and will add one but my code don't work am I missing some thing or am I doing it wrong? 我想在我的vb程序中生成一个ID,它将获得最新的自动递增的数字并添加一个,但是我的代码不起作用是我遗漏了某些东西还是做错了? please help me!
请帮我! THANKS!
谢谢!
Here is the code: 这是代码:
OpenServer()
Dim num As Integer
Dim num1 = num + 1
Newdataset("SELECT MAX(IDnum) AS IDnum FROM addnewemployee WHERE IDnum = '" & num & "' ")
txtEmpNumber.Text = "" & num1 & "." & dtpdatehired.Value.ToString("yyyyMMdd") & ""
Your method has several flaws. 您的方法有几个缺陷。
First, your SQL statement should be: 首先,您的SQL语句应为:
SELECT MAX(IDnum) AS IDnum FROM addnewemployee
This will get the highest IDnum
from the addnewemployee
table. 这将获得最高
IDnum
从addnewemployee
表。 The problem with this is that if a higher record has been created and deleted. 问题在于,如果创建并删除了更高的记录。 Lets say that you get a return value of 55 .
假设您得到的返回值为55 。 It is possible a record with an id of 56 has been created and deleted.
可能已创建并删除ID为56的记录。
The query you really want to use is 您真正要使用的查询是
SHOW TABLE STATUS WHERE `Name` = 'addnewemployee'
Where will get a column named Auto_increment
. 哪里将获得一个名为
Auto_increment
的列。 Get that value from the dataset, then increment it. 从数据集中获取该值,然后将其递增。
But it is a bad idea to do this. 但这是一个坏主意 。
If you have multiple, dependant insert, you shouild either retrieve the id at the time of insert and store that in a variable. 如果您有多个从属插入,则应在插入时检索ID并将其存储在变量中。 Or better still do everything in one statement or stored procedure.
或者最好还是在一个语句或存储过程中做所有事情。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.