15
An Introduction to Application Development in IBM DB2 UDB using Microsoft Visual Studio .NET By Anil Mahadev Database Technologist and Enthusiast In this tutorial, I will dive into the nitty gritties of DB2 Application Development using the DB2 Addin for Visual Studio.NET and build a simple Database Application using VC# and IBM DB2 UDB. This tutorial is a Level 100 session, which means that basic knowledge of the products is necessary. System Requirements: A basic P4 Machine with 512 MB RAM and about 20 GB HDD, a pointing device, A keyboard and a monitor. Software Requirements 1) Windows 2000 and above. 2) A personal copy of DB2 UDB (Any edition will do, as I am still developing for test purposes only, I will be using the Personal Edition of DB2 UDB 8.1 for Windows. 3) Microsoft Visual Studio.NET 2003(Professional, Enterprise Developer or Enterprise Architect). 4) Install the DB2 Addin, once you have completed the Installation of DB2 UDB. 5) Finally the urge to learn new things and adapt accordingly.

IBM DB2 App Development with Microsoft Visual C#

Embed Size (px)

DESCRIPTION

A basic article on IBM DB2 app development with using Microsoft Visual C#

Citation preview

Page 1: IBM DB2 App Development with Microsoft Visual C#

An Introduction to Application Development in IBM DB2 UDB using

Microsoft Visual Studio .NET

By

Anil Mahadev

Database Technologist and Enthusiast

In this tutorial, I will dive into the nitty gritties of DB2 Application Development using the DB2 Addin for Visual Studio.NET and build a simple Database Application using VC# and IBM DB2 UDB.

This tutorial is a Level 100 session, which means that basic knowledge of the products is necessary. System Requirements: A basic P4 Machine with 512 MB RAM and about 20 GB HDD, a pointing device, A keyboard and a monitor. Software Requirements 1) Windows 2000 and above.

2) A personal copy of DB2 UDB (Any edition will do, as I am still developing for test purposes only, I will be using the Personal Edition of DB2 UDB 8.1 for Windows. 3) Microsoft Visual Studio.NET 2003(Professional, Enterprise Developer or Enterprise Architect). 4) Install the DB2 Addin, once you have completed the Installation of DB2 UDB. 5) Finally the urge to learn new things and adapt accordingly.

Page 2: IBM DB2 App Development with Microsoft Visual C#

As this tutorial is going to be fully practical, attention to detail is always necessary. If you do not understand a particular step, please go ahead and try it, until you get it right. So what are we waiting for, let’s develop a simple database application using the tools mentioned above, to create an Employee Management System that views employee details.

So lets Begin (Note: Hereafter I will be referring to Visual Studio.NET as VS.NET) Firstly for those of you who have not installed the DB2 Addin for VS.NET, you can follow these steps.

Page 3: IBM DB2 App Development with Microsoft Visual C#

Click on Start Programs IBM DB2 Setup Tools Register Visual Studio

Addins as shown in Fig 1.1

It will then open a command prompt window stating that the Addins were installed successfully. Now that part is done, we now need to start a new DB2 project within VS.NET.

To do that, start VS.NET from your machine and you will notice a new icon mentioned as DB2 Tools, will be visible along with the remaining languages, you have installed.

Page 4: IBM DB2 App Development with Microsoft Visual C#

Now let’s give our project a sensible name, such as EmployeeApp as shown in Fig 1.2.

Page 5: IBM DB2 App Development with Microsoft Visual C#

Next we get the following Screen by default as shown in Fig 1.3

U may notice a new toolbar appears in your IDE. This toolbar is called the IBM DB2 Toolbar. For your convenience for viewing, I have dragged the toolbar below the form.

NOTE: (This tool isn’t just a few icons up there; it acts as an interface between your DB2 Client tools and your DB2 Server. You can launch the Control center within VS.NET using this toolbar, instead of going through the traditional way to launch it.)

Page 6: IBM DB2 App Development with Microsoft Visual C#

We are now going to start building our Application. Step 1: Establish a connection to the Database using the Appropriate Connection, Data Adapter and build our Application.

Since I have designed this tutorial for newbies, those who are just getting their hands dirty, I shall be using the DB2 Data Adapter Control. So let us now build our Database Connection and fetch the Employees Table from the SAMPLE Database provided by DB2 UDB. Now, drag and drop a DB2 Data Adapter onto your form. Next rename the DataAdapter to db2Adp, under the properties window. Now click on Configure Data Adapter.

