I am sorry to say, there is no solution in approach you expect.
For example, have these columns and values:
- column A, value
,A', - column B, value
,B',
If they are together in column list, you have ',A',',',B','.
Now, where is the boundary between first and second value? It is ambiguous.
You must take action when creating text fields for SQL. Either use SQL parameters or properly escape qoutes and other problematic characters there.
Consider showing the above ambiguous example to managers, pushing the whole problem back as algorithmically unsolvable at your end. Or offer implementing a guess-work and ask them whether they will be happy if content of several text fields can get mixed in some cases like above one.
At time of SQL query creation, if they do not want to start using SQL parameters, the solution for enquoting any input string is as simple as replacing:
string Enquote(string input) { return input.All(c => Strings.AscW(c) < 128) ? "'" : "N'" + input.Replace("'", "''") + "'" }
Of course, it can have problem with deliberately malformed Unicode strings (surrogate pairs to hide ') but it is not normally possible to produce these strings through the user interface. Generally this can be still faster than converting all queries to versions with SQL parameters.
myValue.Replace("'", "''")will work (double up single quotes to escape them). Obviously this then needs to be inserted into the query separately, or the outer quotes will get converted too...Regex.