我只是拥有数据库的初学者。这是我第一次在 VB 中使用数据库,但我的错误不会让我将数据插入表中。我希望是否有人可以纠正我的错误?
提前致谢!
Dim conn As New OleDb.OleDbConnection("PROVIDER=Microsoft.Jet.OLEDB.4.0;Data Source = Z:\VIAJE_Exercises\MachineExercise7\databaseEnrolled.accdb;Persist Security Info=True")
Dim insertsql As String
Try
insertsql = "INSERT INTO databaseEnrolled" & _
"(ID, year, studentID, firstName, middleInitial, lastName, age, gender, birthday, citizenship, religion, placeOfBirth, address, guardian, contact) " & _
" VALUES(@ID, @year, @studentID, @firstName, @middleInitial, @lastName, @age, @gender, @birthday, @citizenship, @religion, @placeOfBirth, @address, @guardian, @contact)"
Dim cmd As OleDb.OleDbCommand = New OleDb.OleDbCommand(insertsql, conn)
cmd.Parameters.AddWithValue("@year", year.Text)
cmd.Parameters.AddWithValue("@studentID", studentIdtxt.Text)
cmd.Parameters.AddWithValue("@firstName", firstNametxt.Text)
cmd.Parameters.AddWithValue("@middleInitial", middleTxt.Text)
cmd.Parameters.AddWithValue("@lastName", lastNametxt.Text)
cmd.Parameters.AddWithValue("@age", ageTxt.Text)
cmd.Parameters.AddWithValue("@gender", genderCom.Text)
cmd.Parameters.AddWithValue("@birthday", monthCom.Text + dayCom.Text + yearCom.Text)
cmd.Parameters.AddWithValue("@citizenship", citizenshipTxt.Text)
cmd.Parameters.AddWithValue("@religion", religionTxt.Text)
cmd.Parameters.AddWithValue("@placeOfBirth", placeOfBirthTxt.Text)
cmd.Parameters.AddWithValue("@address", addressTxt.Text)
cmd.Parameters.AddWithValue("@guardian", guardianTxt.Text)
cmd.Parameters.AddWithValue("@contact", contactTxt.Text)
conn.Open()
Dim rowsaffected As Integer = cmd.ExecuteNonQuery
MessageBox.Show(rowsaffected.ToString & "added")
conn.Close()
Catch ex As Exception
MsgBox(ex.Message)
End Try