Making Best Out of Oracle Databases Using Odp.net - Part I

xiaoxiao2021-03-06  73

Great Dilemma - Vendor Specific or Portable Code

The issue of using generic oledb.net, odbc.net layers vs. vendor specific providers has always been a debate during the development of data access layers. While the generic code will avoid vendor lockin and improve portability it suffers in terms of performance and in using the advanced features of the particular database. Oracle continues to be a dominant force in the database market. Oracle has released its latest version of .net provider in the form of ODP.net version 10g. ODP.net provides a flexible, faster, Optimized and more stable access to oracle Databases from .Net. This Series Tries To Cover The Specific Features of Oracle Database As Used by ODP.NET.

Implementation & usage of odp.net

The ODP.net can be installed as part of oracle server installations as well as stand alone client installations. You need the oracle client 9.2 or later to install ODP.net on top of it. The examples mentioned in this document were tested using ODP. Net 10.1.0.2.0 Which Coms as Part of Oracle 10g Server Installation.

.................................................. ...

CSC% 1.cs /R: C :/oracle/product/10.1.0/do.net/bin/oracle.DataAccess.dll

Substitution C: /Oracle/Product/10.1.0/dotnet with an appropriate Oracle Home for your installation.

The Following Namespaces Needs to Be Added To your application.

Using Oracle.DataAccess.Client; Contains Odp.Net Classes and enums

Using Oracle.DataAccess.types; Contains ODP.NET TYPES

To connect using odp.net.

OracleConnection Con = New OracleConnection ();

Con.comtring = "User ID = Scott; Password = Tiger; Data Source = DOTNET;" Now Let Us Concentrate On The Individual Case Studies.

Generating XML from RELATIONAL TABLES:

Generating XML out of relational data nowadays has been a routine task in database applications for different cases. SQL Server 2000 has been supporting it in the form FOR XML queries. Oracle has been supporting this feature with different PL / SQL functions. The following implementation of ODP.NET ACHIEVES XML OUTPUT OF RELATIONAL DATA Using XMLCommandType Property ORACLE COMMAND Object. Now Look at The Code Piece.

OracleCommand cmd = new oracleCommand (", con);

cmd.comMandText = "SELECT * FROM EMP";

Cmd.xmlcommandtype = oraclexmlcommandtype.query;

XmlReader Dr = cmd.executexmlreader ();

XmLDocument Doc = New XmLDocument ();

Doc.preservewhitespace = true;

Doc.Load (DR);

System.console.writeline (Doc.outerxml);

The Output Generated Will Be As Follows:

7369

smith

Clerk

7902

17-DEC-80

800

20

.

.

Now Let Us Play Around a bit to change the default structure of the xml document useing the xmlqueryproperties.

cmd.xmlQueryProperties.Roottag = "EMPLOYEE";

cmd.xmlQueryProperties.rowTag = "EMP";

The Output Will Be Transformed Into:

7369

smith

Clerk 7902

17-DEC-80

800

20

DML (data manipulation) Using XML.

Now let us perform an insert INTO The EMP TABLE Using A XML Document As Source.

Predictably XmlsaveProperties IS Used to set the property of the save commist.

OracleCommand cmd = new oracleCommand (", con);

cmd.xmlcommandtype = oraclexmlcommandtype.insert;

cmd.commandtext = " / n"

/ N "

/ N "

" 9999 / N"

" Ian thrope / N"

" manager / n"

" 7902 / n"

800 / N "

20 / n "

" / N"

" / n";

String [] Keycols = new string [1];

Keycols [0] = "EMPNO";

String [] UPDCOLS = New String [6];

Updcols [0] = "EMPNO";

Updcols [1] = "ename";

Updcols [2] = "job";

Updcols [3] = "mgr";

Updcols [4] = "sal";

Updcols [5] = "deptno";

cmd.xmlsaveproperties.rowtag = "emprow";

cmd.xmlsaveproperties.table = "EMP";

Cmd.xmlsaveproperties.UpdateColumnslist = Updcols;

Cmd.xmlsaveProperties.KeyColumnslist = keycols;

Cmd.xmlsaveProperties.xslt = NULL;

cmd.xmlsaveproperties.xsltparams = null;

INT ROWS = 0;

Rows = cmd.executenon query ();

Console.writeline ("Rows Inserted" Rows;

The above code piece sets an XMLDocument as a command type and sets the key, update columns and the table to be updated. Execution of the command will result in an insert of a new row to the emp table. Similar logic can be applied to delete Update Using OracleXmlcommandType.Update, OracleXmlcommandType.delete.Applying Transformations

While it may look like transforming XML using XSLT looks like a job at the front end some times transforming one XML format to another XML can still be required at the database or middle tier itself. OracleXML extensions provided in ODP.net can facilitate this. Let US Look Into The Following Example.

Assuming that your company outsources the calculation of taxes to an external firm and the external firm wants the employee records in a different format where by employee's SAL and COMM has to be grouped under a new element called CTC. Let us apply the transformation using the following STYLE Sheet.

String controlttotaxformat = " / n"

/ n "

/ N "

/ N "

/ N "

/ n "

" / n"

/ n "

/ n "

" / n"

/ n " " / N "

" / N"

/ n "

"";

OracleCommand cmd = new oracleCommand (", con);

cmd.comMandText = "SELECT * FROM EMP";

Cmd.xmlcommandtype = oraclexmlcommandtype.query;

cmd.xmlQueryProperties.Roottag = "EMPLOYEE";

cmd.xmlQueryProperties.rowTag = "EMP";

cmd.xmlqueryproperties.xslt = ConvertTotaxFormat;

XmlReader Dr = cmd.executexmlreader ();

XmLDocument Doc = New XmLDocument ();

Doc.preservewhitespace = true;

Doc.Load (DR);

System.console.writeline (Doc.outerxml);

The Output Will Look Like The Following.

7369

smith

800

80

Clerk

20

7499

Conclusion

Oracle provider for .net has many features which could be used while writing vendor specific code. This document explains the XML related features and I am planning to write about other features in the future articles.

转载请注明原文地址:https://www.9cbs.com/read-120147.html

New Post(0)