代码之家  ›  专栏  ›  技术社区  ›  taiko

通过C连接到Informix#

  •  1
  • taiko  · 技术社区  · 6 年前

    我需要连接到informix,但看似简单的任务却变得有点烦人。 我下载了客户端sdk并添加了对dll的引用。

    代码如下:

    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    using IBM.Data.Informix;
    
    namespace ConsoleApp3
    {
        class Program
        {
    
            public static IfxConnection conn { get; set; }
    
            public static IfxCommand cmd { get; set; }
    
            /// <summary>
            /// Opens a database connection
            /// </summary>
            /// <param name="host">Set the host of the database. Ex: 192.168.0.1</param>
            /// <param name="service">Set the service number (port). Ex: 1525</param>
            /// <param name="server">Set the server name. Ex: srv</param>
            /// <param name="database">Set the database name. Ex: InformixDB</param>
            /// <param name="userID">Set the userID. Ex: informix</param>
            /// <param name="password">Set the password. Ex: P@ssw0rd</param>
            /// <returns></returns>
            public static void OpenConnection(string host, string service, string server, string database, string userID, string password)
            {
                string ConnectionString =
                    "Host = " + host + "; " +
                    "Service=" + service + "; " +
                    "Server=" + server + "; " +
                    "Database=" + database + "; " +
                    "User Id=" + userID + "; " +
                    "Password=" + password + "; ";
    
                try
                {
                    conn = new IfxConnection();
                    conn.ConnectionString = ConnectionString;
                    conn.Open();
                }
                catch (Exception e)
                {
                    var ex = new Exception(string.Format("{0} - {1}", e.Message, (e as Win32Exception).ErrorCode));
                    throw ex;
                }
            }
            static void Main(string[] args)
            {
                OpenConnection("", "", "", "", "", "");
    
                Console.ReadKey();
            }
        }
    }
    

    我知道我对openconnection方法没有任何争论。在这种情况下,这并不重要,因为它甚至在取消连接之前抛出异常。它在创建ifxconnection对象时引发异常。上面写着“无法加载dll‘iclit09b.dll’”。

    根据 IBM ,可能的原因是informixdir和path必须设置为环境变量。但他们不清楚到底要设置什么…

    如果您能帮忙,我将不胜感激。

    1 回复  |  直到 6 年前
        1
  •  3
  •   jsagrera    6 年前

    这个旧的技术说明提供了如何配置windows客户机的简单指南(包括path和informixdir环境变量的必需值)

    http://www-01.ibm.com/support/docview.wss?uid=swg21083599