Introduction to access the Oracle database using ASP.NET

zhaozj2021-02-16  174

Summary

This paper briefly describes how to use ASP.NET to access Oracle databases. First, the installation of the component is introduced; secondly, the core class included in System.Data.OracleClient; Finally, the specific method is explained by an instance.

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

table of Contents

introduction

System requirements and installation

Core introduction

for example

Reference Information

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

introduction

Microsoft .NET Framework Data Provider for Oracle (hereinafter referred to as .NET for Oracle) is a component .NET Framework component. This component provides great convenience for us .NET access Oracle database. Those developers who use .NET and Oracle, believe in happiness, because they don't have to use OLDB that is not very "professional =" to access the Oracle database. This component is designed very similar to the Microsoft .NET Framework Data Provider for SQL Server and OLEDB built in .NET. If the reader is very familiar with these two built-in components, I believe that you learn this component is also a light car.

The readers of this article are mainly written for programmers who use .NET technology to access Oracle databases, need to have certain C # languages, ADO.NET technology, and Oracle database basics. The disclosure of which is combined with ASP.NET technology and specific annotations. Of course, this does not mean that .NET for Oracle components can only provide services for writing ASP.NET programs, and it can also provide convenience for Windows programs written using .NET technology.

This article briefly introduces the system requirements and installations and core classes of ASP.NET for Oracle, followed by the key to access the Oracle database using this component. This includes .NET for Oracle Access to special data types in a variety of Oracle databases, introduction of various core types, and in the final example of the article, etc.

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

System requirements and installation

Before installing .NET for Oracle, you must first install .NET Framework Version 1.0. At the same time, it is also necessary to determine the installed data access component (MDAC 2.6 and above, the recommended version is 2.7). Since it is necessary to access the Oracle database, then you need to install Oracle 8i Release 3 (8.1.7) Client and the above version. At present, Oracle9i has been released, the author is installed in Oracle 9i, all the procedures in this article are written and debugged in the Oracle9i database environment.

The installation of the component is very convenient, running Oracle_Net.msi directly. No settings are required during the installation process, all the way to click "Next =" to complete the installation. The default installation will create a folder called OracleClient.net in the C: / Program Files / Microsoft.net directory, which contains the following six files, and the specific annotations are as follows:

Note: The MTXoci8.dll file is not installed in the default folder, but is installed in the system directory, for example: C: / Windows / System32 directory.

For developers, it is critical to the System.Data.OracleClient.dll file. This is the core file of the .NET for Oracle component. During use, developers can use the .NET for Oracle component by installing Oracle_Net.msi, when using this component as a system default component, as if it is system.data.sqlclient and system. Data.OleDB components are the same. However, it is necessary to pay attention to: When the developer is divided into the user after the program has completed the program, we do not want to install Oracle_Net just like developers before the user uses this software. . At this time, the developer can copy the system.data.OracleClient.dll file to the BIN directory of the software before publishing. This way users can use the functionality provided by the software without the need to install Oracle_Net.msi. (This method is limited to the development procedures do not involve distributed transactions) ------------------------------------ --------------------------------------------

Core introduction

The .NET for Oracle component is used for organizational classes and other types of names for organizational classes and other types of spaces are System.Data.OracleClient. In this name space, it mainly contains four core classes, which are: OracleConnection, OracleCommand, OracleDataRead, OracleDataAdapter. If the developer knows ADO.NET technology, the use of these four classes will be familiar. These content is very simple, and the specific use is almost the same as SqlConnection, SqlCommand, SqlDataReader, SqlDataAdapter. This is no longer detailed, the reader will learn about the use of these classes in the following manner, and only the following table is given to readers.

for example

Below is an example of manipulating an Oracle database using the .NET for Oracle component. Before writing the program, you must create a table in the Oracle database and join a line 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 line of data

"INSERT INTO ORACLETABLE VALUES ('Test', 4, TO_DATE ('2000-01-11

12:54:01 ',' YYYY-MM-DD HH24: MI: SS '),' 0001020304 ') ";

