使用SQL Server在VB.net中生成自动编号
问题描述:
我有一张桌子
-tblItem(id,name,category)
ID |名称|分类|
001书籍纸
002钢笔作家
003 Pencel Writer
我希望像那样生成它
ID |名称|类别
PD-001PP书签
PD-002WR钢笔作家
PD-003WR Pencel Writer
I Have a table
-tblItem (id,name,category)
ID | Name | Category |
001 Books Paper
002 Pen Writer
003 Pencel Writer
and i want to generate it like that
ID | Name | Category
PD-001PP Book Paper
PD-002WR Pen Writer
PD-003WR Pencel Writer
答
您可以将ID列设置为关闭身份的主键。
然后,每当您通过VB插入新记录时/>
1)获取最后一个ID。
2)解析字符串以获取右边的数字,例如001等
3)将其增加1
4)将产品代码附加到此数字,例如'PD-'+ 004
5)插入新行
You can set the ID column to a primary key with identity turned off.
Then, whenever you insert a new record through VB
1) get the last ID.
2) parse the string to get the number at the right for e.g. 001 etc
3) increment it by 1
4) append the product code to this number for e.g. 'PD-' + 004
5) insert the new row