.NET CLR stored procedures within Oracle database: Another breaking revolution

In this article, I shall introduce you to developing, deploying and testing a .NET based CLR stored procedure with Oracle database using Visual Studio.NET.

Contributed by
Rating: 5 stars5 stars5 stars5 stars5 stars / 10
December 06, 2005
Rate this Article:
MEH MEH++


SEARCH ASP FREE
TOOLS YOU CAN USE

advertisement

A downloadable file for this article is available here.

Powerful databases for “extended” Stored Procedures

I am already contributing a series on “Oracle Database Interaction Using ODP.NET and ASP.NET.”  While working on the series, I happened to look into the details of new features introduced in Oracle 10g Release 2.  It is really fascinating, so I decided to come up with an introductory article on one of the most important features introduced with Release 2.

Every programmer would definitely agree that “Stored Procedures” are very important at the data access tier, as they boost the performance of applications to the optimum level.  Almost every RDBMS existing today supports stored procedures in one way or another.

To work with stored procedures for a particular RDBMS, we need to study and work with the “native” database-supported data access language (with its own syntax and semantics).  Examples include T-SQL (or Transact SQL) for Microsoft SQL Server 2000, PL/SQL for Oracle, and so on.  That is already an old story.  Day by day, major RDBMS products are trying to expand beyond the “nativeness,” providing flexibility and integration with other products, along with high security and performance.

The trend of “stored procedures” expanded even to the capability of supporting “external language” based stored procedures!  Even though it is good news for the programmers, it would be a great challenge for database administrators and database designers or architects from now on.  Just imagine how much time they need to spare for learning and working with the “external languages.”

As I am trying to focus on the Oracle database in this article, let us examine its capabilities.  Everyone knows that Oracle database supports native stored procedures with the help of PL/SQL. Oracle started supporting Java (external language) stored procedures from Oracle version 8i onwards.  And now, it has further expanded its capability, even to the .NET based CLR stored procedures with Oracle Version 10.2 (Version 10g with Release 2)! 

