Bug #3803 ByteFX.Data.MySqlClient: SQL syntax error
Submitted: 17 May 2004 17:49 Modified: 18 May 2004 20:39
Reporter: Chris Bottin Email Updates:
Status: Closed Impact on me:
None 
Category:Connector / NET Severity:S2 (Serious)
Version:0.7.6.15073 OS:Windows (Windows XP)
Assigned to: Reggie Burnett CPU Architecture:Any

[17 May 2004 17:49] Chris Bottin
Description:
When inserting or updating a string value with the character ’ into a database table using ByteFX.Data.MySqlClient in ASP.NET, An SQL syntax error is generated stating :

"You have an error in your SQL syntax. Check the manual that corresponds to your MySQL server version for the right syntax to use near '''' (...)"

How to repeat:
In ASP.NET (VB), create new page and paste the following code in the code-behind the page. Create a database table and add a column datatyp varchar or text or similar string types.
-------------------------------------------------------------------

Imports ByteFX.Data

Public Class TestBug

    Inherits System.Web.UI.Page

    Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

        Dim dbConn As MySqlClient.MySqlConnection = New MySqlClient.MySqlConnection(Config.DatabaseConnectionString)
        Dim dbComm As MySqlClient.MySqlCommand = New MySqlClient.MySqlCommand("UPDATE your_table_name SET your_column_text=@Text", dbConn)

        dbComm.CommandType = CommandType.Text

        Dim ParameterText As MySqlClient.MySqlParameter = New MySqlClient.MySqlParameter("@Text", MySqlClient.MySqlDbType.VarChar, 250)

        ParameterText.Value = "This is chris’s test"
        dbComm.Parameters.Add(ParameterText)

        dbConn.Open()

        dbComm.ExecuteNonQuery()

        dbConn.Close()

    End Sub

End Class

Suggested fix:

Parameters Value should be checked and replaced for such reserved characters like ’. In the meantime, I have to check my script before doing such database queries by the following:

Dim StringValue as String = "This is chris’s test"

ParameterText.Value  = StringValue.Replace("’", "’’")
[18 May 2004 20:39] Reggie Burnett
Thank you for your bug report. This issue has been committed to our
source repository of that product and will be incorporated into the
next release.

If necessary, you can access the source repository and build the latest
available version, including the bugfix, yourself. More information 
about accessing the source trees is available at
    http://www.mysql.com/doc/en/Installing_source_tree.html