The following program is to access the Oracle database through the .NET for Oracle component, and display this row data. Please note the classes illustrated in the procedure, and the method of use of the data processing class in Lenovo .NET.

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;

Seduce

.

Using

System.Data.OracleClient;

8

.

public

Class

Pic2: Page

{9. Public Label Message; 10. Public Void Page_Load (Object Sender, Eventargs E) 11. {// Settings Connection Strings 12. String Connstring = "Data Source = EIMS; user = zbmis; password = zbmis;"; / / Instantiate OracleConnection objects 13. OracleConnection conn = new OracleConnection (ConnString); 14. Try15. {16. Conn.open (); // Instantiate OracleCommand Object 17. OracleCommand CMD = Conn.createCommand (); 18. Cmd. Commandtext = "select * from zbmis.oracletypestable"; 19. OracleDataReader OracleDataReader1 = cmd.executeReader (); // Read Data 20. While (OracleDataReader1.Read ()) {// Read and display the first line of the first line Data 21. ORACLESTRING ORACLESTRING1 = ORACLEDATAREADER1.GETORACLESTRING (0); 22. Response.write ("ORACLESTRING" ORACLESTRING1.TOSTRING ()); // Read and display the second column of the second column of the first line 23. OracleNumber ORACLENUMBER1 = OracleDataReader1.Getoraclenumber (1); 24. Response.write ("OracleNumber" OracleNumber1.toString ()); // read and display the data of the third column of the first row 25. OracleDateTime OracleDateTime1 = OracleDataReader1.GetOracleDateTime (2); 26. Response.write ("OracleDateTime" OracleDateTime1.toString ()); // Read Take it and display the data of the fourth column of the first row 27. Oraclebinary Oraclebinary1 = OracleDataReader1.Getoraclebinary (3); 28. IF (Oraclebinary1.isnull == false) 29. {30. Foreach (Byte B in Oraclebinary1.Value) 31. {32. Response.write ("byte" b.toString ()); 33.} 34.} 35.} // Release Resource 36. OracleDataReader1.Close ();

37.} 38. {// Abnormal Process 40. Message.Text = EE.MESSAGE (TEXT = EE.MESSAGE; 41.} 42. Finally43. {// Close connection 44. Conn.Close (); 45. 46.} 47.} If you are familiar with the contents of the data operation in .NET, then the above program is completely understandable. So the analysis here is not very large. Please use the .NET and Oracle readers to remember: .NET for Oracle Components Design is very similar to the Built-in Data Provider for SQL Server and OLEDB.

Summarize in the .NET to read the Oracle database. It is often used in two ways: OracleClient and OLEDB, where OLEDB has two types according to the drivers. 1. OracleClient way, Microsoft is also supported by Oracle database, only supported in .NET Framework 1.1. It is said that the speed is fast, and the performance is good, it is recommended to use. But according to my experience, when the Oracle database server is used in English character sets, the client does not care how the character set is set, the Chinese read Chinese is garbled; if the server is used to use Chinese character sets such as ZHS16GBK, there is no garbled problem. Quote Category Library: System.Data.OracleClient.dll. Name Space: System.Data.OracleClient. Common class: OracleConnection, OracleCommand, OracleDataAdapter, OracleTransaction, OracleDataReader, etc. Typical connection string: "data source = oracst; user id = scott; password = tiger" (Note: You may not specify a Provider driver). 2. OLEDB mode, Microsoft and Oracle each provide the OLEDB driver, and there are few different methods. No matter what the Oracle server is used, there is no garbled problem in reading and writing. The same namespace: System.Data.Oledb. Common class: OLEDBConnection, OLEDBCommand, OLEDBDataAdapter, OLEDBTRANSACTION, OLEDBDATAREADER, and more. Different from the reference library: Microsoft requires only system.data.dll; if you use Oracle drivers, although it is introduced into system.data.dll, the premise is to first install Oracle's data access components for .NET. Connection string: Compared with OracleClient mode, add a provider, Microsoft is "Provider = msdara.1;", oracle is "provider = 'oraoledb.racle';".

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

New Post(0)