Click here to Skip to main content
Click here to Skip to main content

Connect to DB2 from Microsoft .NET

By , 28 Aug 2003
 

Introduction

Most of the retail applications are still running in legacy systems like AS/400, Mainframe etc. If you are planning to migrate the entire application (or any modules) to Microsoft .NET, migration of the existing data will be the bottleneck (since there can be terra bytes of data, synchronization issues, data type mismatch etc). So your final choice will be to use the existing database. Let us consider different options to connect the Microsoft .NET and DB2 database resides in AS/400 system.

Managed providers

Managed providers works with in the boundaries of Common Language Runtime. There are no COM interoperability, obviously no worry about serialization and marshaling. Managed providers expose provider specific behavior through methods, properties and interfaces, which are much efficient than OLEDB providers. Hence the final result will be better performance. Currently there are matured managed providers for SQL Server and Oracle database (SqlClient and OracleClient). We are still waiting for System.Data.Db2Client namespace. So current option is to use other third party managed providers available in market. Some of them are still under development.

IBM DB2 UDB 8.1.2

This version provides a managed data provider for DB2 that is developed and supported by IBM. No third party drivers or bridge solutions are needed. A collection of add-ins for the Visual Studio .NET IDE that simplify creation of the applications that use ADO.NET API. These add-ins also extend the power of the Visual Studio .NET to developing DB2 server-side objects such as stored procedures and user-defined functions. Using VS IDE integration we will be able to visually explore the various DB2 resources, similar to SQL server. The .NET support is also shipped with DB2 Connect 8.1.2, providing support for DB2 UDB for z/OS and OS/390 V7. Here is the code snippet using this provider.

DB2Connection cn = new DB2Connection(
  "Database=TEST;UserID=db2admin; Password=password;Server=IS500");
DB2Command cmd = new DB2Command("SELECT COUNT(*) FROM EMP01", cn);
int count = Convert.ToInt32(cmd.ExecuteScalar());

Microsoft Host Integration Server 2004

One of the main features of the Host Integration Server 2004 is the managed provider for legacy database, which obviously include the DB2 provider. The beta version is currently available. We can programmatically invoke legacy Customer Information Control System (CICS) and Information Management System (IMS) programs, which are essential to mainframe transaction processing. But Visual Studio .NET IDE integration is not available.

DataDirect Connect for .NET, Edition 2.0

DataDirect’s "DB2 Wire Protocol" provider can connect to DB2 UDB for z/OS and OS/390 v7.1 and DB2 UDB for AS/400 V4R5, V5R1, and V5R2. At present this will not support distributed transactions.

Mono.Data.DB2Client

Mono project is also creating IBM DB2 Data Provider. It requires the Call Level Interface to IBM DB2 shared library. This is db2cli.dll on Windows. The IBM DB2 CLI API is very similar to the ODBC API. This provider is not fully functional. Compiles on Windows and Linux.

Unmanaged providers

As mentioned in the previous diagram, we have two more options to connect to DB2 database, using ODBC and OLEDB providers. Obviously there will be an extra layer and interoperability that will definitely affect the performance. There are different unmanaged providers are available. Let us consider a few of them.

The OLE DB.NET Data provider to access the database on an iSeries server can use the iSeries OLE DB provider. To take advantage of the .NET support, iSeries Access V5R2 with ServicePack 2 or higher is needed on the Windows system. For maximum stability, you should also install the latest database group PTF (SF99501 for V5R1 or SF99502 for V5R2) on the iSeries server.

OleDbConnection cn = new OleDbConnection(
  "Provider=IBMDA400.1;Data Source=TEST;User ID=db2user;" + 
  "Password=db2user;Default Collection =SAMPLEDB"); 

Similarly, if we are using the ODBC driver, then we have to set the DSN name in the connection string. Specifically, the DSN property is used to point to the target iSeries data source. The DSN must be previously registered with the ODBC Administration utility.

OdbcConnection cn = new OdbcConnection(
  "DSN=TestODBC; UID=db2user; PWD=db2user;DBQ=SAMPLEDB");

If you are using Microsoft OLEDB provider, then connection string will be as follows.

TCP/IP

"Provider=DB2OLEDB;Network Transport Library=TCPIP;Network Address=XXX.XXX.XXX.XXX;Initial Catalog=MyCtlg;Package Collection =MyPkgCol;Default Schema=Schema;User ID=MyUser;Password=MyPW"

