Connect Oracle9i with ASP. NET (ASP. NET1.1)

Source: Internet
Author: User
Tags oracleconnection

Use using System. Data. OracleClient; and add reference System. Data. OracleClient with version 1.0.5000.0 to connect to the oracle database.

Prerequisites: Install at least the ORAcle9I client and open the oracle listener in "Control Panel> Administrative Tools> service.

First, set the data source name in the TNSNAMES. ORA file of oralce. For example

Sample Code for connecting ASP. NET to Oracle9i:

 
 
  1. MYORACLE =  
  2.   (DESCRIPTION =  
  3.     (ADDRESS_LIST =  
  4.       (ADDRESS = (PROTOCOL = TCP)(HOST = SOFTSTAR)(PORT = 1521))  
  5.     )  
  6.     (CONNECT_DATA =  
  7.       (SERVICE_NAME = myoracle)  
  8.     )  
  9.   )  
  10.     
  11. ORA =  
  12.   (DESCRIPTION =  
  13.     (ADDRESS_LIST =  
  14.       (ADDRESS = (PROTOCOL = TCP)(HOST = 210.43.104.131)(PORT = 1521))  
  15.     )  
  16.     (CONNECT_DATA =  
  17.       (SERVICE_NAME = ora)  
  18.     )  
  19.   )  
  20.  

Then, modify the file SQLNET. ORA in the oracle directory

 
 
  1. SQLNET.AUTHENTICATION_SERVICES= (NTS)  
  2.  
  3. NAMES.DIRECTORY_PATH= (TNSNAMES,HOSTNAME,ONAMES)  

Note: add the TNSNAMES parameter to NAMES. DIRECTORY_PATH.

In this way, running tnsping ora or myoracle in cmd will not cause errors.

Finally, if you want to connect to a remote database, you must check the LISTENER. ORA file in the installation directory of the remote oralce database.

 
 
  1. LISTENER =  
  2.   (DESCRIPTION_LIST =  
  3.     (DESCRIPTION =  
  4.       (ADDRESS_LIST =  
  5.         (ADDRESS = (PROTOCOL = TCP)(HOST = SOFTSTAR)(PORT = 1521))  
  6.       )  
  7.     )  
  8.   )  
  9.  
  10. SID_LIST_LISTENER =  
  11.   (SID_LIST =  
  12.     (SID_DESC =  
  13.       (GLOBAL_DBNAME = myoracle)  
  14.       (SID_NAME = myoracle)  
  15.     )  
  16.   )  
  17.  

You can use the net configuration assistant tool provided by oracle to modify the content of this file, that is, modify the "listener" and "Local Network Service name configuration ".

Solution: If "ORA-12154: TNS: unable to process service name" is reported, check the local sqlnet first. ora, look at NAMES. whether the DIRECTORY_PATH is (TNSNAMES). If yes, check the native tnsnames. whether the service name is included in ora.

With several commands: lsnrctl status and tnsping Data Source Name

Source code snippet for connecting to Oracle9i program with ASP. NET

 
 
  1. UsingSystem;
  2. UsingSystem. Collections;
  3. UsingSystem. ComponentModel;
  4. UsingSystem. Data;
  5. UsingSystem. Drawing;
  6. UsingSystem. Web;
  7. UsingSystem. Web. SessionState;
  8. UsingSystem. Web. UI;
  9. UsingSystem. Web. UI. WebControls;
  10. UsingSystem. Web. UI. HtmlControls;
  11. UsingSystem. Data. OracleClient;
  12.  
  13. NamespaceWebApplication1
  14. {
  15.  /// <Summary> 
  16.  /// Summary of testConnOracle. 
  17.  /// </Summary> 
  18.  Public ClassTestConnOracle: System. Web. UI. Page
  19. {Private VoidPage_Load (ObjectSender, System. EventArgs e)
  20. {
  21. // Place user code here to initialize the page 
  22. ConnectToOracle ();
  23. }
  24.  
  25. Public VoidConnectToOracle ()
  26. {
  27. OracleConnection conn =NewOracleConnection ();// Define the oracle connection 
  28.  
  29.  
  30. // The following is the set connection string, where data source is filled with the database name, that is, in TNSNAMES. ORA 
  31.  
  32. // Data source (MYORACLE or ORA) or service name myoracle or ora) 
  33.  
  34. Conn. ConnectionString ="User id = system; data source = ora; password = els ;";
  35.  
  36. Try 
  37. {
  38. Conn. Open ();
  39. OracleCommand cmd =NewOracleCommand ("Select * from HR. TEST", Conn );
  40. DataSet ds =NewDataSet ();
  41. OracleDataAdapter oda =NewOracleDataAdapter ();
  42. Oda. SelectCommand = cmd;
  43. Oda. Fill (ds );
  44. DataGrid1.DataSource = ds. Tables [0]. DefaultView;
  45. DataGrid1.DataBind ();
  46. Response. Write ("Success! ");
  47. }
  48. Catch(Exception ex)
  49. {
  50. Response. Write (ex. Message );
  51. }
  52. Finally 
  53. {
  54. Conn. Close ();
  55. }
  56. }
  57.  

In addition, you can drag an OracleConnection in VS2003 to test whether it can be connected to the oralce database.

Finally, attach a link that may be useful.

Http://community.csdn.net/Expert/topic/4443/4443598.xml? Temp =. 3260309.

Http://bbs.chinaunix.net/archiver? Tid-651387.html

Http://community.csdn.net/Expert/topic/4634/4634614.xml? Temp =. 5633051.

Http://blog.csdn.net/hiyaolee/archive/2005/10/20/509767.aspx

Http://blog.csdn.net/xmltea/archive/2005/01/24/265754.aspx

  1. Implementation of ASP. net mvc paging controls
  2. ASP. net mvc instance: using the Northwind and Entity frameworks
  3. How ASP. NET works
  4. Overview of ASP. NET cookie operations
  5. ASP. NET obtains the code of the primary key of the inserted row.

Related Article

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.