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.