【发布时间】:2019-12-02 06:42:29
【问题描述】:
我正在用 C# 编写一个 .NET 软件,它应该将数据从一个数据库传输到另一个数据库。 每个索引大于 127codepage 1252 的字符都会在目标数据库中引起问题,因此我想在将这些字符从值(字符串)中删除之前,再将它们写入目标数据库。 p>
我一直在搜索和尝试很多,但直到现在我只找到了针对 ASCII 或 UTF 索引执行此操作的解决方案。我需要代码页 1252 索引的解决方案。
编辑:这是迄今为止我最接近的方法:
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;
}
}
【问题讨论】:
-
欢迎来到 SO。我建议先阅读the tour,然后阅读How to Ask 和Minimal, Complete, and Verifiable example,了解SO 的工作原理以及如何有效地提出问题。最重要的是,向我们展示您目前拥有的代码。
-
到目前为止你尝试过什么?你能给我们看一些代码吗?
-
到目前为止,我还没有此主题的工作代码。实际上我有一个方法
protected string GetSqlValue(string input),如果参数为空、空或仅包含空格,则返回“NULL”。如果不是这种情况,这些方法只返回到目前为止的参数。我在这里的要求是我想添加到这个方法中的东西,到目前为止我什至还没有接近解决方案。 -
好的,我已经添加了我认为迄今为止最接近的方法。