Microsoft. netframeworkdataproviderfororacle (. netfororacle) is a. netframework component. This component provides great convenience for us to access the Oracle database using. net. Developers who use. NET and Oracle are sure to be happy because they no longer need to use oledb that is not very "professional =" to access the Oracle database. This component is designed very similar to Microsoft. netframeworkdataproviderforsqlserver and oledb built in. net. If you are very familiar with these two built-in components, you may be familiar with learning this component.
The readers of this article mainly consider accessing the Oracle database using. NET technology.ProgramC # language, ADO. NET technology, and basic knowledge of Oracle Database are required. In this paper, some examples and comments are given based on ASP. NET technology. Of course, this does not mean that the. netfororacle component can only provide services for compiling ASP. NET programs. It can also provide convenience for Windows programs compiled using. NET technology.
This article briefly introduces the system requirements, installation, and core classes of ASP. netfororacle, and then focuses on how to use this component to access the Oracle database. These include. netfororacle's introduction to Special Data Types in various Oracle databases and the usage of various core classes.ArticleFinally, an example is provided.
--------------------------------------------------------------------------------
System requirements and Installation
Before installing. netfororacle, you must first install. netframeworkversion1.0. At the same time, make sure that the data access component is installed (mdac2.6 and later, the recommended version is 2.7 ). To access the data in the Oracle database, you must install the oracle8irelease3 (8.1.7) client and later versions. Currently, Oracle9i has been released. The author installed Oracle9i. All the programs in this article are compiled and debugged in the Oracle9i database environment.
The component installation is very convenient and runs oracle_net.msi directly. No setup is required during the installation process. Click "next =" to complete the installation. By default, a folder named oracleclient. net will be created under the C:/ProgramFiles/Microsoft. NET Directory, which contains the following six files. The specific notes are as follows:
Note: The mtxoci8.dll file is not installed in the default folder, but in the system directory, for example, C:/Windows/system32 directory.
For developers, the system. Data. oracleclient. dll file is crucial. This is the core file of the. netfororacle component. During use, developers can install oracle_net.msi. the netfororacle component is used by the system as a default component. data. sqlclient and system. data. the same is true for oledb components. However, it should be noted that when a developer finishes the program and delivers it to the user for use, for ease of use of the software, we do not want users to use the software before, install oracle_net.msi as a developer. At this time, developers can copy the system. Data. oracleclient. dll file to the bin directory of the software before publishing. In this way, you can use the functions provided by the software without installing oracle_net.msi. (This method is limited to developing programs that do not involve distributed transactions)
--------------------------------------------------------------------------------
Core categories
In the. netfororacle component, the namespace used for organizing classes and other types is system. Data. oracleclient. The namespace contains four core classes: oracleconnection, oraclecommand, oracledatareader, and oracledataadapter. If developers are familiar with the ADO. NET technology, they will be familiar with the use of these four classes. The content is very simple, and its usage is almost the same as sqlconnection, sqlcommand, sqldatareader, and sqldataadapter. We will not describe it in detail here. Readers will learn about the usage of these classes through examples later. Here we will only provide the following table for readers to briefly understand.
--------------------------------------------------------------------------------
Example
The following is an example of using the. netfororacle component to manipulate the Oracle database. Before writing a program, you must create a table in the Oracle database and add a row of data. Use the following statement.
Create a table named oracletypestable
"Createtableoracletypestable (myvarchar2varchar2 (3000), mynumbernumber)
Primarykey, mydatedate, myrawraw (255 ))";
Insert a row of data
"Insertintooracletypestablevalues ('test', 4, to_date ('2017-01-11
12:54:01 ', 'yyyy-mm-ddhh24: MI: ss'), '20140901 ')";
The following program accesses the Oracle database through the. netfororacle component and displays this line of data. In the program, pay attention to the classes described in the previous article, and think about how to use data processing classes in. net.
1. usingsystem;
2. usingsystem. Web;
3. usingsystem. Web. UI;
4. usingsystem. Web. UI. htmlcontrols;
5. usingsystem. Web. UI. webcontrols;
6. usingsystem. Data;
7. usingsystem. Data. oracleclient;
8. publicclasspic2: Page {
9. publiclabelmessage;
10. publicvoidpage_load (objectsender, eventargse)
11 .{
// Set the connection string
12. stringconnstring = "datasource = eims; user = zbmis; Password = zbmis ;";
// Instantiate the oracleconnection object
13. oracleconnectionconn = neworacleconnection (connstring );
14. Try
15 .{
16. Conn. open ();
// Instantiate the oraclecommand object
17. oraclecommandcmd = conn. createcommand ();
18. cmd. commandtext = "select * fromzbmis. oracletypestable ";
19. oracledatareaderoracledatareader1 = cmd. executereader ();
// Read data
20. While (oracledatareader1.read ()){
// Read and display the data in the first column of the First row
21. oraclestringoraclestring1 = oracledatareader1.getoraclestring (0 );
22. response. Write ("oraclestring" + oraclestring1.tostring ());
// Read and display the data in the second column of the First row
23. oraclenumberoraclenumber1 = oracledatareader1.getoraclenumber (1 );
24. response. Write ("oraclenumber" + oraclenumber1.tostring ());
// Read and display the data in the third column of the first row
25. oracledatetimeoracledatetime1 = oracledatareader1.getoracledatetime (2 );
26. response. Write ("oracledatetime" + oracledatetime1.tostring ());
// Read and display the data in the fourth column of the first row
27. oraclebinaryoraclebinary1 = oracledatareader1.getoraclebinary (3 );
28. If (oraclebinary1.isnull = false)
29 .{
30. foreach (bytebinoraclebinary1.value)
31 .{
32. response. Write ("Byte" + B. tostring ());
33 .}
34 .}
35 .}
// Release resources
36. oracledatareader1.close ();
37 .}
38. Catch (exceptionee)
39 .{
// Exception Handling
40. Message. Text = ee. message;
41 .}
42. Finally
43 .{
// Close the connection
44. Conn. Close ();
45 .}
46 .}
47 .}
If you are familiar with the data operations in. net, I believe the above program is completely understandable. So here we will analyze the aboveCodeIt does not make much sense.
Those who use both. NET and Oracle should remember that the design of the. netfororacle component is very similar to the built-in dataproviderforsqlserver and oledb in. net.