asp tutorials, asp.net tutorials, sample code, and Microsoft news from 15Seconds
Data Access  |   Troubleshooting  |   Security  |   Performance  |   ADSI  |   Upload  |   Email  |   Control Building  |   Component Building  |   Forms  |   XML  |   Web Services  |   ASP.NET  |   .NET Features  |   .NET 2.0  |   App Development  |   App Architecture  |   IIS  |   Wireless
 
Pioneering Active Server
 Power Search





Active News
15 Seconds Weekly Newsletter
• Complete Coverage
• Site Updates
• Upcoming Features

More Free Newsletters
Reference
News
Articles
Archive
Writers
Code Samples
Components
Tools
FAQ
Feedback
Books
Links
DL Archives
Community
Messageboard
List Servers
Mailing List
WebHosts
Consultants
Tech Jobs
15 Seconds
Home
Site Map
Press
Legal
Privacy Policy
internet.commerce














internet.com
IT
Developer
Internet News
Small Business
Personal Technology
International

Search internet.com
Advertise
Corporate Info
Newsletters
Tech Jobs
E-mail Offers

HardwareCentral
Compare products, prices, and stores at Hardware Central!

A Primer on Using DB2 with .NET
By Chip Irek
Rating: 4.3 out of 5
Rate this article


  • email this article to a colleague
  • suggest an article

    Introduction

    Working with DB2, from a .NET application developer's perspective, is just like working with any other relational database. One can find endless examples of how to perform databases tasks (from the mundane to the interesting) for Microsoft SQL Server and Oracle, but there is not as much similar documentation for accessing DB2 from Microsoft technologies, including .NET.

    A developer currently has different access techniques to programmatically connect .NET programming clients to DB2. For each of these the code is basically the same thanks to the providers involved, but some interesting limitations exist that you should consider.

    DB2 Architecture Overview

    The basic elements of the DB2 database engine are database objects, system catalogs, directories, and configuration files. All data access takes place through the SQL interface. You can run DB2 Univeral Database (UDB) as just the database server, with no additional products required. But for remote clients, you'll need additional products.

    The server products of DB2 UDB provide support for communication to the database server using protocols such as TCP/IP, SNA, or IPX/SPX. This, then, allows access from remote clients running the Administration Client, Runtime Client, or Application Development Client.

    The Application Development Client component is a collection of tools that are designed for database application developers. It includes libraries, header files, documented APIs, and sample programs.

    You should focus on the client that accesses the database server over the network. By using DB2 Connect, your .NET applications can access host-based DB2 (DB2 for AS/400, DB2 for OS/390, and DB2 for VSE and VM database servers).


    Figure 1: DB2 Component Architecture

    Options for Connecting to DB2 from .NET

    How you connect to DB2 determines what you can do. There are three techniques to connect to DB2 from .NET:

    1. Using an OleDb .NET Managed Provider Both Microsoft and IBM recommend utilizing an OleDb data provider for accessing most DB2 data when either DB2 V7 or lower is used or when a COM object is the caller. Within the .NET framework, OleDb providers are exposed via COM InterOp and essentially use the same drivers available for ADO development.
    2. Using an ODBC .NET Managed Provider This provides access to native ODBC drivers the same way the OLEDb .NET Data Provider provides access to native OLEDb providers. The ODBC .NET Data Provider is an add-on component to the .NET Framework. The ODBC .NET Data Provider is intended to work with all compliant ODBC drivers.
    3. Using the IBM DB2 .NET Provider (Beta) This provides ADO.NET connectivity to DB2 V8.1. The DB2 .NET Data Provider is an add-in component to the Visual Studio .NET Framework. This provider accesses IBM DB2 database servers running on variety of hardware and operating system platforms. At the time of this writing, the provider is still beta but is expected to be in production soon. The DB2 .Net Data Provider provides connectivity from applications written using ADO.NET to the following DB2 family of servers:
      • DB2 Universal Database Version 8.1 running on Windows, UNIX and Linux
      • DB2 Universal Database for z/OS and OS/390 V6.1 and later using DB2 Connect V8.1

    The different connection strings highlight the syntactical difference between scenarios.

    The OleDb connection string ...
    Provider=IBMDADB2.1;User ID=db2admin;Password=db2admin;Data Source=SAMPLE

    The ODBC connection string ...
    DSN=DB2V8;UID=db2admin;PWD=db2admin

    The IBM Managed Provider connection string ...
    Database=SAMPLE;User ID=db2admin;Password=db2admin;Server=IREK

    Comparing Code Level Access

    When you work with DB2 data access code, most operations are largely the same as you would expect as compared to working with other databases. Here I compare some code across connection techniques.

    In each of the following code snippets, we are trying to show some of the mechanics of accessing the database and managing data.

    Notice how the steps are the same for each technique. You start by creating the connection object, then creating a command object. In this simple example, I'm just retrieving a scalar value of the count of rows in this particular table. Finally, we execute the command's ExecuteScalar() method to return the integer.

    With Ole Db...
    private OleDbConnection cn = new OleDbConnection(connectionString); OleDbCommand cmd = new OleDbCommand("SELECT COUNT(*) FROM STAFF", cn); int rc = Convert.ToInt32(cmd.ExecuteScalar());

    With ODBC...
    private OdbcConnection cn = new OdbcConnection(connectionString);
    OdbcCommand cmd = new OdbcCommand("SELECT COUNT(*) FROM STAFF", cn);
    int rc = Convert.ToInt32(cmd.ExecuteScalar());

    With IBM's Managed Provider...
    private DB2Connection cn = new DB2Connection(connectionString);
    DB2Command cmd = new DB2Command("SELECT COUNT(*) FROM STAFF", cn);
    int rc = Convert.ToInt32(cmd.ExecuteScalar());

    What's key here is that nothing remarkable or confusing is happening!

    Functionality Comparison

    Based on which access technique you choose and which version database you access, you will have certain functionality open to you.

    You need to choose a connection strategy based on what work you need to accomplish.

    To measure this and provide some guidance, I put together a quick roadmap to help in the decision framework. I isolated a few key functional areas I felt were common to most database centric software development projects.

    In the chart below I summarize my findings. I measured each of the three access techniques against whether I could perform some particular database task with the provider. The database tasks I measured are:

    • Pass-thru SQL - Does the provider support working with simple pass-through dynamically built SQL statements?
    • Simple Stored Procedures - Does the provider support calling basic stored procedures, including returning resultsets?
    • In, Out, and InOut parameters - Does the provider support calling stored procedures and utilizing parameters to pass variable data?
    • Dates and Currency - Does the provider support vendor-specific data types that might pose a problem?
    • LOBs - Does the provider support working with large object data types?

    I measured these tests against DB2 V7 and V8 on one dimension and across access methods on the other.


    Figure 2: Summary of functionality yielded by various providers

    You can perform different kinds of work based on how you connect because each provider has a set of things it can accomplish. There are some items worth careful noting!

    For example, ODBC doesn't support DB2 stored procedures. So if you are building an application heavily dependant on stored procedures, you need to eliminate ODBC as your access method and consider OleDb or the managed provider.

    OleDb doesn't support large LOB data types with DB2. So, if you want to retrieve LOBs (for example JPEGs) you can use ODBC, or the beta managed provider against V8.

    Currently, the managed provider doesn't work with DB2 V7 or earlier at all, unless you have DB2 Connect V8 running between the application and a host-based DB2, like you would find on an OS/400 or an S/390.

    Interestingly, DB2 V7 and OleDb have a well-known bug working with stored procedure parameters. You can return data from stored procedures, but you cannot pass parameters to or from stored procedures under OleDb with DB2 V7.

    Performance Comparison

    To measure the providers' performance and offer guidance, I set out to define an effective test script and measure some key metrics across providers. For comparing performance I built some very simple Web pages, one for each test that exercised the exact same functionality. That gives us the control data to analyze the throughput under different connection techniques.


    Figure 3: Summary of performance results yielded by various providers.

    When assessed strictly from a performance perspective, you can see that the managed provider yields much better results accessing DB2 data. This is to be expected, but nonetheless it is helpful to see the statistics represented. There are two significant numbers worth monitoring in a test this simple. Requests per second (RPS) gives you a sense of how well a scenario scales. OleDb and ODBC both perform roughly the same under stress, but naturally the managed provider performs better (in fact about 10% better).

    Time-to-last-byte (TTLB) gives you a sense of how responsive the scenario is from an end-user's perspective. Here again, the managed provider performs better (about 10%) than OleDb or ODBC.

    So what is most notable is that the OleDb and ODBC scenarios provide roughly the same performance under load, while providing different functionality. IBM's managed provider provides notably better performance, and the most reliable functionality for addressing DB2 V8. But again, it does not currently support DB2 V7 and lower (without the DB2 Connect intermediary).

    Conclusion

    For application developers, working with DB2 as a backend is just as easy as working with any other RDBMS accessed via ADO or ADO.NET. ADO.NET does all the abstraction for you, such that your code looks and behaves similarly across databases. While there are architectural considerations when choosing between DB2 and other databases, as a developer your work should be predictable and manageable.

    About the Author

    Chip Irek is an Architect with IBM Global Services. He works in a group called Enterprise Services for Microsoft Technologies, providing .NET services to IBM customers. Chip has worked on several projects leveraging .NET and DB2. With nearly 15 years experience, he is an MCSD, an IBM certified Architect, and is pursuing his .NET certification. He can be reached at irek@us.ibm.com.

  • Rate This Article
    Not HelpfulMost Helpful
    1 2 3 4 5
    Supporting Products/Tools
    Proposion N2N
    Proposion N2N connects Microsoft .NET applications to Lotus Notes and Lotus Domino databases. This ADO.NET managed data provider allows you to perform blindingly fast queries and updates of Notes data from ASP.NET pages, .NET web services, Windows, or Mobile applications. An innovative SQL-like query language leverages the unique features of Notes and makes collaborative software accessible to relational database programmers.
    [Top]
    Other Articles
    Sep 15, 2005 - Building an Image Keyword System
    Unlike text-based file formats image files aren't made up of words, which makes searching for an image file by keyword difficult. Instead of being able to simply open the file to see what it contains, we're stuck looking at the text around it and other metadata to determine the image's meaning. In this article, Ziran Sun shows you how to build a simple database-based image keyword system that allows you to associate keywords with images and use these keywords to make finding images easier.
    [Read This Article]  [Top]
    Apr 7, 2005 - A Step-by-Step Guide To Using MySQL with ASP.NET - Part 2
    In the second part of of his article on using MySQL with ASP.NET, Ziran Sun covers how to add a new MySQL user to the database server, assign the user the appropriate permissions, connect to the database, and build a simple ASP.NET page to perform a query.
    [Read This Article]  [Top]
    Feb 10, 2005 - A Step-by-Step Guide To Using MySQL with ASP.NET - Part 1
    Back in the days of classic ASP, if you were building a database-driven web site, your choice was either to invest a lot of money to get a copy of Microsoft SQL Server (or some other enterprise-ready database) or invest a lot of time finding a way to deal with the performance and scalability limitations of Microsoft Access. Luckily these days there's another viable alternative: MySQL.
    [Read This Article]  [Top]
    Jan 27, 2005 - Moving a Database from SQL Server 7.0 to SQL Server 2000
    Moving or copying a SQL Server database from one machine to another requires a lot of preparation in order to ensure a smooth transfer. In this article, Dina Fleet Berry examines the different methods and highlights the different issues associated with each of them.
    [Read This Article]  [Top]
    Jan 6, 2005 - Debugging a SQL Stored Procedure from inside SQL Server 2000 Query Analyzer
    There are many times when using SQL Server 2000 Query Analyzer to debug SQL statements is a better choice than debugging in Visual Studio .NET. In this article, Dina Fleet Berry explains why and walks you through the debugging process step-by step.
    [Read This Article]  [Top]
    Nov 24, 2004 - Persisting .NET Objects to SQL Server Using SQLXML and Serialization
    As a follow up to his article on retrieving objects from SQL Server using SQLXML and serialization, Gianluca Nuzzo discusses saving objects back to SQL Server using a schema definition file and updategrams.
    [Read This Article]  [Top]
    Sep 14, 2004 - Transaction Processing in ADO.NET 2.0
    One area that stands out when comparing ADO.NET 1.x to ADO.NET 2.0 is transaction processing. Bill Ryan shows just how easy transaction processing has become with the TransactionScope object in ADO.NET 2.0.
    [Read This Article]  [Top]
    Sep 8, 2004 - Custom Object Data Binding with .NET
    Developers often use brute force coding to marshal data between the GUI and application objects. In this article, Luther Stanton explains how to use .NET's out-of-the box data-binding functionality to make this job much easier.
    [Read This Article]  [Top]
    Sep 2, 2004 - Queue MSMQ Messages from SQL Server
    Learn how to create a console application to queue a message in Microsoft Message Queuing (MSMQ) and then use an extended stored procedure to call the console application from a SQL Server trigger.
    [Read This Article]  [Top]
    Aug 30, 2004 - Tuning Up ADO.NET Connection Pooling in ASP.NET Applications
    Connection pooling increases the performance of Web applications by reusing active database connections instead of creating a new connection with every request. This article shows how to monitor the connection pool, diagnose a potential problem, and apply the appropriate fix.
    [Read This Article]  [Top]
    Mailing List
    Want to receive email when the next article is published? Just Click Here to sign up.

    Support the Active Server Industry



    JupiterOnlineMedia

    internet.comearthweb.comDevx.commediabistro.comGraphics.com

    Search:

    Jupitermedia Corporation has two divisions: Jupiterimages and JupiterOnlineMedia

    Jupitermedia Corporate Info


    Legal Notices, Licensing, Reprints, & Permissions, Privacy Policy.

    Advertise | Newsletters | Tech Jobs | Shopping | E-mail Offers