APPC

"Provider=DB2OLEDB;APPC Local LU Alias=MyAlias;APPC Remote LU Alias =MyRemote;Initial Catalog=MyCtlg;Package Collection=MyPkgCol;Default Schema=Schema;UserID=MyUser;Password=MyPW"

Conclusion

The final selection depends on so many factors (support from vender, cost, scalability, stability etc). Hope you can make the right selection by keeping performance in mind. :)

References

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here

About the Author

Sreejumon
Web Developer
India India
Member
Sreejumon, Microsoft MVP for last 5 years.
 
He blogs at http://blog.sreesharp.com
 
He maintains the following sites.
 
http://www.industrial-automation-software.com
http://forum.t-mug.org
http://www.t-mug.org
http://www.sreesharp.com
 

Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
You must Sign In to use this message board.
Search this forum  
    Spacing  Noise  Layout  Per page   
QuestionDB2 on Mainframememberkleinkauff16 Apr '13 - 10:34 
Hello guys.
I am new to mainframe.
I need to connect, from .NET application to DB2 z/os mainframe.
I already can connect to a DB2 hosted on Windows, using DB2Connection
 
My question is, to connect to a DB2 on mainframe, i will use the same method? The connection string for mainframe, is different from what i use on Windows?
 
Thanks in advance!
Questionbest practices for exception handlingmemberdeostroll1 Jul '11 - 6:45 
Can you direct me to any exception handling (recommended practices) when it comes to trapping as400 errors?
Generalfrench char modmonomemberAjay Kale New31 Aug '10 - 3:11 
Hi,
 
I am using asp.net application installed on Unix under APache/Mono server.
I am using a functionality to enter name of FRENCH person and store it in databse.
e.g. Sebastián Pani - okk.
 
But while seeing the logs and databse values after stroring , I can see it as Sebasti?!n Pani.
Means the french chars are being missed out. The same is working fine on IIS, but not on MONO.
 
I also checked the settings in web.config, and also fr_FR.utf8 fonts are present in locale.
 
So am I missing out something to set ??
 
Please reply.
 
- Ajay K
QuestionMainframe check Error: Catastrophic failure when connection Mainframe DB2 shadow using Neonmemberilan0522 Jan '10 - 10:12 
we migrating IIS 5.0 to IIS 6.0
 
From dot net we connect mainframe using ODBC. I am getting error after migration
 
"Mainframe check Error: Catastrophic failure"
 


 
Can any one help me on this?
 
Thank You
 
Ilan
QuestionIBM.Data.DB2.DB2Exception: Unable to allocate statement handlememberbart.nicolotti11 Feb '09 - 2:11 
Hello,
 
I've installed mono, and xsp (not yet mod_mono) on ubuntu following the instruction here:
 
http://ubuntuforums.org/showthread.php?t=803743
 
in view of using mod_mono together with php on ubuntu server 8.04, to migrate a web service that access a db2/as400 database.
 
I've also installed db2exc from ubuntu repository as said here:
 
http://www.ubuntu.com/partners/ibm/db2
 
I can compile a test program that does a query to the db:
 
using System;
using System.Data;
using IBM.Data.DB2;
 

public class HelloWorldDb2{
	public static int Main( string[] args ){
		if( args.Length != 1 ){
			System.Console.Error.WriteLine( "You must tell me your name!" );
			//System.Environment.Exit(-1);
		}else{
			string name= args[0];
			System.Console.WriteLine( "Hello, {0}", name );
			DB2Connection conn = null;
			conn = new DB2Connection("Database=VIAGGIFAM;UserID=S_IA;Password=A__N;Server=192.168.0.200");
			conn.Open();
			DB2Command cmd = conn.CreateCommand();
			cmd.CommandText="SELECT * FROM VIAGGIFAM.PWKCXM";
			DB2DataReader reader = cmd.ExecuteReader();
			Console.WriteLine("\nExecuted:"+cmd.CommandText);
			int cols = reader.FieldCount;
			bool rows = reader.HasRows;
			Console.WriteLine("\nCols:"+cols+", rows:"+rows);
			int r=1;
			while( reader.Read() == true ){
				int i=0;
				
				Console.WriteLine(""+r++);
				for( i=1; i<= cols; i++ ){
					if( i>1 ){
						Console.Write( ",");	
					}
					Console.Write(reader.GetString(i));
				}
			}
			
			
		}
		return 0;
	}
}
siap@LxPC54Unsure | :~ /src/test$ gmcs -r:/usr/lib/mono/1.0/IBM.Data.DB2.dll -r:/usr/lib/mono/2.0/System.Data.dll helloDB2.cs
 
