简体   繁体   中英

Converting string from MSSQL to MySQL

I have an app that is reading from a SQL DB and writing to MySQL. This app is used to migrate data for a client. While testing I'm encountering an issue with a single record and I'm stumped. I've tested various suggestions from this site in terms of using the C# Encoding class and have tried to convert it each time with no success on this single record.

The SQL Collation is SQL_Latin1_General_CP1_CI_AS the MySQL collation is latin1_general_ci a sample of the code (in a foreach loop) is below:

foreach(var pgObj in PgObjList)
{
    //byte[] bytes = Encoding.Default.GetBytes(pgObj.Description);
    //pgObj.Description = Encoding.ASCII.GetString(bytes);

    byte[] utf8Bytes = Encoding.UTF8.GetBytes(pgObj.Description);
    byte[] isoBytes = Encoding.Convert(Encoding.ASCII, Encoding.UTF8, utf8Bytes);
    string uf8converted = Encoding.UTF8.GetString(isoBytes);

    string insertSQL = @"INSERT INTO `mainsite`.`sf_root_items` 
                        (`ID`, 
                        `LoweredName`, 
                        `MenuName`, 
                        `Title`, 
                        `Description`, 
                        `PageType`, 
                        `ExternalUrl`
                        )
                        VALUES
                        ('" + pgObj.ID + @"', 
                        '" + pgObj.LoweredName + @"', 
                        '" + pgObj.MenuName + @"', 
                        '" + pgObj.Title + @"', 
                        '" + pgObj.Description + @"', 
                        '" + pgObj.PageType + @"', 
                        '" + pgObj.ExternalUrl + @"'
                        );";
    string outputMsg;
    // more code here to execute the MySQL statement
}

The best result I got was converting to ASCII (the commented code) and that stored the text in what looked like Chinese characters. There was no exception but when I run it any other way I get an exception with this record.

Exception:

MySQL Exception on record#28/r/n
MySql.Data.MySqlClient.MySqlException (0x80004005): 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 'il vous plaît (RSVP)', 
                                                '0', 
             ' at line 15
       at MySql.Data.MySqlClient.MySqlStream.ReadPacket()
       at MySql.Data.MySqlClient.NativeDriver.GetResult(Int32& affectedRow, Int64& insertedId)
       at MySql.Data.MySqlClient.Driver.GetResult(Int32 statementId, Int32& affectedRows, Int64& insertedId)
       at MySql.Data.MySqlClient.Driver.NextResult(Int32 statementId, Boolean force)
       at MySql.Data.MySqlClient.MySqlDataReader.NextResult()
       at MySql.Data.MySqlClient.MySqlCommand.ExecuteReader(CommandBehavior behavior)
       at MySql.Data.MySqlClient.MySqlCommand.ExecuteNonQuery()
       at SharepointImportDL.SharepointContentImport.GetSitefinityContent.addRootItems(List`1 PgObjList, String WPConnectionString) in C:\Users\Administrator\Source\Repos\TestBinaryWriter\Test Binary Writer\SharepointImportDL\SharepointContentImport\GetSitefinityContent.cs:line 986

Line 986 is the SQL execution: int output = cmd.ExecuteNonQuery();

It may also be worth noting that the string that I'm inserting is from a nvarchar column (MSSQL) and into a varchar (MySQL) column

Ironically, the string is a simple "Répondez s'il vous plaît (RSVP)" (I want to punch the pretentious person who had to use the French text) so it's not likely I'll encounter it again but clearly I need to handle this possibility.

Escape strings (eg, pgObj.Description ) before building the SQL ( string insertSQL ). Otherwise you get syntax error from things like

, 'Répondez s'il vous plaît (RSVP)', 

Notice how there is a single quote inside of single quotes? Also notice how the error pointed at the problem:

... syntax to use near 'il vous plaît (RSVP)', 

Thanks to Palle Due for suggesting parameterized queries, I decided to write a stored procedure in MySQL to handle these issues.

My C# code, based on my original question is below:

foreach(var pgObj in PgObjList)
{
    string SP = "sp_insert_root_items";
    string outputMsg = "";

    MySqlConnection myConnection = new MySqlConnection(WPConnectionString);
    MySqlCommand cmd;
    try
    {
        if (myConnection.State != ConnectionState.Open)
        {
            myConnection.Close();
            myConnection.Open();
        }
        cmd = myConnection.CreateCommand();
        cmd.CommandText = SP;
        cmd.CommandType = CommandType.StoredProcedure;

        cmd.Parameters.AddWithValue("@ID", pgObj.ID);
        cmd.Parameters["@ID"].Direction = ParameterDirection.Input;

        cmd.Parameters.AddWithValue("@LoweredName", pgObj.LoweredName);
        cmd.Parameters["@LoweredName"].Direction = ParameterDirection.Input;

        cmd.Parameters.AddWithValue("@MenuName", pgObj.MenuName);
        cmd.Parameters["@MenuName"].Direction = ParameterDirection.Input;

        cmd.Parameters.AddWithValue("@Title", pgObj.Title);
        cmd.Parameters["@Title"].Direction = ParameterDirection.Input;

        cmd.Parameters.AddWithValue("@Description", pgObj.Description);
        cmd.Parameters["@Description"].Direction = ParameterDirection.Input;

        cmd.Parameters.AddWithValue("@PageType", pgObj.PageType);
        cmd.Parameters["@PageType"].Direction = ParameterDirection.Input;

        cmd.Parameters.AddWithValue("@ExternalUrl", pgObj.ExternalUrl);
        cmd.Parameters["@ExternalUrl"].Direction = ParameterDirection.Input;

        cmd.Parameters.AddWithValue("@ParentID", "");
        cmd.Parameters["@ParentID"].Direction = ParameterDirection.Input;

        int output = cmd.ExecuteNonQuery();

        // a value greater than 0 means execution was successful
        if (output > 0)
        {
            counter++;
            //outputMsg = "Record #" + counter.ToString() + " created";

        }
        else
        {
            counter++;
            outputMsg = "There was an error inserting record #" + counter.ToString();
        }

        myConnection.Close();
    }
    catch (Exception ex)
    {
        outputMsg = "MySQL Exception on record#" + counter.ToString() + "/r/n" + ex.ToString();
    }

    returnMsg += outputMsg;

}

An example of the stored procedure is below - it was exported from SQLYog.

DELIMITER $$

/*!50003 CREATE DEFINER=`root`@`localhost` PROCEDURE `sp_insert_root_items`(
        IN ID VARCHAR(255), 
        IN LoweredName VARCHAR(255), 
        IN MenuName VARCHAR(255), 
        IN Title VARCHAR(255), 
        IN Description VARCHAR(500), 
        IN PageType VARCHAR(255), 
        IN ExternalUrl VARCHAR(255), 
        IN ParentID VARCHAR(255)
    )
BEGIN
    INSERT INTO `wp_sf_page_items`
        (`ID`, 
        `LoweredName`, 
        `MenuName`, 
        `Title`, 
        `Description`, 
        `PageType`, 
        `ExternalUrl`, 
        `ParentID`
    )
    VALUES(ID, LoweredName, MenuName, Title, Description, PageType, ExternalUrl, ParentID);  
END */$$
DELIMITER ;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM