Since the middle of 2006, Ive been writing about all the great integration
features in IBM DB2 9 for the Microsoft Visual Studio 2005 integrated
development environment (IDE) and the accompanying ADO.NET 2.0 API.
In an article called Binding
DB2 Data to Visual Studio 2005 Windows Applications, I showed you how
to quickly build a Windows application that binds various controls (ComboBoxes,
DataGrids). In Build a
DB2 Bound Data Application using Visual Studio 2005, I showed you an
alternative way to even more quickly build a WinForm Windows application using
a data grid that binds to your DB2 data server. You dont specifically bind
controls to an application: its even easier than that.
In this article, you can use either of those methods to
build a new application, but I want to show you how to use a FILLBY method to pass dynamic parameters to
the application, and then surprise you with a twist at the end.
The move from DataAdapters to TableAdapters
Although I didnt spell it out in my previous DB2 9 and
Visual Studio 2005 articles, the Visual Studio 2005 IDE adds a new helper class
called a TableAdapter, which improves drag-and-drop application
development. A TableAdapter is created automatically when you drag a data
source from the Data Sources view onto a designer palette. The
TableAdapter in Visual Studio 2005 replaces the DataAdapter that was part of Microsoft
Visual Studio.NET 2003, which Microsoft has now deprecated in Visual Studio
One unique characteristic about TableAdapters is that they
are not found in the .NET 2.0 Framework. Unlike typed data sets, which inherit
from the System.Data.DataSet class,
TableAdapters are entirely generated by Visual Studio 2005 using the data model
you create with the Data Source Configuration Wizard, or in the Data Set
TableAdapters abstract the database type away from
application code, which lends itself to more flexibility for application
deployment. For example, let's say you have developed your application using SQL
Server 2005. Later you decide to port that database to DB2 running on Linux,
UNIX, or Windows. In the .NET 1.0 and .NET 1.1 Frameworks, which were
integrated into Visual Studio.NET 2002 and Visual Studio.NET 2003, you would
have had to change the object types you were using to access the database; in
Visual Studio 2005, its a completely different (and more streamlined) story.
Building an application using the FILLBY method
To build an application using the FILLBY method in your applications tool strip, perform the following
a new C# application, called MyFILLBYProject, by selecting File>New Project>Windows Application, selecting Visual Studio C# (which
may be under Other Languages depending on how you set up your IDE),
entering the name in the Name field, and clicking OK.
the Server Explorer (by pressing Ctrl+Alt+S or by selecting View>Server Explorer from the menu bar), and
select a table. (In my example, I selected the XTREME table,
which I created in the SAMPLE database, but you can use any table you want.)
View the properties of your tables columns by selecting Properties from
the right-click context menu and see if your table has a primary key.
A column that is defined as a
tables primary key has a 1 in the Primary Key property field, while
a column that is not a primary key has a 0 (or may be blank).
For this article, the table you
select doesnt need a primary key because Ill show you how to use the tool
strip to filter data with a FILLBY
method. However, if the table you select doesnt have a primary key, you wont
be able to perform INSERT, UPDATE, and DELETE
operations on this data. The DB2CommandBuilder
uses a primary key for these types of data operations in order to build the corresponding
statements that it uses to change the underlying tables row.
Its easy to understand this requirement
when you think about it. A data set is a disconnected data persistence
container. When the DataAdapter
reconnects to the actual data server to process any data change requests, how
can it be sure it will change the data for the row that was fetched? Using a primary
key, of course! With a primary key, the DB2CommandBuilder
can build a WHERE clause for the dirty
row to ensure that it is positioned on the same row in the database as the data
set. If there is no primary key, then the DB2CommandBuilder
cannot guarantee it is positioned on the same row, and will not build statements
to change data. You can, of course, work around this method if you know the
metadata of the table and hand-generate these data manipulation statements
(DML) yourself. However, we want a slick and easy solution, so if you plan to
code any INSERT, UPDATE, and DELETE
operations into your application, ensure that you have defined a primary key on
the table where the data resides.
that the Add Data Source view is displayed in Visual Studio 2005 by
pressing Shift+Alt+Delete or selecting Data>Show Data Sources from the menu bar.
Add New Data Source in the Data Sources view, select Database,
and click Next:
the SAMPLE database from the Which data connection should your application
use to connect to the database? drop-down list.
the Yes, include sensitive data in the connection string radio button,
and click Next. (Im assuming that youre just working on your own test
machine here and notice that Visual Studio 2005 collapses the Connection
string box to hide these details from prying eyes.)
the defaults on the next pane of this wizard. Click Next.
the Tables toggle, select the table that you want to include in your application,
and click Finish:
You can select multiple sources
from your underlying data server to populate the disconnected data set. In the
previous figure, Ive added a single table called XTREME (and all of its
columns). You could have multiple tables, or a combination of tables and views.
In addition, you could even use a function or stored procedure to populate the
data set. (I will show you this method in a future article.)
After youve added the data set,
the Visual Studio 2005 IDE should look like this:
Note that your table has populated
a data set in the Data Sources view. Expand it to see all of its
contents if it isnt automatically expanded for you.
the left mouse button to select and drag two columns from your data set to your
Windows Application form, and then resize the data grid so it looks like this:
10. Now add a DataGridView to
list all the data in the data set from Toolbox and bind it to the <your_table_name>BindingSource
object such that the designer palette looks like this:
11. Press F5 to build
your application. It will look similar to this:
The default Fill method called by Visual Studio 2005 doesnt
have any capabilities to filter rows; rather, it just does a dump of all the
data thats in a table. This may not be optimal if youre prototyping an
application that goes against a table with a large number of rows.
You should be somewhat careful
when building an application like this with large tables, or you could grind
your system such that youll have an unscheduled coffee break. The FILLBY method provides an opportunity to
filter the result set and subsequently the amount of data being retrieved from
the data server.
12. Add a query for this data
set by right-clicking the <your_table_name>TableAdapter and
selecting Add Query. (Since the table I built a data set for is called
XTREME, my table adapter component tray shows it as xTREMETableAdapter.)
Search Criteria Builder window opens. Click Query Builder.
14. Use the Microsoft Query
Builder to build a query such that you set one of the columns that the data set
retrieves as a parameter that will filter the data. For example, in the
previous figure, I could select to filter this data set based on the COUNTRY
column by changing the Filter field to =? (as shown in the
You can test the data that your
query will retrieve by clicking Execute Query. Simply fill in a data
value for the defined parameter and click OK:
15. Click OK to close the
Query Builder and then OK again to close the Search Criteria Builder
window. Your WinForm should look similar to this:
In the previous figure, Ive
highlighted the new tool strip that was automatically created for you when you
created the new query statement. (You may need to resize your WinForm in order
to see it.)
You should also see a new component
for this query in the component tray:
16. Press F5 to build
your application again. It should look like this:
17. Enter a column value that
corresponds to the column you selected to filter the table by when you built
the qualifying query and click FillBy1. (In my example, I use the COUNTRY
column, whose values include Canada, USA, Australia,
and so on.) Your application should filter the results and look like this:
You can see that the data grid was filtered to include only the
parameter entered in the FillBy1 tool strip. This parameter is dynamic;
you can change it whenever you want. Just click FillBy1 each time to
refresh the data.
As you navigate your data set, you can see that the COUNTRY
and CITY text boxes are automatically updated to correspond to the data
grid pointer ():
Wrapping it up...
In this article, I showed you how you can easily add
controls to manage large data sets that you may be retrieving from your DB2 9
data server without writing a single line of code! With a few added clicks, you
created an application that could be well suited for quick prototyping and data
What was the twist?
The DB2 9 table XTREME isnt really a DB2 table. Its
actually a Microsoft Excel 2007 spreadsheet with a nickname created over it so that
it is exposed to developers as a DB2 table. This technology is referred to as federation:
You can actually use products such as DB2 Connect (for DB2
for i5/OS and DB2 for z/OS integration), WebSphere Federation Server, WebSphere
Information Server, or the Homogeneous Federation add-on feature packs
available with all DB2 9 editions to add capability like this to your data
Whats the benefit? All of your data artifacts can be
abstracted so that developers just go after data. They dont need to concern
themselves with the fact that SQL Server uses a MONEY
data type for currency, while Oracle uses a NUMERIC,
and DB2 a DECIMAL. If Excel doesnt
have a specific function, the DB2 federation software will compensate for it.
Imagine how simple it becomes to build applications using these concepts. You
can expose WebSphere MQSeries message queues as tables. Just insert and delete
from the table to write and destructively read from a message queue. When was
the last time you met a VB.NET developer who could do that? With this type of
technology, they can in an instant. How cool is that?
Now that youre building applications using data artifacts,
you can easily join disparate data sources without replication, APIs,
workarounds, manual conversion, and the like. For example, lets say you wanted
to join your Excel table data with a table in DB2 9:
In the previous example, the BUYERS table resides in DB2 9
and its joined with the Excel spreadsheet used in this articles example.
This is perhaps the biggest differentiator when it comes to
DB2 programmability and productivity aside from the tight integration into the
rapid application development tools found in todays most popular IDEs. Its
important to note that in many cases, connections to remote data sources arent
via the least common denominator (like OLE DB, for example). If you were
joining an Oracle table, an Ora 8 connection library would be loaded.
Federation, as its implemented in a DB2 environment, is perhaps the first real
loosely coupled architecture before the service-oriented architecture framework
made this catch word so popular.
See All Articles by Columnist Paul C. Zikopoulos
About the Author
Paul C. Zikopoulos,
BA, MBA, is an award-winning writer and
speaker with the IBM
Database Competitive Technologies team. He has more than ten years of
experience with DB2 UDB and has written over one hundred magazine articles and several
books about it. Paul has co-authored the books: DB2 9 New Features (available
soon), DB2 Version 8: The Official Guide, DB2: The Complete Reference, DB2
Fundamentals Certification for Dummies, DB2 for Dummies, and A DBA's Guide to
Databases on Linux. Paul is a DB2 Certified Advanced Technical Expert (DRDA and
Cluster/EEE) and a DB2 Certified Solutions
Expert (Business Intelligence and Database Administration). In his spare time,
he enjoys all sorts of sporting activities, running with his dog Chachi, and
trying to figure out the world according to Chloë his new daughter. You can
reach him at: email@example.com.
IBM, DB2, DB2
Connect, DB2 Universal Database, i5/OS, MQSeries, WebSphere, and z/OS are trademarks
or registered trademarks of International Business Machines Corporation in the United States, other countries, or both.
Microsoft and Windows
are trademarks of Microsoft Corporation in the United States, other countries, or both.
UNIX is a
registered trademark of The Open Group in the United States and other countries.
Linux is a registered
trademark of Linus Torvalds in the United States,
other countries, or both.
product, and service names may be trademarks or service marks of others.
International Business Machines Corporation, 2007. All rights reserved.
solutions, and advice in this article are from the authors experiences and are
not intended to represent official communication from IBM or an endorsement of
any products listed within. Neither the author nor IBM is liable for any of the
contents in this article. The accuracy of the information in this article is
based on the authors knowledge at the time of writing.