字符集的问题
AX中的字符型字段用的是NVchar类型,编码是Unicode,这是通用的编码方式,支持多国语言字符的数据。但是如果要将AX里的数据导出到不支持Unicode的系统中就有些麻烦了,用SSIS的简体中文字符集导出时如果遇到简体中文字符集不支持的字符它就会报错了。用如下代码可以找到简体中文不支持的字符。
代码
SqlCommand sqlCommand = new SqlCommand("select nameAlias from InventTable", sqlConnection);
SqlDataReader dataReader = sqlCommand.ExecuteReader();
while (dataReader.Read())
{
string nameAlias = dataReader.GetValue(0).ToString();
byte[] gb2312 = System.Text.Encoding.Convert(System.Text.Encoding.Unicode, System.Text.Encoding.GetEncoding("GB2312"), System.Text.Encoding.Unicode.GetBytes(nameAlias));
char[] gb2312Char = new char[System.Text.Encoding.GetEncoding("GB2312").GetCharCount(gb2312, 0, gb2312.Length)];
System.Text.Encoding.GetEncoding("GB2312").GetChars(gb2312, 0, gb2312.Length, gb2312Char,0);
string gb2312String = new string(gb2312Char);
if (nameAlias != gb2312String)
{
Console.WriteLine(nameAlias);
Console.WriteLine(gb2312String);
}
}
SqlDataReader dataReader = sqlCommand.ExecuteReader();
while (dataReader.Read())
{
string nameAlias = dataReader.GetValue(0).ToString();
byte[] gb2312 = System.Text.Encoding.Convert(System.Text.Encoding.Unicode, System.Text.Encoding.GetEncoding("GB2312"), System.Text.Encoding.Unicode.GetBytes(nameAlias));
char[] gb2312Char = new char[System.Text.Encoding.GetEncoding("GB2312").GetCharCount(gb2312, 0, gb2312.Length)];
System.Text.Encoding.GetEncoding("GB2312").GetChars(gb2312, 0, gb2312.Length, gb2312Char,0);
string gb2312String = new string(gb2312Char);
if (nameAlias != gb2312String)
{
Console.WriteLine(nameAlias);
Console.WriteLine(gb2312String);
}
}