【问题标题】:Queries problems using IBM Informix ODBC Driver in C#在 C# 中使用 IBM Informix ODBC Driver 查询问题
【发布时间】:2017-07-27 06:51:27
【问题描述】:

在网上多次搜索我的问题后,我提出了我的问题。

对于我的工作,我需要使用 IBM Informix ODBC Driver (v 3.70) 在 Informix 数据库上使用 C# 进行查询。 当我要提取字符 Ø(直径)时,引擎的数据库返回以下消息“ERROR [HY000] Invalid byte in codeset conversion input”。

我认为 DB_LOCALE 或 CLIENT_LOCALE 不匹配,但我不确定。

语言环境设置: - DB_LOCALE:en_US.1252 - LIENT_LOCALE:en_US.1252

提前感谢您的帮助。

【问题讨论】:

    标签: c# informix


    【解决方案1】:

    仔细检查数据库是否为 1252,以及表中的内容。 也许数据库中特定字符的代码在 CP1252 中不是真正有效的 1252中,O-斜线对应:

    Ø 0xd8 Latin Capital Letter O With Stroke
    ø 0xf8 Latin Small Letter O with Stroke
    

    使用 1252 数据库进行快速测试:

    D:\infx\ids12>set DB_LOCALE=en_US.1252
    D:\infx\ids12>set CLIENT_LOCALE=en_US.1252
    D:\infx\ids12>dbaccess enus1252 -
    Database selected.
    > drop table t1;
    Table dropped.
    > create table t1(c1 char(10));
    Table created.
    > load from o.txt insert into t1;
    1 row(s) loaded.
    >
    Database closed.
    
    D:\infx\ids12>od -x o.txt
    0000000000      F8D8
    0000000002
    

    使用 oncheck 查看页面中的真实内容

    D:\infx\ids12>oncheck -pp enus1252:t1 256
    addr             stamp    chksum nslots flag type         frptr frcnt next     p
    rev
    1:16444          725726638 ded2   1      1    DATA         34    4054  0
    0
            slot ptr   len   flg
            1    24    10    0
    slot   1:
        0: d8 f8 20 20 20 20 20 20 20 20                     Xx        ......
    
    D:\infx\ids12>
    

    现在来自 C#

    -----
    D:\Infx\work\cs>cat s.cs
    using System;
    using System.IO;
    using System.Data;
    using System.Text;
    using IBM.Data.Informix;
    using System.Windows.Forms;
    
    class sample {
        static void Main(string[] args) {
    
    try
    {
         using (IfxConnection conn = new IfxConnection("Server=ids1210;Database=enus1252;uid=informix;pwd=ximrofni;DB_LOCALE=en_US.1252"))
         {
              conn.Open();
              using (IfxCommand cmd = conn.CreateCommand())
              {
                  cmd.CommandText = "select * from t1";
                  IfxDataReader rd = cmd.ExecuteReader();
                  rd.Read();
                  do
                  {
                       if (rd.HasRows)
                            Console.WriteLine("c1= {0}", rd[0]);
                  } while (rd.Read());
              }
          }
      }
      catch (IfxException exc)
      {
           Console.WriteLine("Update: {0}", exc.Message);
           foreach (IfxError error in exc.Errors)
               Console.WriteLine("Error: ({1}): {0}", error.Message,  error.NativeError);
      }
    }
    }
    
    D:\Infx\work\cs>csc.exe /R:D:\infx\csdk410tc8w2\bin\netf20\IBM.Data.Informix.dll /nologo s.cs /platform:x86
    

    两个字符都按原样返回:

    D:\Infx\work\cs>s
    c1= Øø
    
    D:\Infx\work\cs>
    

    也许表中的数据并不是真正来自 1252。使用 CLIENT_LOCALE=DB_LOCALE 执行卸载或 dbexport(因此没有完成 GLS 转换)并检查 Ø 是 0xd8 还是 0xF8(上/下)如果是不是,这意味着没有使用正确的语言环境插入“Ø”。

    编辑:

    如果表中有 0x9D,则您可能使用 850 而不是 1252 作为客户端代码集。 在 850 中(在某些窗口中是 cmd 的默认代码集)'Ø' 是 0x9D 而不是 0xD8

    D:\Infx>chcp 1252
    Active code page: 1252
    
    D:\Infx>echo Ø | od -x
    0000000000      20D8    0A0D
    0000000004
    
    D:\Infx>chcp 850
    Active code page: 850
    
    D:\Infx>echo Ø | od -x
    0000000000      209D    0A0D
    0000000004
    
    D:\Infx>    
    

    如果你在表格中有这个:

    D:\infx\ids12>dbaccess enus1252 -
    Database selected.
    > truncate t1;
    Table truncated.
    > insert into t1 values ('Ø');
    1 row(s) inserted.
    >
    Database closed.
    D:\infx\ids12>oncheck -pp enus1252:t1 256
    addr             stamp    chksum nslots flag type         frptr frcnt next     p
    rev
    1:16444          725727918 d1d2   1      1    DATA         34    4054  0
    0
        slot ptr   len   flg
        1    24    10    0
    slot   1:
    0: 9d 20 20 20 20 20 20 20 20 20                     .         ......
    
    D:\infx\ids12>
    

    C# 会给你一个错误,因为从 0x9D 转换(0x9D 不应该在 1252 中使用)

    D:\Infx\work\cs>s
    Update: ERROR [HY000] [Informix .NET provider]Invalid byte in codeset conversion input.
    Error: (21000): [Informix .NET provider]Invalid byte in codeset conversion input.
    
    D:\Infx\work\cs>
    

    【讨论】:

    • 感谢您的回复。这个命令 oncheck -pp enus1252:t1 256 非常有帮助。问题不在于角色Ø,而是他面前的一个空白。在表中,我有十六进制代码 0xD8 和 0xF8 以及 0x9D,它们在我的语言环境中是一个未知字符。我删除了它们,现在一切正常!
    • 很好,你成功了。通常开发人员在设置 Informix 数据库时只关心 DB_LOCALE,但 CLIENT_LOCALE 也相当重要。如果你弄错了,它可能会在未来咬你;)
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-04-19
    • 1970-01-01
    • 2023-03-31
    • 1970-01-01
    相关资源
    最近更新 更多