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:
- MYORACLE =
- (DESCRIPTION =
- (ADDRESS_LIST =
- (ADDRESS = (PROTOCOL = TCP)(HOST = SOFTSTAR)(PORT = 1521))
- )
- (CONNECT_DATA =
- (SERVICE_NAME = myoracle)
- )
- )
-
- ORA =
- (DESCRIPTION =
- (ADDRESS_LIST =
- (ADDRESS = (PROTOCOL = TCP)(HOST = 210.43.104.131)(PORT = 1521))
- )
- (CONNECT_DATA =
- (SERVICE_NAME = ora)
- )
- )
-
Then, modify the file SQLNET. ORA in the oracle directory
- SQLNET.AUTHENTICATION_SERVICES= (NTS)
-
- 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.
- LISTENER =
- (DESCRIPTION_LIST =
- (DESCRIPTION =
- (ADDRESS_LIST =
- (ADDRESS = (PROTOCOL = TCP)(HOST = SOFTSTAR)(PORT = 1521))
- )
- )
- )
-
- SID_LIST_LISTENER =
- (SID_LIST =
- (SID_DESC =
- (GLOBAL_DBNAME = myoracle)
- (SID_NAME = myoracle)
- )
- )
-
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
- UsingSystem;
- UsingSystem. Collections;
- UsingSystem. ComponentModel;
- UsingSystem. Data;
- UsingSystem. Drawing;
- UsingSystem. Web;
- UsingSystem. Web. SessionState;
- UsingSystem. Web. UI;
- UsingSystem. Web. UI. WebControls;
- UsingSystem. Web. UI. HtmlControls;
- UsingSystem. Data. OracleClient;
-
- NamespaceWebApplication1
- {
- /// <Summary>
- /// Summary of testConnOracle.
- /// </Summary>
- Public ClassTestConnOracle: System. Web. UI. Page
- {Private VoidPage_Load (ObjectSender, System. EventArgs e)
- {
- // Place user code here to initialize the page
- ConnectToOracle ();
- }
-
- Public VoidConnectToOracle ()
- {
- OracleConnection conn =NewOracleConnection ();// Define the oracle connection
-
-
- // The following is the set connection string, where data source is filled with the database name, that is, in TNSNAMES. ORA
-
- // Data source (MYORACLE or ORA) or service name myoracle or ora)
-
- Conn. ConnectionString ="User id = system; data source = ora; password = els ;";
-
- Try
- {
- Conn. Open ();
- OracleCommand cmd =NewOracleCommand ("Select * from HR. TEST", Conn );
- DataSet ds =NewDataSet ();
- OracleDataAdapter oda =NewOracleDataAdapter ();
- Oda. SelectCommand = cmd;
- Oda. Fill (ds );
- DataGrid1.DataSource = ds. Tables [0]. DefaultView;
- DataGrid1.DataBind ();
- Response. Write ("Success! ");
- }
- Catch(Exception ex)
- {
- Response. Write (ex. Message );
- }
- Finally
- {
- Conn. Close ();
- }
- }
-
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
- Implementation of ASP. net mvc paging controls
- ASP. net mvc instance: using the Northwind and Entity frameworks
- How ASP. NET works
- Overview of ASP. NET cookie operations
- ASP. NET obtains the code of the primary key of the inserted row.