【发布时间】:2017-07-06 08:16:27
【问题描述】:
System.Data.SqlClient.SqlException occurred HResult=0x80131904 Message=Cannot insert explicit value for identity column in table 'DriversDetailsTable' when IDENTITY_INSERT is set to OFF. Source=.Net SqlClient Data Provider StackTrace: <Cannot evaluate the exception stack trace>
当我运行我的代码时,我不断收到上述错误,不知道为什么。这是我的代码:
SqlConnection conn = new SqlConnection(@"Data Source = ComputerName\SQLEXPRESS;Initial Catalog = Drivers;Trusted_Connection=True;");
conn.Open();
SqlDataAdapter adapter = new SqlDataAdapter();
SqlCommand command = new SqlCommand($@"INSERT INTO DriversDetailsTable (name, Id, DateJoined) VALUES ('Driver4', 1, '2017-07-06')", conn);
command.ExecuteNonQuery();
conn.Close();
【问题讨论】:
-
你的 SQL 是问题所在。开头的第 4 列是自动递增的吗?