In my C# code I read in all id’s of a table into a string array ids[i], then update a different column in the table WHERE user_id = id[i].
The problem occurs when the id being read(also passed back into the UPDATE) contains an apostrophe – ‘
while (rdr.Read()
{
ids[i] = rdr.GetValue(0).ToString().Trim();
ids[i].Replace("'", "''");
....
I have also tried ids[i].Replace("'", "\'");
and ids[i].Replace("'", "-"); but I’m sure this will throw off my WHERE as it will look for user_id “O-Test” as opposed to “O’Test”.
My UPDATE SQL is along the lines of:
UPDATE [User]
SET first_name = '{1}'
WHERE [user_id] = '{2}'
Any ideas?
Thanks.
you need to assign it back to
ids[i]ids[i].Replace("'", "''");this will only create a string in memory and discard it, you need to assign it back toids[i]in your while loop.So your code should be: