473,757 Members | 8,085 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Anyone know how to call an Oracle function from C#?

I'm not even sure this can be done. I have a requirement to call an
Oracle function (that returns a value) from C# code. The Oracle
function is as follows:

FUNCTION get_rec_final_q tr_count
(
"P_YEAR" IN NUMBER,
"P_QUARTER" IN NUMBER
)
RETURN Int
IS
p_count Int;
BEGIN
SELECT COUNT(prl_pay_p eriod_payroll_i d) INTO p_count FROM
prl_pay_period_ payroll
WHERE year = p_year AND quarter = p_quarter AND quarter_end_rep ort =
'T';

RETURN p_count;
END get_rec_final_q tr_count;

The C# code I'm trying to use is as follows:

objConnect.Conn ect();
OracleCommand DBCmd =
new
OracleCommand(" HDB.PRL_PAY_PER IOD_PAYROLL_PKG .get_rec_final_ qtr_count",
objConnect.Conn ection);
DBCmd.CommandTy pe = System.Data.Com mandType.Stored Procedure;
DBCmd.Parameter s.

DBCmd.Parameter s.Add("P_YEAR", OracleDbType.In t32);
DBCmd.Parameter s["P_YEAR"].Direction =
System.Data.Par ameterDirection .Input;
DBCmd.Parameter s["P_YEAR"].Value = Year;

DBCmd.Parameter s.Add("P_QUARTE R", OracleDbType.In t32);
DBCmd.Parameter s["P_QUARTER"].Direction =
System.Data.Par ameterDirection .Input;
DBCmd.Parameter s["P_QUARTER"].Value = Quarter;

Count = (Int32)DBCmd.Ex ecuteScalar();

Am I doing something wrong?

Thanks for your help.
Steve

Jan 18 '07 #1
3 13857


"Steve Kershaw" <st***********@ yahoo.comwrote in message
news:11******** **************@ s34g2000cwa.goo glegroups.com.. .
I'm not even sure this can be done. I have a requirement to call an
Oracle function (that returns a value) from C# code. The Oracle
function is as follows:

FUNCTION get_rec_final_q tr_count
(
"P_YEAR" IN NUMBER,
"P_QUARTER" IN NUMBER
)
RETURN Int
IS
p_count Int;
BEGIN
SELECT COUNT(prl_pay_p eriod_payroll_i d) INTO p_count FROM
prl_pay_period_ payroll
WHERE year = p_year AND quarter = p_quarter AND quarter_end_rep ort =
'T';

RETURN p_count;
END get_rec_final_q tr_count;

The C# code I'm trying to use is as follows:

objConnect.Conn ect();
OracleCommand DBCmd =
new
OracleCommand(" HDB.PRL_PAY_PER IOD_PAYROLL_PKG .get_rec_final_ qtr_count",
objConnect.Conn ection);
....

You need to add a parameter for the return value, use ExecuteNonQuery and
then extract the parameter value.

Like this.

DBCmd.CommandTy pe = System.Data.Com mandType.Stored Procedure;

DBCmd.Parameter s.Add("P_YEAR", OracleType.Int3 2);
DBCmd.Parameter s["P_YEAR"].Direction = ParameterDirect ion.Input;
DBCmd.Parameter s["P_YEAR"].Value = 2007;

DBCmd.Parameter s.Add("P_QUARTE R", OracleType.Int3 2);
DBCmd.Parameter s["P_QUARTER"].Direction = ParameterDirect ion.Input;
DBCmd.Parameter s["P_QUARTER"].Value = 2;

DBCmd.Parameter s.Add("P_RV", OracleType.Int3 2);
DBCmd.Parameter s["P_RV"].Direction = ParameterDirect ion.ReturnValue ;

DBCmd.ExecuteNo nQuery();
int Count = (int)DBCmd.Para meters["P_RV"].Value;
Alternatively, you can use CommandType.Tex t, and use a complete PL/SQL block
with parameter markers, then bind parameters into that and execute it.
CommandType.Sto redProcedure just tells the Oracle library to build the
PL/SQL block for you.

David
Jan 18 '07 #2
David,

Thanks that worked! However, it only works with
System.Data.Ora cleClient NOT Oracle.DataAcce ss.Client. When will Oracle
get with the program!?

Steve

David Browne wrote:
"Steve Kershaw" <st***********@ yahoo.comwrote in message
news:11******** **************@ s34g2000cwa.goo glegroups.com.. .
I'm not even sure this can be done. I have a requirement to call an
Oracle function (that returns a value) from C# code. The Oracle
function is as follows:

FUNCTION get_rec_final_q tr_count
(
"P_YEAR" IN NUMBER,
"P_QUARTER" IN NUMBER
)
RETURN Int
IS
p_count Int;
BEGIN
SELECT COUNT(prl_pay_p eriod_payroll_i d) INTO p_count FROM
prl_pay_period_ payroll
WHERE year = p_year AND quarter = p_quarter AND quarter_end_rep ort =
'T';

