从VB.Net应用程序更新/编辑记录到Access数据库的问题

时间:2012-12-13 16:19:13

标签: vb.net ms-access sql-update insert-update

我无法将更新从VB.Net应用程序更新到Access数据库。我收到以下错误:

“更新语句中的语法错误”或“条件表达式中的数据不匹配错误”

以下是以下代码:

公共功能save1stsemester()         '此代码将第一学期的记录更新为数据库

    Dim ds As New DataSet
    Dim dbSource As String
    Dim conn As New OleDb.OleDbConnection
    Dim dbcursor As Integer = 0
    Dim da As New OleDb.OleDbDataAdapter
    Dim cmdUpdate As New OleDb.OleDbCommand


    Dim msg1 As String
    Dim style As MsgBoxStyle
    Dim result As MsgBoxResult
    Dim title, remarks As String

    Dim totalgrade As Integer = CInt(txtTotalMK.Text)
    Dim totalload As Integer = CInt(txtTotalLoad.Text)

    Dim gpadecimal As Decimal
    gpadecimal = CDec(lblGPA1.Text)

    title = "Success"
    style = MsgBoxStyle.Information

    Call allremarks() ' this function gathers all the remarks to a single variable.
    remarks = allremarks()

    Dim failflag As Boolean
    If checkflag100() = True Then 'True means all courses were passed then turn on Flag
        failflag = True
    Else
        failflag = False
    End If


    If conn.State = ConnectionState.Open Then
        GoTo cont
    Else
        If conn.State = ConnectionState.Closed Then
            dbProvider = "PROVIDER=Microsoft.Jet.OLEDB.4.0;"
            dbSource = "Data Source =C:\CGPA\e-CGPA Stat\e-CGPA Stat\bin\Debug\Scoredata.mdb;Persist Security Info=False"
            conn.ConnectionString = dbProvider & dbSource
            conn.Open()

            da.SelectCommand = New OleDb.OleDbCommand("SELECT * FROM 100Level1stSemester WHERE MatNO like '%" + cmbMATNO.Text + "%'")
            da.SelectCommand.Connection = conn

            Dim mycomand As New OleDb.OleDbCommandBuilder(da)

            da.Fill(ds, "100Level1stSemester")

cont:       If cmbCourseLevel.SelectedItem = "100 Level" Then
                '  Try
                cmdUpdate.CommandText = "UPDATE 100Level1stSemester " & _
                "SET MatNo = '" & cmbMATNO.Text & "'" & _
                ", FName = '" & cmbStatFName.Text & "'" & _
                ", MName = '" & cmbStatInitial.Text & "'" & _
                ", SName = '" & cmbStatSname.Text & "'" & _
                ", STA110 = '" & txtGR1.Text & "'" & _
                ", MTH110 = '" & txtGR2.Text & "'" & _
                ", MTH112 = '" & txtGR3.Text & "'" & _
                ", ACC111 = '" & txtGR4.Text & "'" & _
                ", GST111 = '" & txtGR5.Text & "'" & _
                ", GST112 = '" & txtGR6.Text & "'" & _
                ", GST123 = '" & txtGR7.Text & "'" & _
                ", [STA110-SCORE] = '" & txtSC1.Text & "'" & _
                ", [MTH110-SCORE] = '" & txtSC2.Text & "'" & _
                ", [MTH112-SCORE] = '" & txtSC3.Text & "'" & _
                ", [ACC111-SCORE] = '" & txtSC4.Text & "'" & _
                ", [GST111-SCORE] = '" & txtSC5.Text & "'" & _
                ", [GST112-SCORE] = '" & txtSC6.Text & "'" & _
                ", [GST123-SCORE] = '" & txtSC7.Text & "'" & _
                ", [Tot-Grade-Point] = '" & totalgrade & "'" & _
                ", [Tot-Credit-Load] = '" & totalload & "'" & _
                ", [1stSemesterGPA] = '" & gpadecimal & "'" & _
                ", Remarks = '" & remarks & "'" & _
                ", Flag = '" & failflag & "'" & _
                " WHERE MatNo = '" & cmbMATNO.Text & "'"

                cmdUpdate.CommandType = Data.CommandType.Text
                cmdUpdate.Connection = conn
                cmdUpdate.ExecuteNonQuery()
                cmdUpdate.Dispose()

                conn.Close()

                msg1 = "100 Level 1st semester exam score and grades updated successfully."

                result = MsgBox(msg1, style, title)
                cmdVerifySem1.Enabled = False
                Grp1stSEM.Enabled = True

                'Catch
                MessageBox.Show("An error occured while updating the student's 100 Level scores! Duplicate entry was detected in the database. Ensure that the student's scores has not been entered before and try again.")
                'End Try
            End If
        End If
    End If
End Function

我被建议使用参数化查询,我不知道如何使用。

任何帮助将不胜感激

由于

2 个答案:

答案 0 :(得分:0)

当您尝试将数字列设置为非数字值或者尝试在具有不同数据类型的列上连接2个表时,通常会出现Data Mismatch错误。您在此处更新未加入,因此请检查构建的SQL语句并确保将有效值放入每列。鉴于Criteria的引用特别注意cmbMATNO.Text

的值

答案 1 :(得分:0)

由于您的编写方式,很难阅读您用于更新的命令文本。这是使用参数化查询的一个很好的理由,但不是最好的理由。以下是使用参数化查询的示例:

    Dim cmdText As String = "INSERT INTO Customer(Name, EmailAddress) VALUES (?,?)"
    Dim cmd As OleDbCommand = New OleDbCommand(cmdText, con)
    cmd.CommandType = CommandType.Text
    cmd.Parameters.Add("@p1", OleDbType.VarChar).Value = txtUserName.Text
    cmd.Parameters.Add("@p2", OleDbType.VarChar).Value = txtEmail.Text

基本上,你用?在字符串中表示您将用变量/文本框值替换的值。然后根据命令字符串中出现的顺序为每个参数添加一个参数。这使得读取命令字符串和验证正确的语法变得更加容易。它还可以保护您的数据库不被注入。