but when I execute it:
 
siap@LxPC54Unsure | :~ /src/test$ sudo MONO_LOG_LEVEL=debug mono helloDB2.exe bart
 
....
 
Mono-INFO: Assembly Ref addref System.Data 0x8362e10 -> System.Xml 0x83719d8: 2
 
Hello, bart
Mono-INFO: DllImport attempting to load: 'libdb2'.
Mono-INFO: DllImport loading location: 'libdb2.so'.
Mono-INFO: Searching for 'SQLAllocHandle'.
Mono-INFO: Probing 'SQLAllocHandle'.
Mono-INFO: Found as 'SQLAllocHandle'.
Mono-INFO: DllImport attempting to load: 'libdb2'.
Mono-INFO: DllImport loading location: 'libdb2.so'.
Mono-INFO: Searching for 'SQLAllocHandle'.
Mono-INFO: Probing 'SQLAllocHandle'.
Mono-INFO: Found as 'SQLAllocHandle'.
Bart
not useLibCli
Bart
not useLibCli
Mono-INFO: DllImport attempting to load: 'libdb2'.
Mono-INFO: DllImport loading location: 'libdb2.so'.
Mono-INFO: Searching for 'SQLDriverConnectW'.
Mono-INFO: Probing 'SQLDriverConnectWW'.
Mono-INFO: Probing 'SQLDriverConnectWW'.
Mono-INFO: Probing 'SQLDriverConnectW'.
Mono-INFO: Found as 'SQLDriverConnectW'.
Mono-INFO: DllImport attempting to load: 'libdb2'.
Mono-INFO: DllImport loading location: 'libdb2.so'.
Mono-INFO: Searching for 'SQLDriverConnectW'.
Mono-INFO: Probing 'SQLDriverConnectWW'.
Mono-INFO: Probing 'SQLDriverConnectWW'.
Mono-INFO: Probing 'SQLDriverConnectW'.
Mono-INFO: Found as 'SQLDriverConnectW'.
Mono-INFO: DllImport attempting to load: 'libdb2'.
Mono-INFO: DllImport loading location: 'libdb2.so'.
Mono-INFO: Searching for 'SQLGetInfoW'.
Mono-INFO: Probing 'SQLGetInfoWW'.
Mono-INFO: Probing 'SQLGetInfoWW'.
Mono-INFO: Probing 'SQLGetInfoW'.
Mono-INFO: Found as 'SQLGetInfoW'.
Mono-INFO: DllImport attempting to load: 'libdb2'.
Mono-INFO: DllImport loading location: 'libdb2.so'.
Mono-INFO: Searching for 'SQLGetInfoW'.
Mono-INFO: Probing 'SQLGetInfoWW'.
Mono-INFO: Probing 'SQLGetInfoWW'.
Mono-INFO: Probing 'SQLGetInfoW'.
Mono-INFO: Found as 'SQLGetInfoW'.
Mono-INFO: DllImport attempting to load: 'libdb2'.
Mono-INFO: DllImport loading location: 'libdb2.so'.
Mono-INFO: Searching for 'SQLGetDiagRec'.
Mono-INFO: Probing 'SQLGetDiagRec'.
Mono-INFO: Found as 'SQLGetDiagRec'.
Mono-INFO: DllImport attempting to load: 'libdb2'.
Mono-INFO: DllImport loading location: 'libdb2.so'.
Mono-INFO: Searching for 'SQLGetDiagRec'.
Mono-INFO: Probing 'SQLGetDiagRec'.
Mono-INFO: Found as 'SQLGetDiagRec'.
Bart
not useLibCli
 
