Asp. NET access to Oracle databases

Source: Internet
Author: User
Tags exception handling insert ole oracleconnection first row tostring oracle database
asp.net|oracle| Access | data | Database Introduction

The Microsoft. NET Framework Data Provider for Oracle (named. NET for Oracle) is a component of the. NET Framework. This component is used for us. NET access to Oracle databases provides great convenience. Those use. NET and Oracle developers, believe that they will be happy to dance, because they no longer need to use that is not very "professional =" OLE DB to access the Oracle database. The design of this component is very similar. NET, the Microsoft. NET Framework Data Provider for SQL Server and OLE DB are built into. If the reader is familiar with both of these built-in components, it is also easy to believe that you are learning this component.

This article is intended for the reader mainly those who consider using. NET technology to access the Oracle database programmer and write, need to have a certain C # language, ADO. NET Technology and Oracle database basics. In this paper, the relevant examples and specific annotations are given with the asp.net technique. Of course, this does not mean that a. NET for Oracle component can only serve to write asp.net programs, but it also provides convenience for Windows programs written with. NET technology.

This article will briefly describe the system requirements and installation and core classes of asp.net for Oracle, and then focus on the methods used to access the Oracle database using this component. This includes a. NET for Oracle access to special data types in various Oracle databases, introduction to various core class usage methods, and examples at the end of the article.

--------------------------------------------------------------------------------

System Requirements and Installation

Before you install. NET for Oracle, you must first install the. NET Framework version 1.0. Also, be sure to install the Data Access component (MDAC 2.6 and above, and the recommended version is 2.7). Since you are accessing data from an Oracle database, you will also need to install Oracle 8i Release 3 (8.1.7) client and above. At present, Oracle9i has been released, the author of the installation of Oracle 9i, all of the procedures in this article are written in the Oracle9i database environment and debugging completed.

The assembly is easy to install and runs directly oracle_net.msi. In the installation process without any setup, click "Next=" to complete the installation. The default installation is to create a folder named Oracleclient.net under the C:\Program files\ microsoft.net directory, which contains the following six files, which are described in detail in the following table:

Note: The Mtxoci8.dll file is not installed in the default folder, but is installed in the system directory, such as the C:\Windows\System32 directory.

For developers, it is important to System.Data.OracleClient.dll files. This is the core file for the. NET for Oracle component. When used, developers can use the. NET for Oracle component by installing Oracle_net.msi, which is used as a default component of the system. It's like we're familiar with the System.Data.SqlClient and System.Data.OleDb components. However, it is important to note that when the developer has completed the program and distributed it to the user, for the sake of ease of use of the software, we do not want to install Oracle_net.msi as a developer before the user uses the software. The developer can then copy the System.Data.OracleClient.dll file to the software's Bin directory before publishing. This eliminates the need for users to install Oracle_net.msi and use the functionality provided by the software normally. (This approach is limited to programs that are developed and do not involve distributed transactions)

--------------------------------------------------------------------------------

Core class Introduction

The name space used in the. NET for Oracle component for organizing classes and other types is System.Data.OracleClient. In this namespace, there are four core classes, respectively: OracleConnection, OracleCommand, OracleDataReader, OracleDataAdapter. If developers are familiar with Ado.net technology, the use of these four classes will be familiar. The content is very simple, and its use is almost exactly the same as SqlConnection, SqlCommand, SqlDataReader, SqlDataAdapter. This is no longer explained in detail, and the reader will learn about the use of these classes in the following article, giving only the following table for the reader's brief understanding.

--------------------------------------------------------------------------------

Give an example to explain

The following is an example of manipulating an Oracle database using a. NET for Oracle component. Before writing a program, create a table in the Oracle database and add a row of data. Use the following statement.

Create a table named Oracletypestable
"CREATE Table Oracletypestable (MyVarchar2 varchar2 (3000), MyNumber number (28,4)
Primary key, MyDate Date,myraw RAW (255)) ";
Insert a row of data
INSERT into oracletypestable values (' Test ', 4,to_date (' 2000-01-11
12:54:01 ', ' yyyy-mm-dd hh24:mi:ss '), ' 0001020304 ');
The following program uses the. NET for Oracle component to access the Oracle database and displays this row of data. In the program, notice the class described in the previous article, and associate it. NET in the use of data processing classes.

1. Using System;
2.using system.web;
3.using System.Web.UI;
4.using System.Web.UI.HtmlControls;
5.using System.Web.UI.WebControls;
6.using System.Data;
7.using System.Data.OracleClient;

8.public class Pic2:page {
9. public Label message;
public void Page_Load (Object Sender,eventargs E)
11. {
Set connection string
A. String connstring= "Data source=eims;user=zbmis;password=zbmis;";
Instantiating a OracleConnection object
OracleConnection conn=new OracleConnection (connstring);

Try
15. {
Conn. Open ();
Instantiating a OracleCommand object
OracleCommand Cmd=conn. CreateCommand ();

cmd.commandtext= "SELECT * from Zbmis. Oracletypestable ";
OracleDataReader Oracledatareader1=cmd. ExecuteReader ();
Reading data
while (Oracledatareader1. Read ()) {
Reads and displays data for the first row of the first column
OracleString Oraclestring1=oracledatareader1. Getoraclestring (0);
Response.Write ("OracleString" +oraclestring1. ToString ());

Read and display data from the second column of the first row
OracleNumber Oraclenumber1 =oracledatareader1. GetOracleNumber (1);
Response.Write ("OracleNumber" +oraclenumber1. ToString ());

Read and display data from the first row, third column
OracleDateTime Oracledatetime1=oracledatareader1. Getoracledatetime (2);
Response.Write ("OracleDateTime" +oracledatetime1. ToString ());

Reads and displays data from the first row, column fourth
OracleBinary Oraclebinary1=oracledatareader1. Getoraclebinary (3);
if (oraclebinary1. Isnull==false)
29. {
foreach (Byte b in Oraclebinary1. Value)
31. {
Response.Write ("Byte" +b.tostring ());
33.}
34.}
35.}
Releasing resources
Oracledatareader1. Close ();
37.}
catch (Exception ee)
39. {
Exception handling
Message. Text=ee. message;
41.}
Finally,
43. {
Close connection
Conn. Close ();
45.}
46.}
47.}
If you have a. NET in the data manipulation of the content is very familiar with, then believe that the above procedure is completely understand. So it's not very important to analyze the above code here.

Please use those both. NET also uses Oracle's readers to remember that. NET for Oracle components are designed very similar to the data Provider for SQL Server and OLE DB built in. Net




Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.