Connect to Oracle via ASP
contributed by Bret H. Grade bgrade@aris.com
MCSE, MCP+Internet Senior Consultant,
ARIS Corporation Inquire at http://www.aris.com
What do I need for connectivity for Oracle from ASP:
This question is a very valid question for most people beginning and experienced with Oracle from within ASP. Because of the variety of things that are needed, there can be many answers to this question. Here are the basics:
- SQL*Net needs to be loaded on the machine where IIS resides.
- A "System" DSN (Data Source Name) should be configured on the machine where IIS resides. Or you may use a "DSNless" connection. A connection of this type still requires you have the correct ODBC driver. See below.
- Your DSN connection consists of an ODBC driver. Here are your most utilized choices:
- Microsoft ODBC Driver for Oracle 2.00.00.6325 (Microsoft supplied driver)
- Microsoft ODBC for Oracle 2.573.3513.00 (Microsoft supplied driver)
- Oracle ODBC Driver 7.x or 8.x (Oracle supplied driver)
Most people prefer to use the Microsoft ODBC Driver for Oracle 2.0 for the reason that is seems to be more stable than the 2.5 driver from MS.
The Oracle ODBC Driver has a variety of incompatibility problems with ASP and it is not recommended to use this driver.
Make sure you’ve tested the DSN before you implement. This will prevent you from having to deal with connectivity problems at this level while developing.
- The TNSNAMES.ORA file needs to have been configured on the machine where IIS resides. This file is located in you ORANT\NETWORK (or NET80)\ADMIN\ directory. An Example configuration is provided below:
orcl.world = (DESCRIPTION = (ADDRESS_LIST = (ADDRESS = (COMMUNITY = tcp.world) (PROTOCOL = TCP) (Host = THE IP ADDRESS OF YOUR HOST OR DNS NAME) (Port = 1521) ) (ADDRESS = (COMMUNITY = tcp.world) (PROTOCOL = TCP) (Host = THE IP ADDRESS OF YOUR HOST OR DNS NAME) (Port = 1526) ) ) (CONNECT_DATA = (SID = ORCL) ) )
Some things to note are the HOST, CONNECT_DATA, and PORT parameters.
- The host is the IP address or DNS name of the machine where the Oracle instance (database) you are trying to connect to resides.
- The CONNECT_DATA string is made up of the Oracle SID. The SID is the name of the Oracle Instance.
- The PORT parameter is defaulted to 1521 and 1526. These are the default installations where the listener listens for requests from the machine where Oracle is installed.
What does a connection string to Oracle look like?
Set AUM = Server.CreateObject("ADODB.Connection")
AUM.Open Connect_String(DSN), USERNAME, PASSWORD
How do you configure the Oracle ODBC Driver?
To configure the ODBC driver, you will first need to :
- open the "ODBC Data Sources" icon in the control panel applet.
- Once opened, go to the "System DSN" tab and choose add. Select an ODBC driver (See "WHAT DO I NEED FOR CONNECTIVITY FOR ORACLE FROM ASP").
- Once selected, you will see the following screen minus everything below connect string. If you want the advanced configuration as shown below select options.
* The above example is utilizing the "Microsoft ODBC Driver for Oracle 2.00.00.6325" driver.
I’m getting an error with my SQL statement and I don’t know why!
This problem seems to come up quite often. Rest assured, it is not just common to Oracle. Most of the time this problem is because the developer has not tested the SQL statement in question via a SQL tool (i.e. SQL Plus for Oracle or ISQL in SQL Server). Some of the steps you should take when debugging are:
- ALWAYS run your command through SQL*PLUS if your statement fails. This way you eliminate the fact that it could be a syntax problem with Oracle.
- If you have successfully tested the statement against the database outside of ASP, make sure your statement is syntactically correct with ADO.
- Make sure all concatenated statements have the correct spaces in them.
sqlChk = "SELECT COUNT(NAME)"
sqlChk = sqlChk & " FROM MYAPP_USERS"
sqlChk = sqlChk & " WHERE NAME = UPPER('" & Request.Form("txtUserID") & "')"
* Note: Although eliminating the number of lines for interpretation can optimize your code, some prefer readability to the slight performance increase.
If you were to look closely, you would notice that the second and third line have a space between the start of the clause and the ". Another method to insure proper spaces between concatenated statements would be to put the space at the end of each line.
- Response.Write your sql statement to the screen.
- Make sure that you DO NOT have spaces in your table names.
I keep getting a TNS error while trying to connect.
Check the following:
- Insure that you can connect via SQL*PLUS. This will confirm that your TNSNAMES.ORA file is configured properly. If not, check the following:
- Verify the instance is running (i.e. Oracle is up).
- Make sure you have a TNSNAMES.ORA entry.
- Check to see that the "TNS listener" service is running on the machine where Oracle resides.
- Check to make sure that you have the correct ODBC driver.
- If you can connect via SQL*PLUS, check the following:
- Check to make sure that you have the correct ODBC driver.
Comments
Why This Error ?
I've tried the code above..
but this error appear, what could possibly wrong ?
well i tested it with my stored procedure
this is my stored procedure ;
Create or replace package testpda as
TYPE t_cursor IS REF CURSOR ;
Procedure PackTransactions(id varchar, recs out t_cursor);
end testpda;
create or replace package body testpda as
tabtg varchar(30);
tabpb varchar(30);
tabdp varchar(30);
stmt varchar(1000);
selcl varchar(500);
condcl varchar(500);
myreg varchar(2);
myeop varchar(6);
Procedure PackTransactions(id varchar, recs out t_cursor) as
begin
stmt:='select eop,jumtag,tgllunas,jumbyr from tagihan_nasional t,'
|| ' pembayaran_nasional p where t.msisdn=' || id
|| ' and rtrim(t.msisdn)=rtrim(p.msisdn)';
open recs for stmt;
end PackTransactions;
end testpda;
and this is my asp code.. (well it's developed from the Surya Rao's code)
SQL = "{call testpda.PackTransactions({id,recs})}"
Const cIDParam = 0
Set objEmpDetailCommand = Server.CreateObject("ADODB.Command")
objEmpDetailCommand.ActiveConnection = conn
objEmpDetailCommand.CommandText = SQL
objEmpDetailCommand.CommandType = 4 '** adCmdText
objEmpDetailCommand.Parameters(cIDParam).Direction = 1
'* adParamInput
objEmpDetailCommand.Parameters(cIDParam).Value = id
in some reference.. that explains about the stored procedure that put ref cursor as the output parameter, it didn't include the ref cursor as the out parameter.. well.. but it is error. i've tried another code..it worked on their sp..
my sp is already tested on the sql worksheet.. and works
but my asp code ... still can't manage to call it..
the error sounds like this :
Provider
error '80040e14'One or more errors occurred during processing of command.
/rekon/recapp/testpack.asp, line 42
please help me.. i can't figure out the solutions for almost 2 weeks...
and i've been sending to many people asking bout this.. none replies..
You must me logged in to write a comment.