从SQL Server获取自动增益编号 [英] Fetch auto incriment number from SQL server
本文介绍了从SQL Server获取自动增益编号的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
如何从sql server到vb.net windows窗体显示自动增量编号到文本框/ lsbel
我尝试过:
how to display auto increment number from sql server to vb.net windows form to text box/lsbel
What I have tried:
Dim Nextid As String
Dim connection As New SqlConnection("server= madhuresh-pc; database= madhu; integrated security= true")
connection.Open()
Dim cmd As New SqlCommand("SELECT MAX(caseno+1) As 'NextId' FROM case1", connection)
Nextid = cmd.ExecuteScalar()
txtstation.Text = Nextid
connection.Close()
End Sub
推荐答案
NO。
不要这样做。
为什么不呢?因为SQL Server是一个多用户环境,所以两个或更多用户最终会得到相同数量的可能性非常非常好,这会导致以后数据完整性出现严重问题。
在插入数据之前,您不需要知道正在使用的ID - 可以使用 SCOPE_IDENTITY(Transact-SQL)| Microsoft Docs [ ^ ]。
如果在INSERT之前需要ID,则不要使用IDENTITY字段 - 使用GUID并从中提供你的演示软件,而不是让SQL照顾它。
NO.
Do not do it, ever.
Why not? Because SQL Server is a multiuser environment, so the odds are very, very good that two or more users will end up with the same number, and that leads to serious problems with data integrity later.
You don't need to know what ID is being used until after the data is inserted - and that can be retrieved using SCOPE_IDENTITY (Transact-SQL) | Microsoft Docs[^] once you have inserted the row.
If you need an ID before you INSERT, then don't use an IDENTITY field - use a GUID and supply it from your presentation software rather than letting SQL look after it.
这篇关于从SQL Server获取自动增益编号的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文