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>
我不斷收到上述錯誤當我運行我的代碼,不知道爲什麼。這裏是我的代碼:SQL INSERT命令而引發錯誤
SqlConnection conn = new SqlConnection(@"Data Source = ComputerName\SQLEXPRESS;Initial Catalog = Drivers;Trusted_Connection=True;");
conn.Open();
SqlDataAdapter adapter = new SqlDataAdapter();
SqlCommand command = new SqlCommand([email protected]"INSERT INTO DriversDetailsTable (name, Id, DateJoined) VALUES ('Driver4', 1, '2017-07-06')", conn);
command.ExecuteNonQuery();
conn.Close();
您的SQL是問題所在。你是否有第4列在開始意味着自動遞增? –