I am writing a .NET-software in C# that should transfer data from one database to another. Every character with an index greater than 127 of codepage 1252 causes trouble in the target database, so I want to remove those characters from the values (strings), before I write them into the target database.
I have been searching and trying a lot, but until now I have only found solutions for doing that for ASCII or UTF indexes. I need a solution for indexes of codepage 1252.
Edit: here is my closest approach so far:
protected string GetSqlValue(string input, bool isStringValue = true)
{
if (string.IsNullOrWhiteSpace(input)) return "''";
else
{
//TODO: remove all characters with an index greater than 127 in codepage 1252.
Encoding targetEncoding = Encoding.GetEncoding(1252);
byte[] tmp = targetEncoding.GetBytes(input);
for (int i=0;i<tmp.Length;i++)
{
if (tmp[i] > 127) tmp = tmp.Where((source, index) => index != i).ToArray();
}
input = targetEncoding.GetString(tmp);
if (isStringValue) return "'" + input + "'";
else return input;
}
}