最近在工作中遇到了需要連接Informix數據庫的問題,在通過研究后發現了可以通過多種方式實現,我選擇的是通過IBM Informix .NET Provider。該方式需要引用IBM.Data.Informix.dll。
1 using IBM.Data.Informix; 3 using System; 7 8 namespace InformixLinkTest 9 { 10 class Program 11 { 12 static void Main(string[] args) 13 { 14 try 15 { 16 // Open a connection 17 IfxConnection conn = new IfxConnection( 18 "Host=127.0.0.1;Service=9098;" 19 + "Server=informixserver;Database=MyDatabase;" 20 + "User ID=informix;password=MyPassword;db_locale=en_us.819" 21 ); 22 conn.Open(); 23 IfxDataReader rd; 24 using (IfxCommand cmd = conn.CreateCommand()) 25 { 26 cmd.CommandText = "select * from simpletable"; 27 rd = cmd.ExecuteReader(); 28 rd.Read(); 29 do 30 { 31 if (rd.HasRows) 32 { 33 ///Assuming the table has two columns 34 Console.WriteLine("{0}", rd[0]); 35 } 36 37 } while (rd.Read()); 38 } 39 conn.Close(); 40 } 41 catch (IfxException e) 42 { 43 Console.WriteLine(e.ToString()); 44 Console.ReadLine(); 45 } 46 } 47 }
在調試過程中會發現出現異常(System.DllNotFoundException:“無法加載 DLL“IfxDotNetIntrinsicModule.dll”: 找不到指定的模塊。 (異常來自 HRESULT:0x8007007E)。”),具體如圖:,通過提示可以看到找不到IfxDotNetIntrinsicModule.dll,我通過在安裝Informix數據庫的路徑中找到了這個包放到了bin文件夾下后
,就能正常運行了。
另一個問題就是在連接串中最開始db_locale的值我是給的utf8,按照這個也會出現異常情況,異常代碼為(ERROR [HY000] [Informix .NET provider][Informix]Database locale information mismatch. ),截圖如下:,最后將其設置為db_locale=en_us.819后就可正常連接到informix數據庫。