Oracle Magazine Issue Archive
2005
November 2005
DEVELOPER: WindowsTools for Your .NET SpaceBy Christian Shay Use ODT for fast, easy development for Oracle Database on Windows. You're working on some C# or VB.NET routines that manipulate data you're retrieving from Oracle Database. You need to access the database to edit and test some stored procedures, modify some data in a table, change the structure of a table or view, or perform one of countless other database tasks. In the past, you've managed this by using a tool such as SQL*Plus or Toad, but you wish there were a toolset that would let you do all of this from directly inside of Microsoft Visual Studio .NET. And you wish this toolset could include graphical designers and wizards that do the grunt work of figuring out the SQL that's needed for a particular database operation. And maybe this toolset could even automatically generate some of the C# or VB.NET data access code, as well. Your wishes have been granted: With the release of Oracle Developer Tools for Visual Studio .NET (ODT), all of this is now a reality. In this article, I'll give you a quick tour of many of the features that ODT has to offer. I'll also introduce the new .NET stored procedures feature in Oracle Database 10g Release 2 for Windows and show how you can use ODT to deploy a .NET stored procedure into Oracle Database. OverviewODT is a tightly integrated add-in for Visual Studio .NET. ODT, first released in June 2005, is free and available for download from the Oracle Technology Network. Major features of ODT include
Oracle Explorer: Viewing the Oracle SchemaWhen you need to find and examine a table, view, or other Oracle database object, the first place to go is to the Oracle Explorer. To launch the Oracle Explorer, from the main menu, choose View->Oracle Explorer . The Oracle Explorer, shown in Figure 1, is a tree control that lets you view the structure of the Oracle schema. You can view Oracle schema objects including tables, views, packages, and stored procedures here. When you select a schema object, the Visual Studio .NET properties pane displays metadata such as column datatypes or stored procedure parameter types.
A context menu for each schema object offers additional features: You can generate SQL scripts for schema objects, use the provided filters to limit what types of schema objects are shown, and run designers and wizards to alter the schema. For example, while viewing the structure of a table, you can use the context menu to run the Oracle Table Designer to modify the design of the table. Designers and Wizards: Simplifying Database OperationsODT offers a variety of graphical designers and wizards to provide step-by-step help to create or alter database objects including tables, views, stored procedures, stored functions, PL/SQL packages, sequences, indexes, constraints, triggers, synonyms, and more. To launch the Table Designer, right-click the table in the Oracle Explorer, and choose Design . The Table Designer, shown in Figure 2, makes looking up datatypes or memorizing SQL syntax a thing of the past. You simply provide the table name and the names of the columns, choose the appropriate datatype from a drop-down list, and click Save to create or alter the table. The newly created schema object will then immediately appear in the Oracle Explorer tree control.
Oracle Data Window: View and Modify Table DataTo quickly see table or view data, right-click the table or view in the Oracle Explorer and choose Retrieve Data . The Oracle Data window, shown in Figure 3, appears and displays table or view data in an easy-to-read grid and lets you insert, update, or delete data without leaving the Visual Studio .NET development environment. When you modify a row, the cursor moves to a new row, and the data is immediately committed. For large tables you can provide a row number to navigate to a particular row.
PL/SQL EditorTo edit a procedure, function, or, package, right-click the item in the Oracle Explorer and choose Edit . The PL/SQL editor appears, as shown in Figure 4, and provides a tightly integrated development environment: You don't need to leave Visual Studio .NET to develop stored procedures. Standard Microsoft Visual Studio features in the PL/SQL Editor include syntax coloring to improve readability and collapsible regions to hide stored procedures or functions that are part of a large, complex package. This makes it easier to focus on the procedure or function that you're working on. Additionally, as you enter SQL or PL/SQL statements, a drop-down list of tables or columns will pop up to auto-complete the statement.
To compile the PL/SQL code, right-click the item in the Oracle Explorer and choose Compile . Any errors found when you compile the PL/SQL are listed in the Microsoft Visual Studio .NET task list. Click on the error text to take you to the line of code that failed. Press the context-sensitive Help key to open the Error Messages and Codes manual for the error code in question. To test complete stored procedures and functions, right-click the item in the Oracle Explorer and choose Run . A dialog box will appear where any required input parameters for the stored procedure or function can be entered. Click OK in the dialog box to execute the code. The results of running the stored procedure—the output parameters—are displayed in a final window, shown in Figure 5. Complex output types, such as REF CURSOR, are shown as links; click these links to open up grids containing the complex data.
Automatic Code GenerationWhen you drag and drop a schema object from the Oracle Explorer onto an application's form, code is automatically generated, for SELECT , UPDATE , INSERT , and DELETE operations on that schema object. The code uses the OracleDataAdapter class, which is provided by Oracle Data Provider for .NET (ODP.NET). This class derives from the ADO DataAdapter class. Anyone who has worked with DataAdapter classes in the past, even with other database vendors, will be familiar with this. You can then use the DataAdapter class to connect UI elements (such as a DataGrid element) on the application's form to Oracle Database—with minimal additional coding. Code Listing 1: getDepartmentno stored function
using System;
using System.Data;
using Oracle.DataAccess.Client;
namespace Project1
{
/// <summary>
/// Summary description for Class1.
/// </summary>
public class Class1
{
public static int getDepartmentno(int empno)
{
int deptno = 0;
// Get a connection to the db
OracleConnection con = new OracleConnection();
con.ConnectionString = "context connection=true";
con.Open();
// Create and execute a command
OracleCommand cmd = con.CreateCommand();
cmd.CommandText = "select deptno from emp where empno = :1";
cmd.Parameters.Add(":1", OracleDbType.Int32, empno,
ParameterDirection.Input);
OracleDataReader rdr = cmd.ExecuteReader();
while(rdr.Read())
deptno = rdr.GetInt32(0);
rdr.Close();
cmd.Dispose();
// Return the employee's department number
return deptno;
}
}
}
You can also generate code for a typed or untyped dataset based on an Oracle table or view. To generate the code, right-click the OracleDataAdapter icon on your form and choose Generate Dataset . You can then bind this dataset as a data source to UI elements or use it in other standard ways throughout Microsoft Visual Studio .NET, such as with the Microsoft Visual Studio XML Schema designer. Oracle Database Extensions for .NET: Your First .NET Stored FunctionWith Oracle Database 10g Release 2 on Windows, Oracle now offers Oracle Database Extensions for .NET. This database option lets you deploy stored procedures and functions written in a .NET-managed language into Oracle Database on Windows. This option is not installed by default; you'll need a custom install of the database to enable it. (For more information on setting up your database to run .NET stored procedures, please see the Oracle Database Extensions for .NET Developer's Guide .) This database option is available only on the Windows platform; those requiring a platform-neutral solution should continue using PL/SQL and Java.
Follow these steps to write and deploy your first .NET stored function: Step 1: Choose File->New->Project-> C# Projects->Oracle Project to open an Oracle Project.
Step 10: Press Finish to deploy the assembly and register the stored function with Oracle Database. You should now see the GETDEPARTMENTNO function listed under the Functions node of the Oracle Explorer. SummaryThis article presented the major features of Oracle Developer Tools for Visual Studio .NET, which makes .NET development with Oracle Database faster and easier. The article also walked through the steps to develop and deploy a .NET stored function into Oracle Database 10g Release 2 on Microsoft Windows. Christian Shay (christian.shay@oracle.com) is a principal product manager at Oracle. |