vb.net在字段中使用逗号将错误插入mysql

时间:2013-02-08 15:17:02

标签: mysql sql vb.net

创建了一个将数据从Microsoft SQL Server移动到MySQL的vb.net应用程序。到目前为止,除了以下内容之外,我的所有插入函似乎问题出现在文本中有逗号并导致问题的字段。

我认为问题出在“类别”栏目上。它的值如'11 | 0,1 | 0,12 | 0'。

是否需要以特殊方式处理逗号?

错误: Sql错误:MySql.Data.MySqlClient.MySqlException:您的SQL语法中有错误;查看与MySQL服务器版本对应的手册,以便在'Separator,C_ID,EntryDt,S_ID)附近使用正确的语法(第1行中的1,1455090,'11 | 0,1'

代码:

#Region " InsertTable_Activity "
    Function InsertTable_Activity(ByVal Activity_ID As Integer, _
        ByVal Categories As String, _
        ByVal Separator As String, _
        ByVal C_ID As Integer, _
        ByVal EntryDt As DateTime, _
        ByVal S_ID As Integer, _
         ByVal myCon As MySqlConnection) As String

        Dim sRetVal As String = ""
        Dim sSQL As String = "INSERT INTO CandidateInsertResumeDetailCategories_Activity (Activity_ID,Categories,Separator,C_ID,EntryDt,S_ID) VALUES (?Activity_ID, ?Categories, ?Separator, ?C_ID,?EntryDt,?S_ID)"
        Dim myCmd As MySqlCommand = New MySqlCommand(sSQL, myCon)
        myCmd.CommandType = Data.CommandType.Text
        myCmd.Parameters.Add(New MySqlParameter("?Activity_ID", MySqlDbType.Int32)).Value = Activity_ID
        myCmd.Parameters.Add(New MySqlParameter("?Categories", MySqlDbType.VarChar, 500)).Value = Categories
        myCmd.Parameters.Add(New MySqlParameter("?Separator", MySqlDbType.VarChar, 10)).Value = Separator
        myCmd.Parameters.Add(New MySqlParameter("?C_ID", MySqlDbType.Int32)).Value = C_ID
        myCmd.Parameters.Add(New MySqlParameter("?EntryDt", MySqlDbType.DateTime)).Value = EntryDt
        myCmd.Parameters.Add(New MySqlParameter("?S_ID", MySqlDbType.Int32)).Value = S_ID

        Try
            If myCon.State <> Data.ConnectionState.Open Then
                myCon.Open()
            End If

            myCmd.ExecuteNonQuery()
            myCmd.Dispose()

        Catch sqlEx As MySqlException
            sRetVal = "ERROR: Sql error: " & sqlEx.ToString & vbCrLf & " - SQL used: " & sSQL
        Catch ex As Exception
            sRetVal = "ERROR: Regular error: " & ex.ToString & vbCrLf & " - SQL used: " & sSQL
        Finally
        End Try

        Return sRetVal
    End Function
#End Region

1 个答案:

答案 0 :(得分:2)

SEPARATOR是一个保留关键字,您应该使用反引号将其转义,

INSERT INTO CandidateInsertResumeDetailCategories_Activity 
            (Activity_ID, Categories, `Separator`, C_ID, EntryDt, S_ID) 
VALUES .....

另一个建议词是不使用保留关键字列表中的任何名称