Oracle stored procedures

vbjustin

Member
Joined
Aug 3, 2006
Messages
15
Programming Experience
1-3
Hi there, I have been working on a program that talks to an oracle database, Oracle Express 10g right now, and I was wondering exactly how I could use a stored procedure, using the ODP.NET package, to send two variables to it, a num and str. This procedure squares the number and tells you the length of the string, all stored within a table called "test".

If you guys could help me out, that would be great, I've been at this thing for way too long, and have just gotten nowhere on this part of the program.

thanks in advance, and if you need me to show you what I have so far, just ask and I'll post that too.

vbjustin!:)
 
I am using Oracle Express 10g.
I have tried to create a connection using VS IDE, but with no success, I have VS .NET 2003. I think what migt be happening is that it doesn't call the stored procedure, becaue I have this line, dim cmd as OracleCommand("vb",oOracleConn), where "vb" is the stored procedure, and oOracleConn is the connection. I hacve this line too, cmd.CommandType = CommandType.StoredProcedure, but Im not sure that its working proprerly. is it possible that in the string in the OracleComand that I need more than just "vb", oOracleConn?

Thanks,
vbJustin!
 
I think you are missing the code that sets the command type to stored procedure.

Can you post your code again? Just the part that creates the connection object, opens the connection, creates the command type, set command type to stored procedure and the line of code that executes it.
 
I think you are missing the code that sets the command type to stored procedure.

Can you post your code again? Just the part that creates the connection object, opens the connection, creates the command type, set command type to stored procedure and the line of code that executes it.

