使用ODBC ADO.NET从Db2代码页1252读取数据 - 欧元(€)符号未正确恢复

时间:2010-07-05 20:03:17

标签: c# ado.net visual-studio-2005 codepages cp1252

在VS 2005 C#中,在将Euro(€)符号插入Db2表时,它正确地存储值。 DISABLEUNICODE = 1参数有助于正确存储。

但是当我们尝试从表中读取时,它会给出垃圾字符,如下所示。

“Spec GS 1”

相同的代码在VB 6.0 ADO中工作

你能帮我吗?


代码:

OdbcConnection con = new OdbcConnection();
OdbcDataAdapter dataAdapter = new OdbcDataAdapter("SELECT * FROM XXXX.XXXX" , con);
OdbcCommand odbcCommand =
    new OdbcCommand(
        "INSERT INTO   XXXX.XXXX( BREACH_TYPE,JURISDICTION,ACTIVE ,VERIFIED , UPDATED_TS , UPDATED_BY ,VERIFIED_TS ,VERIFIED_BY)VALUES ('Spec GS € 1','IRE','Y','Y',CURRENT TIMESTAMP,'XECCRT3',CURRENT TIMESTAMP,'XECCRT8')");

con.ConnectionString = "DSN=DSNT;UID=xxxxxx;PWD=xxxxxx;MODE=SHARE;DBALIAS=DSNT;DISABLEUNICODE=1;PATCH1=1024;LONGDATACOMPAT=1;LOBMAXCOLUMNSIZE=1048575;PATCH2=6;";
con.Open();

odbcCommand.Connection = con; 
odbcCommand.ExecuteNonQuery();

dataAdapter.SelectCommand.CommandTimeout = 0;

DataSet dataSet = new DataSet();
dataAdapter.Fill(dataSet);
DataTable result = dataSet.Tables[0];

string Text = result.Rows[0][0].ToString();

MessageBox.Show(Text = result.Rows[0][0].ToString());

con.Close(); 

1 个答案:

答案 0 :(得分:0)

从DataSet读取文本后,使用以下编码功能显示正确的数据。

 private static string ConvertToAsciiString(string str)
{            
    Encoding source = Encoding.Default ;
    Encoding dest = Encoding.Unicode ;

    // Convert the string into a byte[].
    byte[] sourceBytes = dest.GetBytes(str);

    // Perform the conversion from one encoding to the other.
    byte[] destBytes = Encoding.Convert(source, dest, sourceBytes);

    string asciiString = dest.GetString(destBytes);
    asciiString = asciiString.Replace("\0", string.Empty);


    return asciiString; 
}