[英]How do I add 1 to a value after it's inserted into SQL Server table in C#?
Especially for the columns with ID's, I want to add a value of 1 after I inserted a ID into a SQL Server table, so it will be ID +1.特别是对于具有 ID 的列,我想在将 ID 插入 SQL Server 表后添加一个值,因此它将是 ID +1。 Now I can't figure out a way to do this.
现在我想不出办法来做到这一点。 Does anyone have tips?
有人有提示吗?
In the table creation script, set that column as identity, so you don't need to mention the column name in the insert script, and the SQL server will automatically fill its value with the maximum value +1.在建表脚本中,将该列设置为标识,所以插入脚本中不需要提及列名,SQL 服务器会自动将其值填充为最大值+1。
Here is an example of table creation and how to insert some data into it, and what the result will be.这是一个创建表的示例以及如何将一些数据插入其中,以及结果将是什么。
Creation创建
create table tablename (id int identity,name nvarchar(max))创建表 tablename (id int identity,name nvarchar(max))
Insertion插入
Insert Into TableName(name) values('Test1') Insert Into TableName(name) values('Test2')插入 TableName(name) values('Test1') 插入 TableName(name) values('Test2')
Result结果
ID Name 1 Test1 2 Test2 ID 名称 1 测试 1 2 测试 2
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.