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

发布于 2024-09-08 17:35:41 字数 1121 浏览 15 评论 0原文

在 VS 2005 C# 中,将欧元 (€) 符号插入 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(); 

In VS 2005 C#, While inserting Euro (€) Symbol into Db2 table, It is storing values correctectly. DISABLEUNICODE=1 parameter helped to store this correctly.

But when we try to read from the table, it is giving junk char as below.

"Spec GS 1"

Same code is working in VB 6.0 ADO

Could you please help me.


Code:

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(); 

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

还不是爱你 2024-09-15 17:35:41

从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; 
}

After reading the text from DataSet, used the following encoding function to display correct data.

 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; 
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文