Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles / Languages / SQL

Connection to DB2 from .NET

2.71/5 (4 votes)
25 May 2008CPOL3 min read 1  
How to connect to IBM DB2 from .NET on Windows

Introduction

I would like to show how to connect to IBM DB2 Express from C# using IBM managed provider. This article is based on various parts from other articles (and experiences) I've came across searching for solution to my problem.

Background

A while ago i started to develop client-server aplication in c# which was connecting to DB2 express server. When i started to look for connection string that will work for me I came to realize that nothing I've found was warking. There are many very good articles on the net but somehow connection strings where not working for me. It took some time but finally I've found solution to my problem. I hope it will help You to.

Using the code

OBTAINING IBM Data Server Driver for

1. You need to have an account (it's free) created in order to download anything from IBM.

2. You can download everything from IBM site at this adress:

http://www-306.ibm.com/software/data/db2/express/download.html

Keep in mind that this driver requires .Net fremework to be already installed on your computer. You don't need any other things to make it work. It's very fast and pain free process.

You will need to install IBM .net provider and ofcourse you need DB2 sever. I'm not going to give you entire code becouse its not different at all from MS SERVER Express.

DIFFERENCES

The only thing that changes are some names ex.:

C#
SqlConnection

to

C#
DB2Connection

and so on.

The

C#
DB2Connection, DB2Command, DB2Exception

are all located in

IBM.Data.DB2.dll

You need to add a reference to your project in order to have accsess to it.

PREREQUIREMENTS

Before I could remotely connect I needed to create exception in windows firewall on the server. It's an easy process. Just add an exception with the name of your choice and set TCP port to 50000 (that's the default port for DB2 Express).

Now for the connection string:

DB2Connection connect = 
new DB2Connection("Database=SAMPLE;UserID=db2admin;Password=yourPass;Server=xxx.xxx.xxx.xxx:50000");

Database - sets the name of the database you are trying to connect to.

UserID - it's the user id you are using "db2admin" is the default user name that is created when you install DB2 Express unless you have changed it during instalation.

Password - it's the password you specified durring instalation for the user.

Server - this is where you specify location/name of the server and optionaly tcp port.

This last parameter gave me a headache. There are different ways to use it but in my case only one worked.

Here are your options:

Lets assume our server name is "Elizabeth".

Opt.1

...Server=Elizabeth;

Opt.2

...Server=Elizabeth:50000;

Opt.3

...Server=XXX.XXX.XXX.XXX:50000

In first two cases I got errors "DNS could not be resolved". Only last option worked. Thats it I think. As you can see the stracture of the connection part of the code in application is almost the same as MS Server version.

Points of Interest

Hope this helps someone who has problems with reading those long error msgs DB2 pops when something goes wrong like I have =].

In the near future I'll add some info on how to write queries and stored procedures for IBM and how to use them with C#. Also I'll try to show the differences between MS sp and DB2 sp.

History

2008-05-25 First version of this article.

2008-05-26 Added info on how to obtain driver and where classes mentioned above are located

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)