Binding DB2 Data to Visual Studio 2005 Windows Applications
July 26, 2006
In a previous set of articles, I showed you all the great integration features between the IBM DB2 Universal Database for Linux, UNIX, and Windows Version 8.x (DB2 UDB) product and Microsoft Visual Studio.NET 2003 integrated development environment (IDE).
In early June 2006, IBM announced the next release of the DB2 UDB product, DB2 9 (formerly known as DB2 Viper). Part of this announcement includes the support for Microsoft Visual Studio 2005 and its accompanying ADO.NET 2.0 driver.
As you may recall, Microsoft announced the Visual Studio 2005 product late in 2005, along with SQL Server 2005. Around the same time, the DB2 team delivered a beta of the now officially announced integration into Microsoft Visual Studio 2005 and ADO.NET 2.0. You can download this beta (not needed after DB2 9 becomes generally available) at: http://www-306.ibm.com/software/data/db2/windows/dotnet.html.
In this article, I want to show you how easy it is to be a .NET developer and work with DB2 UDB V8 or DB2 9 databases. Specifically, I will show you how to quickly create a Windows application and bind DB2 UDB V8 data to various controls on the Windows form. Of course, I could have easily bound data from a DB2 9 database (or a combination of the two versions of the product) but I wanted to illustrate the flexibility of the integration that DB2 offers into the V8 product set.
Adding a DB2 Data Source
Before you can create an application to which you will bind your DB2 data, you need to have a connection to that database in the Server Explorer. If you are familiar with the Visual Studio.NET 2003 support that DB2 UDB provides, youll recall that .NET developers writing applications on the DB2 platform were required to work within the IBM Explorer. The IBM Explorer was functionally equivalent to the Server Explorer; however, there were no open interfaces into the Server Explorer in Visual Studio.NET 2003 that DB2 UDB could leverage to provide some of the features unique to the DB2 UDB plug-in.
The architecture of Visual Studio 2005 changed such that there are now interfaces that let you develop applications that connect to DB2 UDB V8 and DB2 9 databases using the Server Explorer. This provides a more native experience for .NET application developers used to developing applications on SQL Server databases.
To add a DB2 database connection to your Server Explorer, perform the following steps:
1. Right-click the Database Connections folder in the Server Explorer and select the Add Connection option. The Add Connection dialog box opens:
2. Ensure that the Data source field points to the .Net Framework Data Provider for IBM DB2 data provider so that the Server Explorer will use the ADO.NET data provider written by IBM specifically for DB2 UDB 8 and DB2 9 databases.
The DB2 ADO.NET provider is not the default provider shown in this field. To change the database provider to use the one for DB2, click Change and select the IBM DB2 option from the Data source box, as shown below. You should also ensure that the IBM DB2 Data Provider for .NET Framework is selected in the Data provider field, but this should be the default:
Note: If you plan to work frequently with DB2 database connections, select the Always use this selection check box so that, when you add another database connection, Visual Studio 2005 will automatically select the DB2 data provider.
3. Enter the server name and port number (separated by a colon) in the Enter server name field. If you are connecting to a local database, you can use the localhost alias for your workstation.
Depending on the version of DB2 that you are running your beta on, you can optionally click Refresh to automatically list all the databases configured to respond to DB2 network database identification requests and automate the process of entering the required information.
4. Enter your user account credentials in the User ID and Password fields. I recommend that you save these credentials in the connection string (they are encrypted) by selecting Save my password. Selecting this option makes application development more streamlined because you are not challenged to provide authentication details during subsequent access requests to the DB2 database.
5. Select the database name from the Select or enter a database name drop-down box, or enter the name manually.
Note: In this article, I chose to connect to the SAMPLE database that is shipped with DB2 UDB V8. If you dont have the SAMPLE database created on your workstation, you can create it now by entering the db2sampl command from a Windows-based command prompt.
You can use the Specify Connection Options and Specify Filtering Options sections to further customize your database connection. The options associated with these toggles are shown below:
The DB2 support for Visual Studio 2005 comes with a rich set of connection time and filtering options. For the purposes of this article, you can just select the defaults.
6. Test the connection using the Test Connection button.
7. Click OK.
After adding your database connection, the Visual Studio 2005 Server Explorer should look similar to this:
In the previous figure, you can see that I have expanded the SAMPLE database connection object; below it is a connection object to a SQL Server 2005 database. Notice the beside this database connection object: all databases appear this way until you click them to make the database connection.