Her it is:
VB.NET:
[SIZE=2]MsgBox("1.Connecting...")
oOracleConn.Open()
MsgBox("1.Connected!")
MsgBox("connect for data...")
[/SIZE][SIZE=2][COLOR=#0000ff]Dim[/COLOR][/SIZE][SIZE=2] cmd [/SIZE][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][SIZE=2] [/SIZE][SIZE=2][COLOR=#0000ff]New[/COLOR][/SIZE][SIZE=2] OracleCommand("vb", oOracleConn)
MsgBox("connection!")
[/SIZE][SIZE=2][COLOR=#0000ff]Dim[/COLOR][/SIZE][SIZE=2] sqlTransaction [/SIZE][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][SIZE=2] OracleTransaction = oOracleConn.BeginTransaction()
[/SIZE][SIZE=2][COLOR=#008000]'sqlTransaction = oOracleConn.BeginTransaction
[/COLOR][/SIZE][SIZE=2]MsgBox("2.Getting command...")
[/SIZE][SIZE=2][COLOR=#008000]'cmd.CommandText = "vb.vb"
[/COLOR][/SIZE][SIZE=2][/SIZE][SIZE=2][COLOR=#008000]'cmd.Transaction.Initialize
[/COLOR][/SIZE][SIZE=2][/SIZE][SIZE=2][COLOR=#008000]' .LifetimeService()
[/COLOR][/SIZE][SIZE=2]cmd.CommandType = CommandType.StoredProcedure
cmd.Parameters.Add(":numsq", 1)
cmd.Parameters.Add(":len(str)", 1)
cmd.Parameters.Add(":num", 4)
cmd.Parameters.Add(":str", "help!")
[/SIZE][SIZE=2][COLOR=#008000]'cmd.Parameters.Add(New OracleParameter(":numsq", OracleDbType.Decimal, ParameterDirection.Output))
[/COLOR][/SIZE][SIZE=2][/SIZE][SIZE=2][COLOR=#008000]'cmd.Parameters(":numsq").Value = 0
[/COLOR][/SIZE][SIZE=2][/SIZE][SIZE=2][COLOR=#008000]'cmd.Parameters.Add(New OracleParameter(":len(str)", OracleDbType.Decimal, ParameterDirection.Output))
[/COLOR][/SIZE][SIZE=2][/SIZE][SIZE=2][COLOR=#008000]'cmd.Parameters(":len(str)").Value = 0
[/COLOR][/SIZE][SIZE=2][/SIZE][SIZE=2][COLOR=#008000]'cmd.Parameters.Add(New OracleParameter(":num", OracleDbType.Decimal, ParameterDirection.InputOutput))
[/COLOR][/SIZE][SIZE=2][/SIZE][SIZE=2][COLOR=#008000]'cmd.Parameters(":num").Value = TextBox2.Text
[/COLOR][/SIZE][SIZE=2][/SIZE][SIZE=2][COLOR=#008000]'cmd.Parameters.Add(New OracleParameter(":str", OracleDbType.Varchar2, ParameterDirection.InputOutput))
[/COLOR][/SIZE][SIZE=2][/SIZE][SIZE=2][COLOR=#008000]'cmd.Parameters(":str").Value = TextBox1.Text
[/COLOR][/SIZE][SIZE=2][/SIZE][SIZE=2][COLOR=#008000]'AdjustParameters(cmd)
[/COLOR][/SIZE][SIZE=2]cmd.ExecuteNonQuery()
cmd.Transaction.Commit()
[/SIZE][SIZE=2][COLOR=#008000]'sqlTransaction.Commit()
[/COLOR][/SIZE][SIZE=2][/SIZE][SIZE=2][COLOR=#008000]'Dim ds as System.Data.OracleClient.OracleDataAdapter(cmd)
[/COLOR][/SIZE][SIZE=2][/SIZE][SIZE=2][COLOR=#008000]'result = ds.Fill(Data)
[/COLOR][/SIZE][SIZE=2]MsgBox("2.Got Command!")
[/SIZE]
 
please also post the code for the stored procedure.. looking at your parameters names, something is very messed up
 
Like i said above, the stored procedure is in the Oracle database side of things, all im trying to do is call the procedure, input the values, let the procedure do its thing, and enter the data within the table. The values that should be in the table are as follows: num, str, numsq, len(str).

I have seen two ways of doing the parameters, both of which I have above, with one of the ways commented out so it wont get confused.
 
I didnt ask where the stored procedure is; they cannot be anywhere other than the database. I can see we arent really getting anywhere with this, so do this instead:

In your SQL editor:
VB.NET:
DROP TABLE tmp_table;
 
CREATE TABLE tmp_table(a_column varchar2(100));
 
CREATE OR REPLACE PROCEDURE tmp_procedure(
  input_param IN varchar2,
  output_param OUT number
)
IS BEGIN
 
  INSERT INTO tmp_table(a_column) VALUES(input_param);
  SELECT COUNT(*) INTO output_param FROM tmp_table;
 
END tmp_procedure;

now in your vb, make a new button on the form, and paste this into the button's click handler:

VB.NET:
        Dim oConn As New OracleClient.OracleConnection("Data Source=THE_TNS_NAME_OF_YOUR_SERVER;User ID=THE_USER_ID_OF_YOUR_SERVER;Password=THE_PASSWORD_OF_YOUR_SERVER;Persist Security Info=True;Unicode=True")
        Dim oCmd As OracleClient.OracleCommand = oConn.CreateCommand()

        oCmd.CommandType = CommandType.StoredProcedure
        oCmd.CommandText = "tmp_procedure"
        oCmd.Parameters.Clear()
        oCmd.Parameters.Add("input_parameter", OracleClient.OracleType.VarChar).Value = "Text to insert into tmp_table.a_column"
        oCmd.Parameters.Add("output_parameter", OracleClient.OracleType.Number).Direction = ParameterDirection.Output

        oCmd.ExecuteNonQuery()

        MessageBox.Show("The return value from the stored proc was: " & oCmd.Parameters("output_parameter").Value.ToString())

additionally, remove any reference to Oracle.DataAccess from your project (it's oracle's data provider) and ensure that the reference System.Data.OracleClient exists

If you dont want to do this to your existing project, start a new one


Really, youre making this far harder than it actually is by not answering questions or using the information youre provided with. I told you previously that parameters names do not contain colons when youre setting them up. Additionally, ":len(str)" cannot be a valid parameter name because it contains illegal characters ( and )
It would be like writing this:

Dim left(myString, 4) as New String
 
Ok, this works a bit better, except I want to access a remote server, not the Oracle server on my computer. When I put in the information for the remote username and password it says illegal Username/password: logon denied. How can I fix this issue, is there another part of the OracleClient.OracleConnection string that I need to Include?

Thanks, vbjustin :)
 
I cannot tell you what the username and password of your oracle server are, any more than I can tell you what your username and password are to log onto your email or windows account are, sorry. If you were connecting my my oracle database, i could tell you the passwords because I would have set them. You will have to seek assistance from your DB Administrator on this issue.
 
NO, I know my username and Password! Is there anything else that needs to be put down for the connection string to connect to a remote server? I know those are correct, it just tells me that it is tryin gto connect to the server on the Oracle database on my comuter I set up. If I try to log onto that, it lets me connect. But the table and the procedure is in a remote computer that I have been accesing through its IP address, and then using the set up Account in that Oracle Database.

Sorry if I sound a bit off, but this is just bugging the crap out of me, and I know Im very close to getting this working.

Thanks, vbJustin.
 
There are several ways to access Oracle databases, and we have chosen to go the route of TNS naming. On every client PC there is a file called TNSNAMES.ORA that looks like this:

VB.NET:
 This file contains the syntax information for 
# the entries to be put in any tnsnames.ora file
# The entries in this file are need based. 
# There are no defaults for entries in this file
# that Sqlnet/Net3 use that need to be overridden 
#
# Typically you could have two tnsnames.ora files
# in the system, one that is set for the entire system
# and is called the system tnsnames.ora file, and a
# second file that is used by each user locally so that
# he can override the definitions dictated by the system
# tnsnames.ora file.
# The entries in tnsnames.ora are an alternative to using
# the names server with the onames adapter.
# They are a collection of aliases for the addresses that 
# the listener(s) is(are) listening for a database or 
# several databases.
# The following is the general syntax for any entry in 
# a tnsnames.ora file. There could be several such entries 
# tailored to the user's needs.
<alias>= [ (DESCRIPTION_LIST =  # Optional depending on whether u have 
    # one or more descriptions
    # If there is just one description, unnecessary ]
   (DESCRIPTION=
     [ (SDU=2048) ] # Optional, defaults to 2048
    # Can take values between 512 and 32K
     [ (ADDRESS_LIST=    # Optional depending on whether u have
    # one or more addresses
    # If there is just one address, unnecessary ]
       (ADDRESS=
  [ (COMMUNITY=<community_name>) ] 
  (PROTOCOL=tcp)
  (HOST=<hostname>)
  (PORT=<portnumber (1521 is a standard port used)>)
       )
       [ (ADDRESS=
    (PROTOCOL=ipc)
    (KEY=<ipckey (PNPKEY is a standard key used)>) 
  )
       ]
       [ (ADDRESS=
    [ (COMMUNITY=<community_name>) ]
    (PROTOCOL=decnet)
    (NODE=<nodename>)
    (OBJECT=<objectname>)
  )
       ]
              ... # More addresses
     [ ) ] # Optional depending on whether ADDRESS_LIST is used or not 
     [ (CONNECT_DATA=
  (SID=<oracle_sid>)
  [ (GLOBAL_NAME=<global_database_name>) ]
       )
     ]
     [ (SOURCE_ROUTE=yes) ]  
   )
   (DESCRIPTION=     
     [ (SDU=2048) ] # Optional, defaults to 2048
    # Can take values between 512 and 32K
     [ (ADDRESS_LIST= ] # Optional depending on whether u have more
    # than one address or not
    # If there is just one address, unnecessary
       (ADDRESS
  [ (COMMUNITY=<community_name>) ]
  (PROTOCOL=tcp)
  (HOST=<hostname>)
  (PORT=<portnumber (1521 is a standard port used)>)
       )
       [ (ADDRESS=
    (PROTOCOL=ipc)
    (KEY=<ipckey (PNPKEY is a standard key used)>)
          )
       ]
       ...   # More addresses
     [ ) ]   # Optional depending on whether ADDRESS_LIST  
    # is being used
     [ (CONNECT_DATA=
  (SID=<oracle_sid>)
  [ (GLOBAL_NAME=<global_database_name>) ]
       )
     ]
     [ (SOURCE_ROUTE=yes) ]
   )
   [ (CONNECT_DATA=
       (SID=<oracle_sid>)
       [ (GLOBAL_NAME=<global_database_name>) ]
     )
   ]
   ...   # More descriptions 
 [ ) ] # Optional depending on whether DESCRIPTION_LIST is used or not

in a real application it might look like this:

VB.NET:
# TNSNAMES.ORA Network Configuration File: C:\oracle\ora92\NETWORK\ADMIN\tnsnames.ora
# Generated by Oracle configuration tools.

GTPD.G-T-P.LOCAL =
  (DESCRIPTION =
    (ADDRESS_LIST =
      (ADDRESS = (PROTOCOL = TCP)(HOST = testserver)(PORT = 1521))
    )
    (CONNECT_DATA =
      (SERVICE_NAME = GTPD)
    )
  )

in my connect string i simply provide GTPD as the datasource, though differening network configurations require that sometimes GTPD.G-T-P.LOCAL is specified.


Additionally, there is a SQLNET.ORA file that looks like this:

VB.NET:
# SQLNET.ORA Network Configuration File: C:\oracle\ora92\network\admin\sqlnet.ora
# Generated by Oracle configuration tools.
NAMES.DEFAULT_DOMAIN = G-T-P.LOCAL
SQLNET.AUTHENTICATION_SERVICES= (NONE)
NAMES.DIRECTORY_PATH= (TNSNAMES, ONAMES, HOSTNAME)

it is likely that systems requiring the fully specified name do not have this SQLNET.ORA

In this SQLNET.ORA there is also a specifier of how authentication is handled with the oracle database. We have encountered issues with username/password authentication when
SQLNET.AUTHENTICATION_SERVICES= (NONE) was set to some other value. The sample SQLNET.ORA looks like:

VB.NET:
# - Oracle Advanced Security Authentication Adapters ----------------
#sqlnet.authentication_services
#
# Syntax: A single value or a list from {beq, none, all, kerberos5,
#   radius, nts}
# Default: NONE
#
# Enables one or more authentication services.  If
# Oracle Advanced Security has been installed with Kerberos5
# support, using (beq, kerberos5) would enable authentication via
# Kerberos.
#
#sqlnet.authentication_services=(beq, kerberos5)
for this entry. if you have these auth services enabled, simple username/password may be rejected.

On my system the SQLNET and TNSNAMES are found in
C:\oracle\product\10.2.0\client\network\ADMIN

if you have a different client version installed you may have a different path. however the paths are arranged, they should end in network\ADMIN

I hope this will be enough information for you to investigate your system and possibly switch to using TNS names to test whether there is a problem elsewhere. I have no experience of forming an oracle connection string with anything other than a TNSname, but I will attempt to do so shortly for additional understanding of your problem
 
Correct, I have been setting up my original connection string using the tnsnames.ora file. the problem is that the length that the data source is at is too long, it only allows 128 characters, and mine for some reason is over that number. I tried just adding the Connection string that I had made in the begining, but the System.Oracle.Client doesn't like the length.

I thought that there was just an additional part to this string:
VB.NET:
[SIZE=2][COLOR=#008000]
"Data Source=XE;User ID=uid;Password=pwd;Persist Security Info=True;Unicode=True"
[/COLOR][/SIZE]
 
The TNS name you have listed is two characters long. This is somewhat less than the 128 character maximum you infer. I suggest you post a copy of your TNSNAMES file so that I may review it. Additionally you say "it is too long" but you list no evidence as to how you arrive at this conclusion. If it is an error message from the IDE, it would help to know what line of your code it occurs on, and the StackTrace of the exception if indeed it is, an exception.
 
It is an error message that I get, here is my tnsnames.ora file as of right now:

VB.NET:
XE =
  (DESCRIPTION =
    (ADDRESS = (PROTOCOL = TCP)(HOST = rsgis-summer2.erd.ds.usace.army.mil)(PORT = 1521))
    (CONNECT_DATA =
      (SERVER = DEDICATED)
      (SERVICE_NAME = XE)
    )
  )
EXTPROC_CONNECTION_DATA =
  (DESCRIPTION =
    (ADDRESS_LIST =
      (ADDRESS = (PROTOCOL = IPC)(KEY = EXTPROC_FOR_XE))
    )
    (CONNECT_DATA =
      (SID = PLSExtProc)
      (PRESENTATION = RO)
    )
  )
ORACLR_CONNECTION_DATA = 
  (DESCRIPTION = 
    (ADDRESS_LIST = 
      (ADDRESS = (PROTOCOL = IPC)(KEY = EXTPROC_FOR_XE)) 
    ) 
    (CONNECT_DATA = 
      (SID = CLRExtProc) 
      (PRESENTATION = RO) 
    ) 
  )

I tried the string that I gave above, and it did not work properly, it said as I stated above that it was an invalid username/password, wich told me that it wasn't talking to the right server.

Now my question is, what can I change of the below code for it to be the right connection string:

VB.NET:
[SIZE=2][/SIZE][SIZE=2][COLOR=#0000ff]Dim[/COLOR][/SIZE][SIZE=2] oradb [/SIZE][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][SIZE=2] [/SIZE][SIZE=2][COLOR=#0000ff]String[/COLOR][/SIZE][SIZE=2] = "Data Source=(DESCRIPTION=(ADDRESS_LIST=" _
+ "(ADDRESS=(PROTOCOL=TCP)(HOST=" + host + ")(PORT=152 1)))" _
+ "(CONNECT_DATA=(SERVER=DEDICATED)(SERVICE_NAME=XE)));" _
+ "User Id=" + username + ";Password=" + password + ";"
[/SIZE]
 
ugh..

the following connection string is correct:

"Data Source=XE;User ID=uid;Password=pwd;Persist Security Info=True;Unicode=True"

but it indicates that the username and password for your database are UID and PWD respectively. I find this hard to believe but as noted before I do not know the server in question. All oracle instances come with a test user called scott whose password is tiger. Try this connection string:

"Data Source=XE;User ID=scott;Password=tiger;Persist Security Info=True;Unicode=True"
 
Back
Top