We are presented with the DB2 Data Adapter Configuration Wizard as shown in Fig 1.4

Follow the instructions as given from now on.

Page 7: IBM DB2 App Development with Microsoft Visual C#

Please remember, depending on the permissions set by your DBA, you will be able to access the respective Database Objects.

Click on Next

You will be presented with the following screen.

We are now going to specify the parameters for each one.

Since you have not established a connection yet. Please do so immediately.

Page 8: IBM DB2 App Development with Microsoft Visual C#

Click on New Connection Button and you will be presented with the main

connection parameters dialog, where you will specify the following details Connection Name: This is your connection name. (Your Database Name becomes you connection name)

Database Alias: You can specify the Database you wish to connect. In this case we shall connect to the SAMPLE Database. User Name: This is your db2 username. (Use your credentials).

Password: This is your db2 password. (Use your credentials). Now click on Test Connection.

You should get the following result as from the Fig 1.5

Page 9: IBM DB2 App Development with Microsoft Visual C#

Next Deselect all the default options as shown in Fig 1.6; since we are only going

to display data, I have another interactive tutorial in stored for that purpose.

Next we are presented with a query editor that allows us to enter custom SQL Statements.

Page 10: IBM DB2 App Development with Microsoft Visual C#

Since we would like the details of all Employees, please give the following query.

SELECT * FROM DATABASEARCHITECT.EMPLOYEE AS

EMPLOYEE;

Here please replace DATABASEARCHITECT with your Schema and User credentials. The query should be like in fig 1.8

Click Next

Page 11: IBM DB2 App Development with Microsoft Visual C#

As you can see in Fig 1.10, the Finish screen appears with all the parameters.

Click Finish and you’re done configuring your DB2 Database to listen to your application now. As you will notice, two new icons will appear on the screen, they are your db2ADP and your DB2 Connection objects.

Page 12: IBM DB2 App Development with Microsoft Visual C#

As we all know, we need to create a Dataset. For those of you, who don’t know what a dataset is, it is an in memory database, that stores the database related information even after the connection is being lost from the database. Now right click on your DataAdapter and say generate Dataset as shown in Fig 1.11 and Fig 1.12.

Rename your Dataset to DB2DS1 for simplicity.

We have now completed the task of configuring our Data Adapter, creating a Dataset from it and successfully establishing a connection to the SAMPLE Database.

Page 13: IBM DB2 App Development with Microsoft Visual C#

For this tutorial, I am going to place a Data Grid Control on to the Form and we are

going to display the employee records when the Form is loaded.

1) Add a Data Grid on to the Form and Set its Dock property to Fill.

Set its Data Source = db2DS11 and for Data Member = EMPLOYEE.

Then set the Forms Startup Position = “Center Screen” and Border Style =”Fixed Single”. Next Open the Code window and type the following code in the Form_Load Event. (I shall be using C# for this tutorial);

//Fill the Data Adapter

db2Adp.Fill(db2DS11);

//Set the Data Source of the DataGrid to the DataSet

dataGrid1.DataSource = db2DS11;

//Now bind the DataGrid with the Binding Option to

display Employee Details from the Employee Table

dataGrid1.SetDataBinding(db2DS11,"Employee");

Page 14: IBM DB2 App Development with Microsoft Visual C#

Now let’s run the Application by pressing f5.

2) And let DB2 and .NET work Magic for you as you see in Figure 1.13

Voila!!! Now wasn’t that easy. In my next series, I shall be diving into the various powers of DB2 and .NET combined using Stored Procedures, Triggers, the only difference will be that , I will be developing an ASP.NET Web Application and Web Service that interact with a DB2 UDB Database. Well folks this was a very short introduction to DB2 using .NET, hope you have found this article fun learning, as much as I have writing it. Please feel free to mail me your comments and valuable inputs on how I can write

better in DB2 and .NET.

Page 15: IBM DB2 App Development with Microsoft Visual C#

You can reach me @ Anil Mahadev

[email protected]

IBM DB2 UDB Logo is Copyright of IBM (International Business Machines) Corporation, USA and other International Countries where present.

Microsoft Windows and Microsoft Visual Studio.NET logo are copyright of Microsoft Corporation, USA and other International Countries where present.