Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
612 views
in Technique[技术] by (71.8m points)

c# - How to connect to DB2?

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?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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())
                {
                }
            }
        }
    }
}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...