473,396 Members | 1,773 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

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_qtr_count
(
"P_YEAR" IN NUMBER,
"P_QUARTER" IN NUMBER
)
RETURN Int
IS
p_count Int;
BEGIN
SELECT COUNT(prl_pay_period_payroll_id) INTO p_count FROM
prl_pay_period_payroll
WHERE year = p_year AND quarter = p_quarter AND quarter_end_report =
'T';

RETURN p_count;
END get_rec_final_qtr_count;

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

objConnect.Connect();
OracleCommand DBCmd =
new
OracleCommand("HDB.PRL_PAY_PERIOD_PAYROLL_PKG.get_ rec_final_qtr_count",
objConnect.Connection);
DBCmd.CommandType = System.Data.CommandType.StoredProcedure;
DBCmd.Parameters.

DBCmd.Parameters.Add("P_YEAR", OracleDbType.Int32);
DBCmd.Parameters["P_YEAR"].Direction =
System.Data.ParameterDirection.Input;
DBCmd.Parameters["P_YEAR"].Value = Year;

DBCmd.Parameters.Add("P_QUARTER", OracleDbType.Int32);
DBCmd.Parameters["P_QUARTER"].Direction =
System.Data.ParameterDirection.Input;
DBCmd.Parameters["P_QUARTER"].Value = Quarter;

Count = (Int32)DBCmd.ExecuteScalar();

Am I doing something wrong?

Thanks for your help.
Steve

Jan 18 '07 #1
3 13676


"Steve Kershaw" <st***********@yahoo.comwrote in message
news:11**********************@s34g2000cwa.googlegr oups.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_qtr_count
(
"P_YEAR" IN NUMBER,
"P_QUARTER" IN NUMBER
)
RETURN Int
IS
p_count Int;
BEGIN
SELECT COUNT(prl_pay_period_payroll_id) INTO p_count FROM
prl_pay_period_payroll
WHERE year = p_year AND quarter = p_quarter AND quarter_end_report =
'T';

RETURN p_count;
END get_rec_final_qtr_count;

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

objConnect.Connect();
OracleCommand DBCmd =
new
OracleCommand("HDB.PRL_PAY_PERIOD_PAYROLL_PKG.get_ rec_final_qtr_count",
objConnect.Connection);
....

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

Like this.

DBCmd.CommandType = System.Data.CommandType.StoredProcedure;

DBCmd.Parameters.Add("P_YEAR", OracleType.Int32);
DBCmd.Parameters["P_YEAR"].Direction = ParameterDirection.Input;
DBCmd.Parameters["P_YEAR"].Value = 2007;

DBCmd.Parameters.Add("P_QUARTER", OracleType.Int32);
DBCmd.Parameters["P_QUARTER"].Direction = ParameterDirection.Input;
DBCmd.Parameters["P_QUARTER"].Value = 2;

DBCmd.Parameters.Add("P_RV", OracleType.Int32);
DBCmd.Parameters["P_RV"].Direction = ParameterDirection.ReturnValue;

DBCmd.ExecuteNonQuery();
int Count = (int)DBCmd.Parameters["P_RV"].Value;
Alternatively, you can use CommandType.Text, and use a complete PL/SQL block
with parameter markers, then bind parameters into that and execute it.
CommandType.StoredProcedure 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.OracleClient NOT Oracle.DataAccess.Client. When will Oracle
get with the program!?

Steve

David Browne wrote:
"Steve Kershaw" <st***********@yahoo.comwrote in message
news:11**********************@s34g2000cwa.googlegr oups.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_qtr_count
(
"P_YEAR" IN NUMBER,
"P_QUARTER" IN NUMBER
)
RETURN Int
IS
p_count Int;
BEGIN
SELECT COUNT(prl_pay_period_payroll_id) INTO p_count FROM
prl_pay_period_payroll
WHERE year = p_year AND quarter = p_quarter AND quarter_end_report =
'T';

RETURN p_count;
END get_rec_final_qtr_count;

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

objConnect.Connect();
OracleCommand DBCmd =
new
OracleCommand("HDB.PRL_PAY_PERIOD_PAYROLL_PKG.get_ rec_final_qtr_count",
objConnect.Connection);
...

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

Like this.

DBCmd.CommandType = System.Data.CommandType.StoredProcedure;

DBCmd.Parameters.Add("P_YEAR", OracleType.Int32);
DBCmd.Parameters["P_YEAR"].Direction = ParameterDirection.Input;
DBCmd.Parameters["P_YEAR"].Value = 2007;

DBCmd.Parameters.Add("P_QUARTER", OracleType.Int32);
DBCmd.Parameters["P_QUARTER"].Direction = ParameterDirection.Input;
DBCmd.Parameters["P_QUARTER"].Value = 2;

DBCmd.Parameters.Add("P_RV", OracleType.Int32);
DBCmd.Parameters["P_RV"].Direction = ParameterDirection.ReturnValue;

DBCmd.ExecuteNonQuery();
int Count = (int)DBCmd.Parameters["P_RV"].Value;
Alternatively, you can use CommandType.Text, and use a complete PL/SQL block
with parameter markers, then bind parameters into that and execute it.
CommandType.StoredProcedure 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*********************@38g2000cwa.googlegrou ps.com...
David,

Thanks that worked! However, it only works with
System.Data.OracleClient NOT Oracle.DataAccess.Client. When will Oracle
get with the program!?
Well, you can always just use CommandType.Text. And set command text to

string sql = @"
begin
:p_rv :=
HDB.PRL_PAY_PERIOD_PAYROLL_PKG.get_rec_final_qtr_c ount(:p_year,: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
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)....
5
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,...
12
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...
2
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
by: Gary | last post by:
Hi Al I have the following parameters in an oracle function PACKAGE BODY ALLO A ------------------------------------------------------------------------------- FUNCTION ITEM...
4
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...
2
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....
0
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")...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
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,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
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...
0
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...
0
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,...

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.