简体   繁体   English

主键列已设置默认标识(1, 1)时如何向sql server数据库插入数据

[英]How to insert data into sql server database when the primary key column already set default identity(1, 1)

I'm trying to insert two data columns into my SQL Server database but I get an error at the code line -> cmd.ExecuteNonQuery();我正在尝试将两个数据列插入到我的 SQL Server 数据库中,但在代码行中出现错误 -> cmd.ExecuteNonQuery();

Cannot insert the value NULL into column OrderID , table RestaurantApp.dbo.Junc_Order ;无法将值 NULL 插入到OrderID列、表RestaurantApp.dbo.Junc_Order column does not allow nulls.列不允许空值。 INSERT fails.插入失败。

The OrderID column is actually the primary key in my data table. OrderID列实际上是我的数据表中的主键。 I set it identity(1, 1) and want to insert other data and meanwhile it can insert 1, 2, 3, 4....automatically.我将它设置为 identity(1, 1) 并想插入其他数据,同时它可以插入 1, 2, 3, 4 ..... 自动。

Here is the part of my code:这是我的代码的一部分:

string insertString = "INSERT INTO Junc_Order(ID, Quantity)values (@ID, @Quantity)";
SqlCommand cmd = new SqlCommand(insertString, conn);
cmd.Parameters.AddWithValue("@ID", r_ID);
cmd.Parameters.AddWithValue("@Quantity", r_Quantity);
cmd.ExecuteNonQuery();

I already get connection with database ahead of these codes, so the problem should not be that.我已经在这些代码之前与数据库建立了连接,所以问题不应该是那个。

Updated Junc_Order table design:更新 Junc_Order 表设计:

OrderID (PK,FK,int,not null)

ID(FK,int,not null)

Quantity(int,not null)

By viewing your question, it seems that your insert query is not correct:通过查看您的问题,您的插入查询似乎不正确:

  1. First of all, you don't need to insert "OrderID" as it is primary key identity so sql server automatically insert it.首先,您不需要插入“OrderID”,因为它是主键标识,因此sql server 会自动插入它。
  2. second, somewhere you are getting "r_ID" as null that's why you are facing error.Verify it and modify your code with the following:其次,在某处您将“r_ID”设为空,这就是您面临错误的原因。验证它并使用以下内容修改您的代码:

    string insertString = "INSERT INTO Junc_Order(Quantity) values(@Quantity)"; string insertString = "INSERT INTO Junc_Order(Quantity) values(@Quantity)"; SqlCommand cmd = new SqlCommand(insertString, conn); SqlCommand cmd = new SqlCommand(insertString, conn); cmd.Parameters.AddWithValue("@Quantity", r_Quantity); cmd.Parameters.AddWithValue("@Quantity", r_Quantity); cmd.ExecuteNonQuery(); cmd.ExecuteNonQuery();

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 当主键列不是标识列时,如何插入数据? - How do I insert data when the primary key column is not an identity column? 如何在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的主键标识列中? - How to insert value into primary key identity column in SQL through C#? 在SQL Server中插入新行时,如何将主键值设置为表中的其他col - How can I set the primary key value to other col in table when I insert a new row in SQL Server 如何将一个具有公共ID(主键)的用户的多行插入SQL Server数据库? - How to insert multiple rows of one user with common ID (primary key) into SQL Server database? “当IDENTITY_INSERT设置为OFF”时,无法使用复合键为表中的标识列插入显式值 - “Cannot insert explicit value for identity column in table when IDENTITY_INSERT is set to OFF” with composite key 循环列数据插入 SQL Server 数据库 - Looping column data insert into SQL Server database 当已经实现了自动增量时,如果将identity_insert设置为OFF,则无法为标识列插入显式值 - Cannot insert explicit value for identity column when identity_insert is set to OFF when auto-increment is already implemented 如何将数据插入涉及C#中主键和外键的sql server关系数据库 - How to insert data to a sql server relational database that involve primary and foreign keys in C# 实体添加/插入列等于身份主键值 - Entity Add/Insert with column equal to identity primary key value
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM