0

I'll try to word this as best I can.

I'm coding a website using C# MVC4 with a MS SQL database. Whenever a user submits an application via the website I do a look-up on the table, find the highest numbered session code (7 digit, number) add 1 to this number and populate it in the database along with the user details.

We are on the verge of starting a huge add campaign and there will be a massive influx of users. I am worried that if two users submit their applications at the same time that the same session code will be generated for both. What are some practices to stop this? I'm thinking make the field unique in the DB and have a loop surrounding my session code generation and population to try again should it fail.

Is there a better practice than this (other than creating a composite primary key)?

4

3 回答 3

5

您可以identity从您喜欢的任何数字开始制作数字。然后数据库会自行将最高数字加 1。

请参阅MSSQL 文档

于 2013-08-23T08:29:39.827 回答
1

您可以将数字设置为标识符并让它自动递增 Microsoft SQL Server 2008 R2 的索引自动递增

否则,如果可以更改数据类型 - 创建 Guids(数据库中的唯一标识符)。那么你对碰撞很安全

于 2013-08-23T08:31:32.800 回答
0

在 SQL Server Management Studio 中将列属性设置为

身份规范 是(IS 身份) 是 身份增量 1 身份种子 [您的最后一个号码]

这会将该列设置为自动递增,然后在向数据库提交数据时不再需要设置该列

于 2013-08-23T08:33:37.520 回答