Connecting to the Oracle database through ASP. NET

Source: Internet
Author: User
Tags oracleconnection

 
1. System. Data. oracleclient and system. Data. oledb namespaces
Although the classes in the two namespaces can access the Oracle database. data. the class efficiency of the sqlclient namespace is higher than that of the system. data. the class in the oledb namespace is higher), system. data. the classes in the oracleclient namespace are larger than those in the system. data. the oledb namespace class is more efficient (I did not personally verify this, but most places will say so, moreover, since the things specifically designed for Oracle should also be specifically optimized in theory ).
Of course, another point is to make system. Data. oracleclient better:
For example, the data type, system. data. oledb. system is not listed in oledbtype enumeration. data. oracleclient. which are specific in the oracletype enumeration. In addition, if the number type of Oracle is large, it exceeds. NET data type range, you must use system. data. special Class in oracleclient-oraclenumber type.
Well, I will not repeat these two comparisons. The following mainly discusses the types in the system. Data. oracleclient namespace, that is, ADO. Net for Oracle Data Provider (data provider ).
2. Database connection:
Whether you use system. Data. oledb or system. Data. oracleclient to access Oracle, you must install the Oracle client component on the. NET running machine (Web server in ASP. NET. (This is different from the two types of MS databases, MS things install MDAC: Microsoft Data Access Component 2.6 or later, there is no need to install the SQL Server Client or office software, .)
System Requirements:
(1) If you use system. Data. oracleclient to access Oracle, the client component version should be later than Oracle 8i client Release 3 (8.1.7. The MS only ensures access to Oracle 8.1.6, Oracle 8.1.7, and Oracle 9i servers. MDAC 2.6 or above.
(2) If you use system. Data. oledb to access Oracle, the client component version 7.3.3.4.0 or later may be 8.1.7.4.1 or later. MDAC 2.6 or above.
If the server is Oracle8i or above, the client component version should be 8.0.4.1.1c.
In. net running machine, install the Oracle client, and then open net manager (Oracle 9i)/easy config (Oracle 8i) set the local service ing based on your previous experience (the service name here is used for database connection strings ).
The connection string used to access the Oracle database in system. Data. oracleclient is:
User ID = user name; Password = password; Data Source = service name
(The above is a general connection string. Detailed connection string items can be found in the document of the system. Data. oracleclient. oracleconnection. connectionstring attribute .)
The connection string used to access the Oracle database in system. Data. oledb is:
Provider = msdaora.1; user id = user name; Password = password; Data Source = service name
3. Data Types in Oracle:
Compared with SQL Server, Oracle data types are somewhat "odd": most data types of SQL Server are easy to find.. NET is close to the type in Oracle. the net type is far away. After all, Oracle is a database close to Java.
Number: Number type. Generally, it is number (m, n), M is a valid number, and N is the number of digits after the decimal point (0 by default). This is in decimal format.
Nvarchar2: variable-length Unicode, which is similar to SQL Server nvarchar (but I don't know why oracle adds "2 "). (Remove "N" from non-Unicode, the same below .)
Nchar: Unicode ).
Nclob: The field used to store a large number of characters (UNICODE.
Date: date type, which is closer to the datetime of SQL Server.
In Oracle, fields cannot be of the BIT or bool type. They are generally replaced by number (1.
Like SQL Server, in SQL commands, character types must be separated by single quotation marks ('). Two single quotation marks ('') are escape characters in single quotation marks (for example, I'm fat. write an SQL command: update... set... = 'I'm fat. '...).
The special date type is as follows:
Update... set... = timestamp '2017-7-20 15:20:07 '...
Note that the timestamp keyword is used and separated by single quotation marks. Note that the date format is recognizable. The format recognized by Oracle is not as many as that recognized by SQL Server. This is different from SQL Server.
By the way, the date types in access are separated by the well number (#). Update... set... = #15:20:07 #...
4. Accessing Oracle process/function (1)
Stored procedures are often used when SQL Server is used as a program. Procedures and functions can also be used in Oracle. The Oracle process does not seem to have a return value, but the function that has a return value (such as basic. SQL Server Stored Procedures can return values ).
. Net accesses the Oracle process/function in a way similar to SQL Server, for example:
Oracleparameter [] parameters = {
New oracleparameter ("returnvalue", oracletype. int32, 0, parameterdirection. returnvalue, true, 0, 0 ,"",
Datarowversion. Default, convert. dbnull)
New oracleparameter ("parameter 1", oracletype. nvarchar, 10 ),
New oracleparameter ("parameter 2", oracletype. datetime ),
New oracleparameter ("parameter 3", oracletype. Number, 1)
};
Parameters [1]. value = "test ";
Parameters [2]. value = datetime. now;
Parameters [3]. value = 1; // It can also be new oraclenumber (1 );
Oracleconnection connection = new oracleconnection (connectionstring );
Oraclecommand command = new oraclecommand ("function/process name", connection );
Command. commandtype = commandtype. storedprocedure;
Foreach (oracleparameter parameter in parameters)
Command. Parameters. Add (parameter );
Connection. open ();
Command. executenonquery ();
Int returnvalue = parameters [0]. value; // receives the function return value.
Connection. Close ();
For the dbtype setting of parameter, see system. data. oracleclient. documents of oracletype enumeration. For example, the value of number type parameters in Oracle Database can be used. net decimal or system. data. oracleclient. the value of an integer parameter can be used.. Net Int or oraclenumber type. And so on.
In the preceding example, we can see that the function return value is specified using the parameter "returnvalue", which is the parameter of parameterdirection. returnvalue.
5. Accessing Oracle process/function (2)
Processes/functions that do not return record sets (without select output) are called in a similar way as SQL Server. However, if you want to return a record set through a process/function, it is more troublesome in Oracle.
In SQL Server, the following stored procedure:
Create procedure getcategorybooks
(
@ Categoryid int
)
As
Select * From books
Where categoryid = @ categoryid
Go
In Oracle, follow these steps:
(1) create a package containing a cursor type: (only once in a database)
Create or replace package test
As
Type test_cursor is ref cursor;
End test;
(2) process:
Create or replace procedure getcategorybooks
(
P_cursor out test. test_cursor, -- the type in the upper part of the bread. The output parameter
P_catogoryid integer
)
As
Begin
Open p_cursor
Select * From books
Where categoryid = p_catogoryid;
End getcategorybooks;
(3). Net Program:
Oracleparameters parameters = {
New oracleparameter ("p_cursor", oracletype. cursor, 2000, parameterdirection. Output, true, 0, 0 ,"",
Datarowversion. Default, convert. dbnull ),
New oracleparameter ("p_catogoryid", oracletype. int32)
};
Parameters [1]. value = 22;
Oracleconnection connection = new oracleconnection (connectionstring );
Oraclecommand command = new oraclecommand ("getcategorybooks", connection );
Command. commandtype = commandtype. storedprocedure;
Foreach (oracleparameter parameter in parameters)
Command. Parameters. Add (parameter );
Connection. open ();
Oracledatareader DR = command. executereader ();
While (dr. Read ())
{
// Your specific operation. I don't need to teach this?
}
Connection. Close ();
In addition, if datareader is used to obtain a record set, the program cannot access the data of output parameters and returned values before datareader is disabled.

Demo

Demo interface: default. aspx

Controls added on the interface:
Two textbox: temail (for entering user email) and tpassword (for entering the registration password)
A button: breg
A label: llable (used to display a response after successful registration)
DEMO code: default. aspx. CS
We will first write the data connection string in Web. config:
<Deleetask>
<Add key = "oracleconn" value = "User ID = username; Password = password; Data Source = Database Service name;"/>
</Appsettings>
The source code of the demo is as follows:
Using system;
Using system. Data;
Using system. configuration;
Using system. Web;
Using system. Web. Security;
Using system. Web. UI;
Using system. Web. UI. webcontrols;
Using system. Web. UI. webcontrols. webparts;
Using system. Web. UI. htmlcontrols;
Using system. Data. oracleclient; // Add an oracleclient reference
Public partial class _ default: system. Web. UI. Page
{
Public System. Data. oracleclient. oracleconnection objconn; // declare an oracleconnection object
Public System. Data. oracleclient. oraclecommand objcmd; // declare an oraclecommand object
Protected void page_load (Object sender, eventargs E)
{
Llable. Text = "";
String strcon = system. configuration. configurationmanager. etettings ["oracleconn"];
// Call the database connection string from the Web. config file
Objconn = new oracleconnection (strcon );
}
Protected void breg_click (Object sender, eventargs E)
{
String strsql = "insert into temp (email, password) values ('";
Strsql + = temail. Text. Replace ("'", "'' "). tostring () + "','";
Strsql + = tpassword. Text. Replace ("'", "'' "). tostring () + "')";
Objcmd = new oraclecommand (strsql, objconn );
Objconn. open ();
Objcmd. executenonquery ();
Objconn. Close ();
Llable. Text = "registered successfully. your email address is:" + temail. Text. tostring ();
}
}

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.