我希望有人修改以下代码以保存数据,以自动为每条记录生成主键。 PS。在数据库中,ID列是主键,Is Identity设置为Yes。
尝试
cmd = New SqlCommand("Insert into StudentDetails.programmes(ID,Programme,Form,AcademicYear,Class) values(@ID,@Programme,@Form,@AcademicYear,@Class)", cn)
cmd.Parameters.AddWithValue("@Programme", txtProgramme.Text)
cmd.Parameters.AddWithValue("@Form", txtForm.Text)
cmd.Parameters.AddWithValue("@AcademicYear", txtAcademicYear.Text)
cmd.Parameters.AddWithValue("@Class", txtClass.Text)
cmd.ExecuteNonQuery()
MessageBox.Show("Record successfully saved", "Saved", MessageBoxButtons.OK, MessageBoxIcon.Information)
答案 0 :(得分:3)
只需从查询中删除ID即可。如果没有给出,它将自动生成。
cmd = New SqlCommand("Insert into StudentDetails.programmes(Programme,Form,AcademicYear,Class) values(@Programme,@Form,@AcademicYear,@Class)", cn)