How to connect to DB2?

2020-02-14 09:07发布

问题:

I have to connect to a DB2 database installed on some other system. I have the machine name of the server, the database name to which I would like to connect to, the port number and the credentials. I do not have any client installed on my system for DB2. I want to use OLEDB connections.

Can I achieve this without installing a client? Also let me know what reference dlls would help me in achieving this i.e. what should I use - IBM OLE DB Provider for DB2 or Microsoft OLEDB provider for IBM DB2 or some other? Where do I find them?

回答1:

For OLEDB refer .NET DB2 OLEDB pre-requisites

Also refer: what is the difference between OLE DB and ODBC data sources?

For ODBC connection, I use the following

Connection String

  <add name="DB2ConnectionString_XA"
    connectionString="Driver={IBM DB2 ODBC DRIVER};Database=MyDB;Hostname=DB2GWXX;Protocol=TCPIP;Port=3700;Uid=ffghxa;Pwd=xxxx;"/>

Code:

using (OdbcConnection odbcConnection = new OdbcConnection(db2ConnectionString))
{
    odbcConnection.Open();

    // string commandText = "";

    using (OdbcCommand command = new OdbcCommand(commandText, odbcConnection))
    {
        command.CommandType = System.Data.CommandType.Text;
        using (OdbcDataReader reader = command.ExecuteReader())
        {
            if (reader.HasRows)
            {
                while (reader.Read())
                {
                }
            }
        }
    }
}


回答2:

I have previously used ODBC, not OLEDB but I'll share the link anyways if you decide to change your mind about ODBC. You will need to, as a minimum, install the Db2 clients and drivers. This installs the IMB DB" odbc driver into your system (odbc data sources). The drivers is found from this site: http://www-01.ibm.com/support/docview.wss?uid=swg27016878.

After installing add references to the IBM.Data.DB2.dll



标签: c# ado.net db2