2017-07-27 170 views
2

我在網上搜索了很多次關於我的問題後,詢問我的問題。在C#中使用IBM Informix ODBC驅動程序查詢問題

對於我的工作,我需要使用IBM Informix ODBC Driver(v 3.70)在Informix數據庫的C#中進行查詢。 當我想要提取字符Ø(直徑)時,引擎的數據庫返回以下消息「錯誤[HY000]代碼集轉換輸入中的無效字節」。

我認爲DB_LOCALE或CLIENT_LOCALE不匹配,但我不確定。

語言環境設置: - DB_LOCALE:en_US.1252 - LIENT_LOCALE:en_US.1252

感謝的提前對您有所幫助。

回答

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 

從C#

使用的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> 

真的現在

----- 
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 

兩個characteres返回,因爲他們應該:

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

D:\Infx\work\cs> 

也許在表中的數據是不是真的從1252做一個卸載或CLIENT_LOCALE一個的dbexport = DB_LOCALE(所以沒有GLS轉換完成),如果不是,則檢查Ø是0xd8還是0xF8(上/下),這意味着'Ø'沒有使用正確的語言環境插入。

編輯:

如果您在表中有一個0x9D,你可能會使用850,而不是1252爲您的客戶端代碼集。 在850(在一些窗口是默認的編碼集一個cmd)「O」是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> 
+0

感謝您的回覆。這個命令oncheck -pp enus1252:t1 256非常有幫助。問題不是人物Ø,而是他之前的空白。在表中,我有十六進制代碼0xD8和0xF8,還有我的語言環境中未知字符的0x9D。我刪除了它們,現在對我來說一切正常! –

+0

好你有它的工作。通常,開發人員在設置Informix數據庫時只關心DB_LOCALE,但CLIENT_LOCALE也非常重要。如果你弄錯了,將來可能會咬你;) –