Differences between. NET 2.0 access to Oracle and SQL Server

Source: Internet
Author: User
Tags oracleconnection
You can access a database on the. NET platform in the following ways:
1. oledb database access program,
2. ODBC Database Access Program,
3. proprietary database access programs. For example, when accessing SQL Server 2000, we generally like to use proprietary SQL Server. NET Framework database access programs. The namespace is system. Data. sqlclient.

Here I use the third type, that is, the "dedicated database access program" (Oracle. NET Framework Database Access Program) to access the Oracle database.

Before Version 1.1, Oracle. NET framework needs to be downloaded separately. NET Framework itself does not have this component. :

In Framework 2.0, the Oracle. NET Framework database access program is provided. But it does not mean that with the oracle. NET Framework, you can access Oracle smoothly. To access the Oracle database, in addition to the proprietary database access program, the following conditions must be met:

You must install the Oracle 8i Release 3 (8.1.7) client or a later version.

The following is a detailed description of Oracle Database Access:

Frequently Used Components

The namespace of the Oracle. NET Framework Database Access Program is system. Data. oracleclient. The file name is system. Data. oracleclient. dll, which is located in the global assembly cache. By default, VS 2005 does not reference this component. To use this component, you only need to add a reference.
Similar to sqlclient, The oracleclient namespace consists of oracleconnection, oraclecommand, oracledatareader, oracleparameter, and oracletype. The above lists the most commonly used classes. For more detailed class views, see msdn.

Field Type, parameter type

The field type is generally involved when parameter is used. In SQL Server, we generally use sqldbtype enumeration to represent various field types in the database, while in Oracle, oracletype is used. In Oracle, character fields often use varchar2 or nvarchar2, while Numeric Fields use number. Varchar is followed by a 2 character, so I didn't go deep into Oracle. I don't know what the meaning of this 2 character is. When parameter (parameter) is used.
Oracletype. varchar indicates varchar2 in the database,
Oracletype. nvarchar indicates nvarchar2 in the database,
Oracletype. number indicates number
Oracletype. datetime indicates date
For example, oracletype. int32 because it does not have the corresponding field type in Oracle, it is generally small. If the value of the number type field in the database does not have a decimal place, you can also use oracletype. int32 corresponds to number.

Database Connection

Similar to sqlconnection, the connection string is generally: User ID = user name; Data Source = Database Server Source Name (paiel Database Service name); Password = Password

The following is the connection string I used: User ID = search_user; Data Source = etbank_192.168.0.250; Password = 12345

Use of Stored Procedures

A stored procedure in Oracle is called a packages. A package is divided into a packet header and a package body, similar to the class declaration in C ++. The header defines the name and parameters of a stored procedure. Besides the name and parameters, the package body also contains all statements of the stored procedure. Unlike SQL Server, the stored procedures in Oracle are generally written as functions rather than procedure. Oracle stored procedures do not directly return record sets. Record Sets are returned through parameters in the form of cursors. A package can contain multiple stored procedures.Package name. Stored Procedure nameThe following is a typical Oracle stored procedure. It is located in the packages named "test". Its usage should be: Test. getlist
 

Function getlist (keywords in varchar2
, P_info_list_cursor out get_data_cur_type)
Return number
As

Begin

Open p_info_list_cursor
Select * from test where key = keywords
;
Return 0;
End;

The stored procedure returns only one number, and the record set is returned as an out parameter. The method of calling in. NET is as follows:

1 oracleconnection oracleconn = new oracleconnection (connection string );
2 oraclecommand cmd = new oraclecommand ("test. getlist", oracleconn );
3 cmd. Parameters. addrange (
4 New oracleparameter []
5 {
6 new oracleparameter ("keywords", oracletype. varchar ),
7 new oracleparameter ("returnvalue", oracletype. Number, 0, parameterdirection. returnvalue, true, 0, 0, "", datarowversion. Default, convert. dbnull ),
8 new oracleparameter ("p_info_list_cursor", oracletype. cursor, 2000, parameterdirection. Output, true, 0, 0, "", datarowversion. Default, convert. dbnull)
9 });
10 cmd. Parameters [0]. value = 'beauty ';
11 cmd. Parameters [0]. Direction = parameterdirection. input;
12 cmd. commandtype = commandtype. storedprocedure;
13 oracleconn. open ()
14 oracledatareader RDR = cmd. executereader ();
15 // other code
16 oracleconn. Close ();
17

The parameter name in oracleparameter must be the same as the name in the stored procedure, which can be case-insensitive. A Stored Procedure defines two parameters, one keywords and the other a cursor of the out type: p_info_list_cursor. Because the function has a return value, we also need to add aReturnvalue"Parameter, which is fixed. The record set is returned through p_info_list_cursor. After the parameter configuration is complete, you can directly use the exec method of CMD. Although we use an out parameter to accept the record set cursor, this parameter does not need to be processed and oraclecommand will automatically process it, we only need to get the datareader and then read the data like sqlcommand in the past.

Directly execute SQL statements

":" Is used in SQL statements to indicate parameters.
In SQL Server, we can use the SQL statement as follows: "insert into table (field1, field2) values (@ value1, @ value2)", and then we will create several new paramters: new sqlparameter ("@ value1", value )...
The @ + character is used in the query string to describe the parameter. The parameter name in sqlparameter must also use the "@" symbol.
In Oracle, SQL statements cannot use the @ symbol instead of the colon ":". For example:

String SQL = "insert into search_history (keywords, phone, result_id, search_time) values (: keywords,: Phone,: result_id,: search_time )";
Oraclecommand cmd = new oraclecommand (SQL, oracleconn );
Cmd. Parameters. addrange (New oracleparameter [] {
New oracleparameter ("keywords", oracletype. varchar ),
New oracleparameter ("phone", oracletype. varchar ),
New oracleparameter ("result_id", oracletype. Number ),
New oracleparameter ("search_time", oracletype. datetime)
});
Cmd. Parameters [0]. value = keywords;
Cmd. Parameters [1]. value = phone;
Cmd. Parameters [2]. value = 2;
Cmd. Parameters [3]. value = datetime. now;


Common Errors:

1. the number or type of parameters for calling 'stored procedure name' is incorrect"

The error occurs because the parameter name you used to create oracleparameter is inconsistent with the parameter name defined in the stored procedure or SQL statement. In addition, you should also note that, although the SQL statement uses the colon ":" To represent the parameter, but when creating oracleparameter, the specified parameter name cannot use the colon, when new oracleparameter, parametername can only use the character section of the parameter.

2. "Stored Procedure name" is not a process or has not been defined"

The number of parameters in the parameters set of oraclecommand is inconsistent with the number defined in the stored procedure. You may have missed a parameter not created

3. "ORA-01036: Invalid variable name/number"
 
The cause of this error is probably that the parameter definition of the package body in the stored procedure is different from that of the header. Many times the package body is modified, but the packet header is forgotten. In addition, this error may occur when you create oracleparameter due to a large number of constructor versions. We recommend that you specify oracletype when creating oracleparameter.
When you directly use an SQL statement, the parameter section in the SQL statement does not use a colon as the prefix, or the "@" symbol of SQL Server is incorrectly used.

In short, this error occurs when the parametername specified by parameter does not match the actual parameter name.
 
Note: In New oracleparameter (), the specified parametername only needs to contain the character section of the parameter and does not need to contain the prefix, for example, colon.

4,System. Data. oracleclient requires Oracle client software version 8.1.7 or greater. 

This error indicates that you need to install the Oracle client. If you have already installed the Oracle client and this error still occurs, you must have answered the following question due to permission issues: http://www.cnblogs.com/jeet/archive/2005/06/24/115150.html:

1. Log on as an administrator;
2. Find the ORACLE_HOME folder (such as C:/Oracle/ora92), right-click it, select attribute-security, and select "Authenticated Users" in the group or user bar ", in the following permission list, remove the "read and run" permission, and then press the application. Re-select the "read and run" permission and click the application; select the "advanced" button under the permission box, confirm that the application following "Authenticated Users" is "this folder, subfolders, and files", and apply the permission changes to this folder according to OK;

The first time I used Oracle, I encountered this problem and solved it using the Jeet solution. Jeet said that the system must be restarted, but I can do it without restarting. This may be a problem with the operating system version. I use Windows 2003.

Summary:
The access to Oracle and SQL Server is very different:

1. Different Field Types
2. There is a big difference in the stored procedure. Oracle cannot directly return the record set, and an out parameter is required. There is an oracletype. cursor type in oracletype that corresponds to it. Most of the stored procedures in Oracle are defined as funcion and return values. Add a "returnvalue" parameter when defining the command parameter set.
3. Oracle parameters do not require the "@" symbol
4. in Oracle SQL statements, add a colon Before the parameter":", While SQL Server's SQL is preceded "@"

-- SQL statement of SQL Server
Insert into table (column1, column2) values (@ value1, @ value2)

-- SQL statement in cmdel
Insert into table (column1, column2) Values
(: Value1,: value2)

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.