Click here to Skip to main content
15,444,203 members
Articles / Programming Languages / SQL
Posted 25 May 2008


19 bookmarked

Connection to DB2 from .NET

Rate me:
Please Sign up or sign in to vote.
2.71/5 (4 votes)
25 May 2008CPOL3 min read
How to connect to IBM DB2 from .NET on Windows


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.


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:

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.


The only thing that changes are some names ex.:




and so on.


DB2Connection, DB2Command, DB2Exception

are all located in


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


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;");

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".







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.


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


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

Written By
Database Developer
Poland Poland
I've been devoloping MSSQL databases since 2004. My favorite language is C#. Currently I've switched from MS Server to IBM DB2, but still do some work on MSS.

Comments and Discussions

QuestionDB2Connection' is a 'field' but is used like a 'type' Pin
captainronohio29-Nov-18 3:17
Membercaptainronohio29-Nov-18 3:17 
QuestionError connecting DB2 from C#.Net Pin
raj19749017-Nov-14 6:18
Memberraj19749017-Nov-14 6:18 
QuestionException saying ERROR [42968] [IBM] SQL8002N An attempt to connect to a host failed due to a missing DB2 Connect product or invalid license. SQLSTATE=42968 Pin
Member 799794426-Dec-12 20:12
MemberMember 799794426-Dec-12 20:12 
AnswerRe: Exception saying ERROR [42968] [IBM] SQL8002N An attempt to connect to a host failed due to a missing DB2 Connect product or invalid license. SQLSTATE=42968 Pin
Mohamed ElSheikh22-Sep-14 2:47
MemberMohamed ElSheikh22-Sep-14 2:47 
QuestionConnection on DB2 Express failed... why that???? Pin
micheWeb13-Aug-09 0:01
MembermicheWeb13-Aug-09 0:01 
AnswerRe: Connection on DB2 Express failed... why that???? Pin
micheWeb13-Aug-09 3:12
MembermicheWeb13-Aug-09 3:12 
GeneralMissing the point of the article Pin
Chris Maunder25-May-08 16:30
cofounderChris Maunder25-May-08 16:30 
GeneralRe: Missing the point of the article Pin
Vasudevan Deepak Kumar25-May-08 18:00
MemberVasudevan Deepak Kumar25-May-08 18:00 
GeneralRe: Missing the point of the article Pin
Lukas_J25-May-08 23:41
MemberLukas_J25-May-08 23:41 
GeneralRe: Missing the point of the article Pin
Lukas_J25-May-08 23:18
MemberLukas_J25-May-08 23:18 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.