The new Oracle managed provider provides optimized methods to access the Oracle 9i data store. Let’s see how we can do the same. We have a simple stored procedure which updates a particular table. The stored procedure accepts three parameters, two of them are input parameters and one is an output parameter.
The stored procedure checks that the update statement is executed successfully and if yes then, the OUT parameter status is set to TRUE else it is set to FALSE.
CREATE OR REPLACE PROCEDURE sp_updateBalance(accNo number, amount number, status OUT varchar2) You can run the above code in SQL*PLUS and the procedure is created. Of course, you should have the table Customer_Account with at least the required fields i.e Here is the C# code (console.cs)
// import the required assemblies class StoredProc //create an instance of the command object giving the procedure name // Define the command type u r executing as a Stored Procedure. //Add the parameter "accNo" giving it's value and defining it as a Input parameter //Add the parameter "amount" giving it's value and defining it as a Input parameter //Add the parameter "status" giving it's value and defining it as a Output parameter // using the Try Catch Finally Block. // giving screen output // execute the stored procedure // get the confirmation on the screen } // catch the error message and put it in the string "msg" //show the error message on the screen } } } You will have to compile this separately with the following command csc /r:System.Data.OracleClient.dll console.cs This creates console.exe, which you can run at command prompt. Running the above executable should give you the appropriate result.
Here is the code for the Stored Procedure
IS
BEGIN
UPDATE customer_account
SET balance = balance + amount
WHERE accountNo = accNo ;
if SQL%ROWCOUNT = 1 then
status := 'TRUE' ;
commit ;
else
status := 'FALSE' ;
end if ;
END ;
1. AccountNo which is a number and
2. Balance also a number.
Let's see the how can we execute it using .NET. Here, we will be a using a simple console application to execute the stored procedure. The language used is C#.
using System;
using System.Data;
using System.Data.OracleClient;
{
public static void Main()
{
// your Database connection string
string sDBstr = "" ;
// create an instance of the connection object
OracleConnection oCn = new OracleConnection(sDBstr) ;
OracleCommand oCm = new OracleCommand("sp_updateBalance",oCn) ;
oCm.CommandType = CommandType.StoredProcedure ;
oCm.Parameters.Add("accNo",OracleType.Number,16);
oCm.Parameters["accNo"].Value = 1 ;
oCm.Parameters["accNo"].Direction = ParameterDirection.Input ;
oCm.Parameters.Add("amount",OracleType.Number,16);
oCm.Parameters["amount"].Value = 200 ;
oCm.Parameters["amount"].Direction = ParameterDirection.Input ;
oCm.Parameters.Add("status",OracleType.VarChar,5);
oCm.Parameters["status"].Direction = ParameterDirection.Output ;
try
{
// Open the connection
oCn.Open();
Console.WriteLine("created connection") ;
oCm.ExecuteNonQuery() ;
Console.WriteLine(oCm.Parameters["status"].Value) ; ;
catch(Exception ex)
{
string msg = ex.Message ;
Console.WriteLine(msg) ;
finally
{
//Close the connection
oCn.Close();
oCn = null ;
oCm = null ;
}
Happy Coding !!!
Executing Oracle 9i Stored Procedures with Oracle Managed Data Provider
0 comments
Hide comments