MySqlConnection ExecuteNonQuery UPDATE错误

时间:2016-08-22 01:46:21

标签: mysql

从OleDB更改为MySqlConnect后,我遇到了ExecuteNonQuery在UPDATE sql中抛出错误的问题 - 现有代码使用提交的OleDB行更新了访问数据库:

Public Function InsertClassRecord(ByVal UserName As String, ByVal Instructor As String, _
                                  ByVal DateCompleted As Date, ByVal Completed As Boolean, _
                                  ByVal Enrolled As Boolean, ByVal ClassName As String, _
                                  ByVal ClassDate As Date, ByVal WaitListed As Boolean, _
                                  ByVal Walkin As Boolean) As Object

    Dim connStr As String = "Datasource=localhost;Database=MYSQL_Training;uid=username;pwd=password;"

    'Dim conn As New Data.OleDb.OleDbConnection(connStr)
    Dim conn As New MySqlConnection(connStr)

    conn.ConnectionString = connStr
    conn.Open()

    Dim sql As String = "INSERT INTO EnrollmentsTbl (" & _
    "[UserName],[SubmitTime],[ClassTime],[ClassDate],[Enrolled],[ClassName],[WaitListed]," & _
    "[Instructor],[DateCompleted],[Completed],[Walkin]) VALUES " & _
    "(@UserName, @SubmitTime, @ClassTime, @ClassDate, @Enrolled, @ClassName, @WaitListed, " & _
    "@Instructor, @DateCompleted, @Completed, @Walkin) "

    'Dim comm As New Data.OleDb.OleDbCommand(sql, conn)
    Dim comm As New MySqlCommand(sql, conn)

    comm.Parameters.AddWithValue("@UserName", UserName)
    comm.Parameters.AddWithValue("@SubmitTime", DateTime.Now.ToString())
    comm.Parameters.AddWithValue("@ClassTime", "0800")
    comm.Parameters.AddWithValue("@ClassDate", ClassDate)
    comm.Parameters.AddWithValue("@Enrolled", Enrolled)
    comm.Parameters.AddWithValue("@ClassName", ClassName)
    comm.Parameters.AddWithValue("@WaitListed", WaitListed)
    comm.Parameters.AddWithValue("@Instructor", Instructor)
    comm.Parameters.AddWithValue("@DateCompleted", DateCompleted)
    comm.Parameters.AddWithValue("@Completed", Completed)
    comm.Parameters.AddWithValue("@Walkin", Walkin)

    Dim result As Integer = comm.ExecuteNonQuery()
    conn.Close()
    Return True
End Function

以下是UPDATE错误: https://discussions.apple.com/thread/6602475?tstart=0

1 个答案:

答案 0 :(得分:1)

看起来你拿了一些适用于SQL Server的代码,并试图使它适用于MySQL。

但是,您无法在MySQL中使用方括号括起标识符名称。如果确实需要,请使用返回标记(`UserName`)或双引号("UserName")。

或者更好的是,如果你的专栏名称没有空格或特殊字符,就像你的情况一样,那么只有列名没有别的:

... UserName, SubmitTime, ClassTime, ...