RETURN p_count;
END get_rec_final_q tr_count;

The C# code I'm trying to use is as follows:

objConnect.Conn ect();
OracleCommand DBCmd =
new
OracleCommand(" HDB.PRL_PAY_PER IOD_PAYROLL_PKG .get_rec_final_ qtr_count",
objConnect.Conn ection);
...

You need to add a parameter for the return value, use ExecuteNonQuery and
then extract the parameter value.

Like this.

DBCmd.CommandTy pe = System.Data.Com mandType.Stored Procedure;

DBCmd.Parameter s.Add("P_YEAR", OracleType.Int3 2);
DBCmd.Parameter s["P_YEAR"].Direction = ParameterDirect ion.Input;
DBCmd.Parameter s["P_YEAR"].Value = 2007;

DBCmd.Parameter s.Add("P_QUARTE R", OracleType.Int3 2);
DBCmd.Parameter s["P_QUARTER"].Direction = ParameterDirect ion.Input;
DBCmd.Parameter s["P_QUARTER"].Value = 2;

DBCmd.Parameter s.Add("P_RV", OracleType.Int3 2);
DBCmd.Parameter s["P_RV"].Direction = ParameterDirect ion.ReturnValue ;

DBCmd.ExecuteNo nQuery();
int Count = (int)DBCmd.Para meters["P_RV"].Value;
Alternatively, you can use CommandType.Tex t, and use a complete PL/SQL block
with parameter markers, then bind parameters into that and execute it.
CommandType.Sto redProcedure just tells the Oracle library to build the
PL/SQL block for you.

David
Jan 19 '07 #3


"Steve Kershaw" <st***********@ yahoo.comwrote in message
news:11******** *************@3 8g2000cwa.googl egroups.com...
David,

Thanks that worked! However, it only works with
System.Data.Ora cleClient NOT Oracle.DataAcce ss.Client. When will Oracle
get with the program!?
Well, you can always just use CommandType.Tex t. And set command text to

string sql = @"
begin
:p_rv :=
HDB.PRL_PAY_PER IOD_PAYROLL_PKG .get_rec_final_ qtr_count(:p_ye ar,:p_quarter);
end;
";

This is the exact same block you can use in SqlPlus.
Then bind an output parameter and two input parameters. Be careful, ODP.NET
used positional parameter binding by default.
David
Jan 19 '07 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
4274
by: Bryan Jackson | last post by:
Greetings, (I am an Oracle newbie -- been working with SQLServer for quite some time, however. I'm using Oracle9i and Oracle9i JDeveloper v9.0.3.1 (build 1107) for my programming environment). I'm trying to get an Oracle function to return XML to a JSP page, but am having some problems (mostly Oracle errors). Let me start by showing you what I've done so far.
5
2502
by: hongky | last post by:
"pthread_delay_np" function can make a thread sleep, but it is a function in Unix and not POSIX, so i did not compile my code...what to do for me?? i just want to make a sleep for the thread, RedHat Linux thread lib has not pthread_delay_np function, how to do? thx.
12
9545
by: Newbie | last post by:
how can i call an oracle function to get data without using a select statement or stored procedures? given a project_no, i need to call the function: ops$sqltime.pa_new_job_no_fn which will return the next job_no thanks in advance.
2
1623
by: Newbie | last post by:
how can i call an oracle function? given a project_no, i need to call the function: ops$sqltime.pa_new_job_no_fn which will return the next job_no thanks in advance.
2
6677
by: Gary | last post by:
Hi Al I have the following parameters in an oracle function PACKAGE BODY ALLO A ------------------------------------------------------------------------------- FUNCTION ITEM (O_error_message OUT VARCHAR2 O_item IN OUT item.item%TYPE RETURN BOOLEAN IS......
4
29338
by: jens Jensen | last post by:
Hello, Assum i have an oracle function called ofunction. Can someone tell me how i can call this function assuming it take the parameter in_param of type clob. Any help will be highly appreciated C# .ADO.NET 2.0
2
14773
by: John | last post by:
My application needs to call Oracle function using oracle client 9.2. The oracle function returns boolean value from its returned parameter. The name space that I used is system.data.oracleclient. found out there is no boolean type in Oracle parameter in OracleType. How can I get the returned value from function call in my code? Thanks in advance
0
1955
by: One from | last post by:
I have a lot of Oracle function in packages and i would like use it in my ASP pages. With Oracle procedure it's all right. Sample like this .... set cn = Server.CreateObject("ADODB.Connection") connString = "Provider=MSDAORA.1;Data Source=<>;User ID=<>;Password=<>" cn.Open connString SQL = "{call test.test_asp({resultset 0, cResult})}" Set cmd = Server.CreateObject ("ADODB.Command")
0
9489
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9298
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10072
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
9885
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9737
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
8737
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7286
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5329
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
3
3399
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.