2

创建了一个将数据从 Microsoft SQL Server 移动到 MySQL 的 vb.net 应用程序。到目前为止,我所有的插入功能都有效,但以下除外。看来问题出在文本中带有逗号的字段并导致问题。

我认为问题出在“类别”列上。它的值类似于 '11|0,1|0,12|0'。

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

错误:Sql 错误:MySql.Data.MySqlClient.MySqlException:您的 SQL 语法有错误;检查与您的 MySQL 服务器版本相对应的手册,以在第 1 行的 'Separator,C_ID,EntryDt,S_ID) VALUES (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
4

1 回答 1

2

SEPARATOR是一个保留关键字,你应该用反引号转义它,

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

另一个忠告是不要使用保留关键字列表中的任何名称

于 2013-02-08T15:18:08.627 回答