Unhandled Exception: IBM.Data.DB2.DB2Exception: ERROR 08003 IBMCLI Driver CLI0106E Connection is closed. SQLSTATE=08003 InternalExecuteNonQuery: Unable to allocate statement handle.
at IBM.Data.DB2.DB2Command.AllocateStatement (System.String location) 0x00000
at IBM.Data.DB2.DB2Command.ExecuteNonQueryInternal (CommandBehavior behavior) 0x00000
at IBM.Data.DB2.DB2Command.ExecuteReader (CommandBehavior behavior) 0x00000
at IBM.Data.DB2.DB2Command.ExecuteReader () 0x00000
at (wrapper remoting-invoke-with-check) IBM.Data.DB2.DB2Command:ExecuteReader ()
at HelloWorldDb2.Main (System.String[] args) 0x00000

QuestionMulti Table ConnectionmemberNigelAF30 Aug '07 - 6:14 
Hi
 
I am programing in VB.NET 2003 & have successfully connected to DB2 using against a single table using the following code:-
 
' Open a connection to the iSeries
Dim cn As iDB2Connection = New iDB2Connection ("DataSource=AS400;DefaultCollection=TEST;")
cn.Open()
 
' Create a command that selects records from the EMPLOYEE table
Dim cmd As iDB2Command = New iDB2Command("select * from Table1", cn)
 
' Create a DataSet to hold our data.
Dim ds As DataSet = New DataSet()
 
' Execute the command, and get a DataReader in return.
Dim dr As iDB2DataReader = cmd.ExecuteReader()
 
Using the same principle I am trying to create a dataset that contains data from three tables.
 
How do I go about doing this?

 
Nigel
GeneralNetwork error while connecting to DB2/MVS from .Netmemberviv_bhatt6 Jul '07 - 5:51 
Hi,
I am trying to connect to a DB2/MVS V5R1 dtabase from a sample .Net application. I am using MS Oledb provider for DB2 for connection.
 
The connection.open throws a network error -
Could not connect to data source 'testdb2':
An internal network library error has occurred. A network level syntax error has occurred. SQLSTATE: HY000, SQLCODE: -379

 
Connection String -
Provider=DB2OLEDB; DataSource=10.10.100.5; Persist Security Info=True ; Host CCSID=
20285 ; PC Code Page=1252 ; Network Transport Library=TCPIP ; Network Address=10.10.
100.5;Network Port=23;Initial Catalog=BOGA721; Package Collection=C066705;Default
Schema=C066705;User ID=C066705; Password=******;DBMS Platform=DB2/MVS
 
Am I doing anything wrong? Please advise
GeneralurgentmemberPHANINDRA.TSK4 May '07 - 20:13 
hi please send that how to resolve this
 
Server Error in '/phani' Application.
phani "where i saved my app in Inetpub\wwwroot\-
-------------------------------------------------------------------------------
 
The 'ibmdadb2' provider is not registered on the local machine.
Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.
 
Exception Details: System.InvalidOperationException: The 'ibmdadb2' provider is not registered on the local machine.
 
Source Error:
 
An unhandled exception was generated during the execution of the current web request. Information regarding the origin and location of the exception can be identified using the exception stack trace below.
 
Stack Trace:
 

[InvalidOperationException: The 'ibmdadb2' provider is not registered on the local machine.]

 
with best regards

QuestionThe 'IBMDADB2.1' provider is not registered on the local machine???memberparvinder sehrawat9 Oct '06 - 21:14 
Hi
 
I am trying to connect DB2 through .NET but i m getting following error
- "The 'IBMDADB2.1' provider is not registered on the local machine."
 
I have VS.NET 2003,How can i resolve this problem ???
 
Code--
 
Shared connectionString As String = "Provider=IBMDADB2.1;Server=xxx.xxx.xxx.xxx;Port=xxxx;User ID=myUserIDassword=myPWD;Data Source=Database name"
 

Regards
parvinder
 

GeneralWeird connection problem with OleDBsussChristian Blackburn8 Mar '04 - 13:12 
When trying to connect to our AS400 using an OleDb connection I receive the following error:
 
Non-NULL controlling IUnknown was specified, and either the requested interface was not
IUnknown, or the provider does not support COM aggregation.
 
However, if I pop a message box, or show a form before opening the connection it all works. Just wondering if anyone has had similar experiences, and if so, how they got around having to use a GUI element to get it to work?

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

Permalink | Advertise | Privacy | Mobile
Web02 | 2.6.130523.1 | Last Updated 29 Aug 2003
Article Copyright 2003 by Sreejumon
Everything else Copyright © CodeProject, 1999-2013
Terms of Use
Layout: fixed | fluid