Building .NET Applications Using Oracle Developer Tools for Visual Studio .NET
Building .NET Applications Using Oracle Developer Tools For Visual Studio .NET
Building .NET
Applications Using Oracle Developer Tools for Visual Studio .NET
This tutorial describes how to build .NET applications using Oracle Developer Tools for Visual Studio .NET.
Approximately 30 minutes
This tutorial covers the following topics:
Place
the cursor over this icon to load and view all
the screenshots for this tutorial. (Caution: This action loads all
screenshots simultaneously, so response time may be slow depending on your Internet
connection.)
Note: Alternatively, you can place the cursor over an individual icon in the following steps to load and view only the screenshot associated with that step. You can hide an individual screenshot by clicking it.
The new Oracle Developer Tools for Visual Studio .NET (ODT) is a tightly
integrated "Add-in" for Visual Studio 2005 and Visual Studio .NET 2003 that brings the power of the Oracle Database to .NET developers! ODT is a free product that can be downloaded from the Oracle Technology Center website.
Use the Oracle Explorer to browse your Oracle schema, launch one of the many powerful designers and wizards to create and alter schema objects, and drag and drop schema objects onto your form to automatically generate code.
There's also a PL/SQL editor, a fully integrated PL/SQL debugger, Adhoc
SQL Query Window, and integrated context sensitive online help,
including the Oracle SQL and PL/SQL Users Guides. And with the Oracle Data Window you won't have to leave the Visual Studio environment for routine database tasks like inserting and updating Oracle data or testing stored procedures!
This and many other features greatly improve developer productivity and make developing for Oracle on Windows easy to learn!
Back to Topic List
Before you begin creating your application, you open Visual Studio and examine the interface. Perform the following steps:
| 1. |
Select Start > Programs > Microsoft Visual
Studio 2005> Microsoft Visual Studio 2005.
|
| 2. |
Select View > Oracle Explorer.
|
| 3. |
Click the + icon to add a connection.
|
| 4. |
Enter your Data source name. Enter
hr for the User name and
Password, click Save password, select Default
as Role, and click Test connection.
|
| 5. |
Test connection was successful. Click OK.
|
| 6. |
Click OK.
|
| 7. |
Click the + in front of Data Connections in the
Oracle Explorer view. The hr.ORCL connection has been created.
|
Back to Topic List
Now you will create a new table called DEPENDENTS which has a foreign key with the EMPLOYEES table.
Back to Topic List
Create the Columns
To create the columns in the table, perform the following:
| 1. |
Expand hr.ORCL. Right-click Tables
and select New Relational Table...
|
| 2. |
Enter DEPENDENTS
for Table name and click Add in the Columns section.
|
| 3. |
Enter FIRSTNAME
for the Name, select VARCHAR2 for the Data type and enter
30 for the Size. Then click Add.
|
| 4. |
Enter LASTNAME
for the Name, select VARCHAR2 for the Data type and enter
30 for the Size. Then click Add.
|
| 5. |
Enter BIRTHDATE
for the Name, select DATE for the Data type. Then click
Add.
|
| 6. |
Enter RELATIONSHIP
for the Name, select VARCHAR2 for the Data type and enter
20 for the Size. Then click Add.
|
| 7. |
Enter EMPLOYEEID
for the Name, select NUMBER for the Data type, deselect
Allow null and enter 6 for the Precision and
0 for the Scale. Then click Add.
|
| 8. |
Enter DEPENDENTID
for the Name, select NUMBER for the Data type, deselect
Allow null and enter 6 for the Precision and
0 for the Scale. Then click Preview SQL.
|
| 9. |
Here is the SQL that will execute when the table is
created. Click OK.
|
| 10. |
Click Save. Note: the Preview SQL is
no longer available since the statement has executed.
|
| 11. |
The Output window is displayed. The DEPENDENTS table
has been created. Close the Output window.
|
| 12. |
Click the + in front of DEPENDENTS table in the
Oracle Explorer view. You can view the columns that have been created
in this topic. Now you are ready to create the index.
|
Back to Topic
Create the Index
To create the index for the table, perform the following:
| 1. |
Click the Indexes tab.
|
| 2. |
Click Add.
|
| 3. |
Enter DEPENDENTS_INDX
for the Name and click Add in the Index Keys area.
|
| 4. |
Select DEPENDENTID for
the Key and then click Save.
|
| 5. |
The Output window is displayed. The DEPENDENTS_INDX
index has been created. Close the Output window.
|
Back to Topic
Create the Foreign Key
To create the foreign key for the table, perform the following:
| 1. |
Click Constraints tab.
|
| 2. |
Click Add.
|
| 3. |
Enter EMPLOYEES_FK for the Name, select Foreign Key for the type, select EMPLOYEES for the table, and select the constraint EMP_EMP_ID_PK. Select EMPLOYEE_ID as the Referenced Column and EMPLOYEEID as the local column. Then click Add.
|
| 4. |
Enter DEPENDENTS_PK
for the Name, select Primary Key for the type, and click
Add in the Primary key columns area.
|
| 5. |
Select DEPENDENTID for
the primary key column. Then click Save. Close
the Output window.
|
| 6. |
Click the + in front of Indexes and Constraints
in the Oracle Explorer view. You can see the table, its indexes and constraints.
|
Back to Topic
You can access the data in a table by performing the following
| 1. |
Right-click DEPENDENTS table and select Retrieve Data...
|
| 2. |
Enter at least 4 lines of data as shown in the table
below. Make sure you enter a valid Employee ID. Then click Save.
|
FIRSTNAME
|
LASTNAME
|
BIRTHDATE
|
RELATIONSHIP
|
EMPLOYEEID
|
DEPENDENTID
|
|
Mary
|
Martin
|
06-MAY-80
|
daughter
|
104
|
9999
|
|
Sue
|
Littlefield
|
12-JUL-68
|
daughter
|
130
|
8888
|
|
David
|
Griffiths
|
02-APR-97
|
son
|
104
|
7777
|
|
Aaron
|
Young
|
31-AUG-99
|
son
|
111
|
6666
|
|
Back to Topic List
In this topic, you will create a project, and generate the code to show the data in the DEPENDENTS table. Perform the following steps:
| 1. |
Click Start Page tab.
|
| 2. |
Click Project... adjacent to Create:
in the Recent Projects area.
|
| 3. |
Select the Project Type Visual C#,
select the Template Windows Application, enter obe1
in the name and enter the directory where you want the files stored (Note:
if the directory does not exist, it will be created). Then click OK.
|
| 4. |
Select the DEPENDENTS table in the
Oracle Explorer view and drag and drop it into Form1.
|
| 5. |
Click Yes to save the connection password
in the code that is generated.
|
| 6. |
The icons you now see just below your form represent
code that has now been automatically generated for you. Specifically an
OracleDataAdapter class has been generated which is part of the Oracle
Data Provider for .NET. Double-click dependentsOracleDataAdapter1
to view the code.
|
| 7. |
The form will appear with the code that was generated.
|
| 8. |
Select InitializeComponent () from the drop-down
menu.
|
| 9. |
The OracleDataAdapter is based on the ADO DataAdapter
which is designed to make it easy to send data to and from your database
and widgets on your form, with a minimal amount of code. The OracleDataAdapter
class is provided by the Oracle Data Provider for .NET which was installed
when you installed the Oracle Developer Tools. Click Form1.cs
(Design)* tab.
|
| 10. |
Click anywhere next to the classes
so they become deselected (no box around them).
|
| 11. |
Right-click dependentsOracleDataAdapter1
and select Generate Dataset...
Dependents11 is displayed.
|
| 12. |
In order to be able to display
the data in the Dependents table in your application, you need to add
a DataGridView to the form which is located in the Toolbox.
Select View then Toolbox.
|
| 13. |
Select DataGridView from the list of
Windows Forms and drag and drop it on your Form1.
You may see a pop up window asking you to set the data
source property. If you do, you can skip ahead to step 17, otherwise
follow the instructions below to open this window.
|
| 14. |
Close the Toolbox view.
|
| 15. |
Now you need to set the Data Source of the Grid to
the Oracle Data Source. Select Properties Window from
the View pulldown. This step maybe optional if your properties
window is already open.
|
| 16. |
Scroll down to the Data>DataSource section
in the Properties window and click (none).
|
| 17. |
Pull down the DataSource drop-down menu and click
the + in front of Other Data Sources.
|
| 18. |
Click the + in front of Project Data Sources.
|
| 19. |
Click the + in front of Dependents1.
|
| 20. |
Click Dependents and click the Save icon.
|
| 21. |
Expand Form 1 and DatagridView as shown
in the screenshot. Double-click Datagridview to view the form with
code that was generated.
|
| 22. |
Select DataGridView1 from the list
of Members.
|
| 23. |
Select Form1() from the drop-down menu.
|
| 24. |
Enter the following code after the InitializeComponent();
this.
You will see a list of valid options to select from.
Select dependentsOracleData Adapter1. Then type or select
the rest of the code as follows:
this.dependentsOracleDataAdapter1.Fill(this.dependents11.Dependents);
|
| 25. |
Select Build > Build Solution.
|
| 26. |
Check your output area to make sure you didn't receive
any errors. Close the Output window. Then select Debug
> Start Debugging.
|
| 27. |
Your application runs and a form appears. Your form
has been populated with data. Review your data. Then close the window.
Close the Properties window.
|
Back to Topic List
Now you will create a stored procedure and run it.
Back to Topic List
Create a Stored Procedure
| 1. |
From the Oracle Explorer view, right-click Packages
and select New Package...
|
| 2. |
Accept the default Package name and click Add under Methods.
|
| 3. |
Enter GETCURSORS for the method name, select Procedure for the method type and click Add under Parameters.
|
| 4. |
Enter MAXROWS for the Name and click Add.
|
| 5. |
Enter EMPLOYEESCUR for the Name, select OUT for Direction, and select SYS_REFCURSOR for Data type. Then click Add.
|
| 6. |
Enter DEPENDENTSCUR for the Name, select OUT for the Direction and select SYS_REFCURSOR for the Data type. Then click OK.
|
| 7. |
Click Preview SQL >> to see the SQL that will be executed.
|
| 8. |
Review the SQL and click OK.
|
| 9. |
Click OK to create the Package. Close
the Output window.
|
Back to Topic
Edit the Package Body
| 1. |
The Package is created and the Package Body is opened.
If you do not see the code that is generated for the Package Body, right-click
PACKAGE1 Package in the Oracle Explorer and select
Edit Package Body. Scroll down to find the line NULL;
|
| 2. |
Replace the line NULL; with the following:
OPEN EMPLOYEESCUR FOR SELECT * FROM EMPLOYEES;
OPEN DEPENDENTSCUR FOR SELECT * FROM DEPENDENTS;
|
| 3. |
Right-click the window tab and select Save.
Close the Output window.
|
Back to Topic
Run the Stored Procedure
| 1. |
Expand the Package PACKAGE1 in the Oracle Explorer view. Right-click GETCURSORS and select Run.
|
| 2. |
Enter 999 for the MAXROWS parameter value and click OK.
|
| 3. |
Select the value for EMPLOYEESCUR.
|
| 4. |
The details for the EMPLOYEESCUR
are shown. Select the value for the DEPENDENTSCUR.
|
| 5. |
The DEPENDENTSCUR details are shown. Close the
Output window.
|
Back to Topic
The query window allows you to run one or queries against data in your database. Perform the following steps:
| 1. |
Right-click your database connection Hr.ORCL
and select Query Window.
|
| 2. |
Enter the SQL command SELECT
* FROM DEPENDENTS; and click Execute.
|
| 3. |
Your results are shown. To format the results, right-click
in the query area, select Set Query Output > Text and
click Execute again.
|
| 4. |
The results are shown in text format.
|
| 5. |
You can execute more than one SQL statement at a time.
Add the SQL command SELECT * FROM EMPLOYEES;
In order for both statements to be executed at the same time, you need
to select both statements, then click Execute. Note:
if both statements are not selected, only the last SQL statement in the
list will be run.
|
| 6. |
Scroll up to the top of your output. You see the data
from both queries.
|
Back to Topic List
In this lesson, you learned how to:
 |
Create a table and view the data |
 |
Automatically generate code |
 |
Create and run a stored procedure |
|