查询值和目标字段的数量不同。 vb.net出错

时间:2016-08-14 08:11:08

标签: vb.net visual-studio-2010

我正在使用Microsoft Visual Studio 2010 Express,而我正在尝试使用VB.NET创建注册表单。到目前为止,这是我的代码:

Private Sub btnSave_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSave.Click
    Dim cardStr As String
    Dim formStr As String
    Dim birthStr As String
    Dim goodmoralStr As String

    If cbcard.Checked Then
        cardStr = "OK"
    Else
        cardStr = ""
    End If

    If cbform.Checked Then
        formStr = "OK"
    Else
        formStr = ""
    End If

    If cbbirth.Checked Then
        birthStr = "OK"
    Else
        birthStr = ""
    End If

    If cbgoodmoral.Checked Then
        goodmoralStr = "OK"
    Else
        goodmoralStr = ""
    End If

    Dim cmd As New OleDbCommand
    Dim conn As New OleDbConnection(conStr)
    conn.Open()
    cmd.Connection = conn
    cmd.CommandType = CommandType.Text
    cmd.CommandText = "insert into tblEnroll(StudID,StudLevel,StudFName,StudMName,StudLName,StudGender,StudBirthday,StudNationality,StudContact,StudPOB,StudCitizenship,StudReligion,MomName,MomContact,MomOccupation,DadName,DadContact,DadOccupation,PrevSchool,Card,F137,BirthCertificate) values ('" & txtID.Text & "','" & lbGrade.Text & "','" & txtFName.Text & "','" & txtMName.Text & "','" & txtLName.Text & "','" & lbGender.Text & "','" & dtpBirthDate.Text & "','" & txtNationality.Text & "','" & txtStudContact.Text & "','" & txtPOB.Text & "','" & txtCitizen.Text & "','" & txtReligion.Text & "','" & txtMom.Text & "','" & txtMomContact.Text & "','" & txtMomOccupation.Text & "','" & txtDad.Text & "','" & txtDadContact.Text & "','" & txtDadOccupation.Text & "','" & txtPrevSchool.Text & "','" & cardStr & "','" & formStr & "','" & birthStr & "','" & goodmoralStr & "')"
    cmd.ExecuteNonQuery()
    conn.Close()
    MessageBox.Show("Student Successfully Enrolled!")`

这里可能有什么解决方案?

1 个答案:

答案 0 :(得分:1)

INSERT语句值中有一列太多。值goodmoralStr没有要插入的相应列。

稍微说,您应该在代码中使用参数化SQL以避免SQL注入问题。