I am trying to generate SQL using C# but, it failed to execute on server. There are html inputs in string which causing syntax error.
I have tried to replace single quote(') with twice ('') but it also failed for few inputs like (\\\\'').
Error example:
Actual Input String
'It\'s compact design with stable signal'
Modified SQL Input String
'It\''s compact design with stable signal'
Erroneous SQL
UPDATE products SET LongDescription = 'It\''s compact design with stable signal';
private static void makeSQLThread(List<Product> products, ref List<string> sqlList)
{
foreach (var item in products)
{
var colorId = item.ColorId;
var description = item.Description.Replace("'", "''"); // html input
var longDescription = item.LongDescription.Replace("'", "''"); // long html input
var isAvailable = item.isAvailable ? 1 : 0;
var formatSQl = string.Format("UPDATE products SET ColorId = {0}, Description = '{1}', LongDescription = '{2}', isAvailable = {3} WHERE Id = {4};",
colorId, description, longDescription, isAvailable, item.Id);
Console.WriteLine(formatSQl);
sqlList.Add(formatSQl);
}
}
I am saving all these queries in text file and executing on server. How can I do it in safe and better way. I can't use C# commands like SQLCommand etc right now.
Thanks