Now you can develop stored procedures in any .NET compliant languages (like VB.NET, C#, and so on) and deploy them in an Oracle database, as if they were native stored procedures.  Best of all, the development and deployment has been made quite simple, as Oracle released tools for Visual Studio.NET IDE, which are tightly integrated directly with Oracle database.  Now the stored procedures can be developed with the “intellisense” features of Visual Studio.NET

Another RDBMS, Microsoft SQL Server 2005 (which was scheduled to launch in November 2005) has already started supporting “.NET CLR stored procedures” (along with T-SQL stored procedures).  I contributed an article (very long ago) on developing and deploying a .NET CLR stored procedure using Visual studio.NET 2005 at the following link:

http://www.aspfree.com/c/a/MS-SQL-Server/Implementing-Managed-code-in-SQL-
Server-2005-using-Visual-Studio-NET-2005/

Okay. Now let us start by connecting to Oracle 10g using Visual Studio.NET.

Connecting to Oracle 10g (Release 2) from Visual Studio.NET using Oracle Explorer

Before proceeding further with this article, make sure that all of the following software is properly installed and configured on your system:

Let us go through the following steps to connect to Oracle 10g using Visual Studio.NET 2003:

  • Open Visual Studio.NET 2003 IDE
  • Go to menu View -> Oracle Explorer as shown in Figure 1
  • Within the Oracle Explorer, right click on Data Connections -> Add Connection (Fig2)
  • Within the “Add Connection” window, provide the details of “Data Source”, “User Name” and “Password” (as shown in Figure 3).  You can test the details by clicking on “Test Connection” button.
  • Once you click “OK”, Oracle Explorer gets updated as shown in Figure 4.

“Data Source” will be generally the global SID or “Local net Service Name” you configured during Oracle server/client installation.  For this demonstration, I am using the “scott” schema (sample schema provided by Oracle) with the password “tiger”.

Once you get connected, you can play with all the items existing within the “Oracle Explorer” along with creating tables, inserting, modifying, deleting, dropping, designing relations, stored procedures (using PL/SQL), and so on. Enjoy playing with it.

Developing an Oracle based .NET CLR stored procedure using Visual Studio.NET

Once you complete all of the steps in previous section, proceed with the following steps to develop a .NET CLR based stored procedure using Visual Studio.NET.

  • Go to File -> New -> Project
  • Within the “New Project” window, select “Visual Basic Projects” as Project Type “Oracle Project” as template, Name as “CLRSample” and click “OK” (shown in Fig5).
  • Modify the code as follows:

Imports Oracle.DataAccess.Client
Imports Oracle.DataAccess.Types

Public Class Class1

    Public Shared Sub IncrementSalary(ByVal empno As Integer, ByVal incrementValue As Double)
        ' Add code here.
        Dim conn As New OracleConnection("context connection=true")
        conn.Open()
        Dim cmd As OracleCommand = conn.CreateCommand
        cmd.CommandText = "UPDATE scott.emp SET sal = sal + " & incrementValue & " WHERE empno = " & empno
        cmd.ExecuteNonQuery()
        cmd.Dispose()
        conn.Close()
    End Sub
End Class
  • Press Ctrl+Alt+L to open “Solution Explorer.”
  • Right click on “CLRSample” project and select “Deploy” (shown in Figure 6).
  • Skip the welcome screen by clicking on “next.”
  • Select the “data connection” you already created (you can even create a new connection by clicking on the “New Connection” button) and click “next.”
  • In the next screen select the “Copy assembly and generate stored procedures” option and click “next” (shown in Figure 7).
  • Accept the defaults in the next screen (shown in Figure 8) and click “next.”
  • Accept the defaults in the next screen (shown in Figure 9) and click “next.”
  • Select all checkboxes in the “Available methods” (shown in Figure 10) and click “finish.”

Testing an Oracle based .NET CLR stored procedure using Visual Studio.NET

Once you click on “finish” button (from the previous section), you should be able to see the output window of Visual studio.NET as something like the following figure 11.

To execute the .NET CLR stored procedure from within Visual Studio.NET, open “Oracle Explorer” from the “View” menu, open “procedures” of “scott” schema and right click on “incrementsalary” as shown in Figure 12.

In the “Run Procedure” dialog, provide value for “empno” as 7839 and “incrementvalue” as 500 (as shown in Figure 13) and click OK.

Once it is successfully executed, it should display something like the following Figure 14.

Now you can check your “emp” table in the database, and “KING” gets his salary incremented from 5000 to 5500!

Testing an Oracle based .NET CLR stored procedure using “iSQL*Plus”

Open any browser and go to “http://server:port/isqlplus”. Provide “Username”, “password” and “connect identifier” according to your configuration (as shown in Fig 15) and click “login”.

Once it opens workspace, just type the following code and press “execute”.

Begin

    IncrementSalary(7839, 500);

End;

You should see the output something like the following Figure 16.

That’s it.  Now you can check your “emp” table, which gets updated accordingly.  Actually, the above three lines are nothing but some simple PL/SQL statements, generally used to call (or execute) any stored procedure stored in an Oracle database.

Summary

When writing stored procedures, triggers, and user-defined functions, one decision you must make is whether to use traditional PL/SQL, or a .NET Framework language such as Visual Basic .NET or Visual C# .NET (or even Java). Use PL/SQL when the code will mostly perform data access with little or no procedural logic. Use .NET CLR based (or Java based code) for CPU-intensive functions and procedures that feature complex logic, or when you want to make use of the BCL of the .NET Framework (or Java API).

Another important issue to consider before comparing the two major RDBMS, MS SQL Server 2005 and Oracle 10g, is that Microsoft SQL Server 2005 supports CLR integration based on .NET version 2.0, whereas Oracle 10.2 supports only .NET version 1.1 (at the time of this writing).

Even though I examined only a stored procedure in the above scenario, you can embed .NET CLR stored procedures in PL/SQL Packages also.  Having trails with .NET CLR based stored functions, getting them under the packages, and so on, would be enhancements. Check this section regularly for new articles on the same concept.

I developed this entire solution (downloadable here or at the beginning of this article) using Microsoft Visual Studio.NET 2003 Enterprise Architect on Windows Server 2003, standard edition, running Oracle database Version 10g Release 2.  I could not go in depth into the CLR details (or even ADO.NET using ODP.NET) as it is beyond the scope of this article.  But, if you really need them, you can go through my list of articles related to that subject within this site.

I leave it to the programmers for further enhancements. Any doubts, comments, suggestions, bugs, errors or feedback are welcomed at jag_chat@yahoo.com 

blog comments powered by Disqus
.NET ARTICLES

- .Net 4.5 Brings Changes
- Understanding Events in VB.NET
- Objects, Properties, Events and Methods in V...
- Install Visual Web Developer Express 2010
- Microsoft Gadgeteer an Open Source Alternati...
- Best DotNetNuke Modules
- Facebook Image Viewer in Visual Basic
- Murach`s ADO.NET 4 Database Programming with...
- 5 Must Have Visual Studio 2010 Extensions
- Dynamic Web Applications with ASP.NET Mono u...
- PDFSharp: HTML to PDF in ASP.NET 3.5 using V...
- Using the PDFSharp Library in ASP.NET 3.5 wi...
- Sending Email in ASP.NET 3.5 using VB.NET wi...
- ASP.NET 3.5 Role Based Security and User Aut...
- Creating ASP.NET Login Web Pages and Basic C...

ASP Web Hosting ASP.Net Web Hosting Windows Web Hosting
ASP Free Forums 
 RSS  Tutorials RSS
 RSS  Forums RSS
 RSS  All Feeds
Site Map 
Request Media Kit
Write For Us Get Paid 
Weekly Newsletter
 
Developer Updates  
Free Website Content 
Privacy Policy 
Support 


© 2003-2012 by Developer Shed. All rights reserved. DS Cluster 2 - Follow our Sitemap
Most Popular Topics
All ASP.Net Tutorials