#1
You have two options to achieve this goal in .NET enviroment
- using System.Data.OracleClient namespace provided by .NET Framework
- using Oracle.DataAccess namespace provided by ORACLE
I chose No.1 becuase of simplicity. Don’t need to install any addons,.. all I need to do is typing some codes like followings….
using System.Data.OracleClient OracleCommand cmd = new OracleCommand("DeleteStoredProcedureName", connOracleConnectionObject); // ([ProcedureName],[OracleConnection]) param. cmd.CommandType = CommandType.StoredProcedure; OracleParameter p_ID = new OracleParameter("PrimaryKeyID", OracleType.Int32); p_ID.Value = _ID; // _ID is class level variable (aka field) that holds the id for the record to be deleted cmd.Parameters.Add(p_ID); OracleParameter p_ReturnValue = new OracleParameter("PrimaryKeyID",OracleType.Int32); p_ReturnValue.Direction = ParameterDirection.ReturnValue; // ReturnValue if it's a stored fuction. cmd.ExecuteNonQuery();
Ths point is System.Data.OracleClient part. which means you’re using classes provided from .Net.
Code is pretty simple. You make a connection. You make a OracleCommand and specify the CommandType. And you make parameters in right way.
Finally you Execute it. (There are diffrent methods for query execution depends on the type of query, find it in MSDN)And you may see an error-code
‘ORA-06502: PL/SQL: numeric or value error string…..‘
which is funcking funny since you put all parameters in right way. I spent fucking 2-days and googling to find out solution to resolve this problem. But never I did. So I changed my mind. I chose No.2 option.
Here is the code,..
using Oracle.DataAccess; using Oracle.DataAccess.Client; using Oracle.DataAccess.Types; OracleCommand cmd = new OracleCommand("DeleteStoredProcedureName", connOracleConnectionObject); // ([ProcedureName],[OracleConnection]) param. cmd.CommandType = CommandType.StoredProcedure; OracleParameter p_ID = new OracleParameter("PrimaryKeyID", OracleType.Int32); p_ID.Value = _ID; // _ID is class level variable (aka field) that holds the id for the record to be deleted cmd.Parameters.Add(p_ID); OracleParameter p_ReturnValue = new OracleParameter("ReturnValue",OracleType.Int32); p_ReturnValue.Direction = ParameterDirection.ReturnValue; // Choose ReturnValue if it's a stored fuction. cmd.ExecuteNonQuery();
See which namespaces I’m using…
Everything is pretty much same as example above.
But you may see this error
‘ORA-06550: line 1, column 7: PLS-00306: wrong number or types of arguments in call to ‘[StoredFunction]‘‘
I did googling, googling again and again… finally I found solution.
Some expert says that your code finally turned into this form
:rv = [Schema].[StoredFunction](:p1, :p2…);
which means you should add parameters in right order.
Here is the code.
using Oracle.DataAccess; using Oracle.DataAccess.Client; using Oracle.DataAccess.Types; OracleCommand cmd = new OracleCommand("DeleteStoredProcedureName", connOracleConnectionObject); // ([ProcedureName],[OracleConnection]) param. cmd.CommandType = CommandType.StoredProcedure; /* Add return Parameter before than any other parameter*/ OracleParameter p_ReturnValue = new OracleParameter("ReturnValue",OracleType.Int32); p_ReturnValue.Direction = ParameterDirection.ReturnValue; // Choose ReturnValue if it's a stored fuction. OracleParameter p_ID = new OracleParameter("PrimaryKeyID", OracleType.Int32); p_ID.Value = _ID; cmd.Parameters.Add(p_ID); cmd.ExecuteNonQuery();
and It will work like magic… T-T
Let’s summarize it.
- Use OLE Provider from Oracle.. namespace Oracle.DataAccess-Oracle Client and ODAC required(Download it from Oracle.com).-Don’t forget about TNSNAMES.ORA(If you know what it means).
- !Add Return Parameter First!!
- Specify the Size of Char and Varchar2 Parameters
