简体   繁体   English

将值插入 C# 中的 SQL 服务器表后,如何将值加 1?

[英]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.

相关问题 当在 sql server 表中插入特定值时,如何在 C# 中运行事件..? - How to run an event in C# when a Particular value is inserted in sql server table..? C#如何在插入SQL Server数据库后立即读取数据? - C# How do I read data from an SQL Server database as soon as it is inserted? 如何在C#中使用一个命令将条目插入SQL Server数据库并返回包含主键标识的已插入列? - How do I insert an entry into a SQL Server database and return inserted columns including primary key identity with one command in C#? 与本地字符串值c#比较后如何在sql server表中存储值 - how to store value in sql server table after comparing with local string value c# 如何在C#Windows Form应用程序中获取SQL Server中今天插入的数据的记录 - How to get record of today's inserted data in SQL Server in C# windows form application C#和SQL Server:如何通过使用C#调用存储过程来从SQL表中获取值? - C# and SQL Server: How do I get values from my SQL Table, by calling my stored procedure with C#? 如何使用C#为sql server表添加列? - How to use C# to add a column for a table of sql server? 自动插入后如何在 C# WPF 中选择新的 ListBoxItem - How do I select a new ListBoxItem in C# WPF after I just inserted it automatically 如何从SQL Server向C#添加DateTime值中的分钟 - How to add minutes in DateTime value from SQL Server to C# 如何在C#中的标签中显示基于关系的SQL Server表值 - How can I show the relationship based SQL Server table value in label in C#
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM