This article will introduce in detail the code for connecting to an Oracle data instance in asp.net. For more information, see.
Connecting to the oracle database is simple, as shown below
The Code is as follows: |
Copy code |
ConnectionString = "Password = czh; User ID = czh; Data Source = (DESCRIPTION = (ADDRESS_LIST = (ADDRESS = (PROTOCOL = TCP) (HOST = 192.168.168.211) (PORT = 1521) (CONNECT_DATA = (SERVER = DEDICATED) (SERVICE_NAME = skydream )));" |
HOST: Server IP address or server name
SERVICE_NAME: oracle Data name
User ID: oracle User name
Password: oracle user Password
If the oracle database and web server are on the same server, you can write
ConnectionString = "Password = czh; User ID = czh; Data Source = skydream ;"
Now we are starting to query the data,
Preparations:
A. Install the Oracle Client. I installed oracle 10g. If you use Data Source = IP address, you must install the client
1. Add reference System. Data. OracleClient in. net
2. Define the connection mode (BIND Data to the GridView): string strConn = "Data Source = IP address; User ID = Account; Password = Password ";
The Code is as follows: |
Copy code |
OracleConnection conn = new OracleConnection (strConn); --- instantiate the connection Conn. open (); String strSql = "select * from Table "; OracleDataAdapter da = new Oracle DataAdapter (strSql, conn ); DataTable dt = new DataTable (); Da. Fill (dt ); Conn. Close (); Gridview1.DataSource = dt; Gridview1.DataBind (); |
Bound successfully.
B. If the oracle client is not installed, you can view the following information:
The Code is as follows: |
Copy code |
String StrConn = "Data Source = ( DESCRIPTION = (ADDRESS_LIST = (ADDRESS = (PROTOCOL = TCP) (HOST = *. *) (PORT = **)) ) (CONNECT_DATA = (SERVICE_NAME = service name) ) ); |
User ID = Account; Password = Password; "; ----- not verified
B. Centralized database reading methods:
1.1 is the instance in.
Update and insert Update query of oracle Database content *
The Code is as follows: |
Copy code |
OracleConnection conn = new OracleConnection (strConn ); Conn. open (); String strSql = "update tabel1 set column1 = ''where ..."; Or strSql = "insert into table1 values ('','')"; OracleCommand ocd = new OracleCommand (strSql, conn ); Int intResult = ocd. ExecuteNonQuery (); Conn. closed (); |
Read and query oracle database content
The Code is as follows: |
Copy code |
String strSql = "select * from tblproject "; OracleCommand cmd = new OracleCommand (strSql, conn ); OracleDataReader dr = cmd. ExecuteReader (); If (dr. Read ()) {......} Cmd. Dispose (); Dr. Dispose (); Conn. Closed (); |
I have encountered some problems during the connection process. I will also share with you that an error occurred while connecting to ORACLE.
When the data access component is used in the asp.net application (calling System. data. oracleClient), the Program Reports "System. exception: System. data. oracleClient requires Oracle client software version 8.1.7 or greater "error.
A When Oracle 9.2 runs on the NTFS partition, the ORACLE_HOME directory is invisible to some non-administrator groups, in windows server 2003, the account used by the asp.net application is netword service. Therefore, you cannot create an oracle connection. You only need to reset the permission of the ORACLE_HOME directory.
The procedure is as follows:
1. Log on as an administrator;
2. Find the ORACLE_HOME folder (D: oracleora92 for myself, do not change it to the ORACLE folder, because the ORACLE_HOME folder is the ora92 folder under oracle), right-click and select Properties-security, select "Authenticated Users" in the group or user column, remove the "read and run" permission from the permission list below, and then press the application. Select the "read and run" permission again, click "application", select the "advanced" button under the permission box, and confirm that the application following "Authenticated Users" is "this folder, subfolders, and files ", apply the permission change to the folder according to OK;
3. restart the computer to make the permission settings take effect (you only need to restart IIS );
4. log on and run the asp.net application to obtain data from the Oracle database.