Free Information Technology Magazines and eBooks

Saturday, June 27, 2009

VB.NET: How to connect to IBM DB2 Database

VB.NET: How to connect to IBM DB2 DatabaseFor the past days, I already discussed some articles on how to connect to database server outside Microsoft platform such as Oracle and MySQL. On this entry, I will show you how to connect on another database platform called IBM DB2. DB2 is one of IBM's families of relational database management system (RDBMS) software products within IBM's broader Information Management Software line. Although there are different "editions" and "versions" of DB2, which run on devices ranging from handhelds to mainframes, most often DB2 refers to the DB2 Enterprise Server Edition, which runs on Unix (AIX), Windows, Linux and z/OS servers

To connect to a IBM DB2 Database, follow these steps.
1. First Install IBM Data Server Provider for .NET
2. Then add a reference to "IBM.Data.DB2.dll" by right-clicking your project on solution explorer then choose "Add Reference".
3. Include IBM.Data.DB2 namespace on our code as shown below


Imports IBM.Data.DB2


4. Then on your main function, add the code that connects to IBM DB2 server.


Sub Main()
Dim connString As String = "DATABASE=SAMPLE;"

'Create connection
Dim con As New DB2Connection(connString)

Try
con.ConnectionString = connString
con.Open()
Console.WriteLine("Connected to IBM DB2.")

Catch ex As OracleException
Console.WriteLine("Error: " & ex.ToString())
Finally
' Close Connection
con.Close()
Console.WriteLine("Connection Closed")

End Try

End Sub



For more VB.NET Coding Tutorials, subscribe now.

2 comments:

Anonymous said...

Could you show us how to connect to DB2 database from Server Explorer?

Anonymous said...

How about showing us how to connect using impersonation.