简体   繁体   中英

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? 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:

SELECT MAX(IDnum) AS IDnum FROM addnewemployee

This will get the highest IDnum from the addnewemployee table. 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 . It is possible a record with an id of 56 has been created and deleted.

The query you really want to use is

SHOW TABLE STATUS WHERE `Name` = 'addnewemployee'

Where will get a column named 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. Or better still do everything in one statement or stored procedure.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM