Id变为10后不增加 [英] Id is not incremented after it become 10

查看:85
本文介绍了Id变为10后不增加的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

SqlCommand cmd1 = new SqlCommand("select max(id) from login1",conn);
                      int id1 = Convert.ToInt32(cmd1.ExecuteScalar());
               id1 =id1 + 1;
//Here  id is primary key.but it will not increment after digit 10,it remain as it is

推荐答案

1)id1int,不能将您的数量限制为10.
2)login1中的id列可能是tinyint,它也允许0到255,所以没问题.

这使我想到要检查您是否对login1有任何愚蠢的约束.
1)id1 is int, which can''t limit your count to 10.
2)id column in login1 could possibly be tinyint, which also allows 0 to 255, so no problem.

It makes me think to check if you have any silly constraint on login1.


您这样做的方式是错误的,是您现在应该摆脱的习惯

数据库的工作是分配主键标识符,而不是代码.尝试获得一个字段的MAX,然后加1并不是您应该这样做的方式.

相反,应将表 login1 中的字段 id 设置为 Identity (SQL Server)

这意味着当您将记录添加到表中时,数据库引擎将自动为新记录分配一个ID,然后您可以检索该ID并返回到您的应用程序代码.
The way you''re doing this is wrong and is a habit you should get out of right now

It''s the job of the database to allocate primary key identifiers, not your code. Trying to get a MAX of a field, then increment by 1 is not the way you should be doing this.

Instead, your field id in table login1 should be set as an Identity (SQL Server)

This means when you add a record to the table, the database engine will automatically assign an ID to the new record, which you can then retrieve and return to your application code.


hi,

始终尝试将id列设置为identity(1,1),它会自动将int值1插入您的primay id列,当您插入其他记录时,它会将它递增1,您也可以根据需要进行更改 因为它会减少您出错的机会.



always try to give id column as identity(1,1) it will automatically insert int value 1 to your primay id column and when you insert other record it will increment it by 1 you can also changed that as required
as it will reduce your chances of error.

create table test(id int primary key identity(1,1),Name varchar(200))
insert into test values ('Pratik')


这篇关于Id变为10后不增加的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