You are on page 1of 8

VISION COMPUTERS VARDHAN

D. HARSHA

III) Stored Procedure / Function Handling

This includes with calling a procedure / function that is created at

backend database from frontend application. The procedures / functions can be created using PL/SQL at database. PL/SQL is supported by Sql Server, Oracle and MySql.

Purpose of Stored Procedures / Functions:


API:

To implement complex database logics. To retrieve multiple tables data at-a-time, with a single database call. To perform multiple query / non-query operations. To hide query / non-query statements in the code.

Connection: Maintains the connection with database. Command: Calls a database procedure / function. Parameter: Represents the argument value for the procedure /

function.

Connecti on

db

(provider, data Source, user id, password)

(proc / function name, paramete

Comman d

.NET Applicatio n

.NET 3.5 and Visual Studio 2008

Hour 67 - Page 1 of 8

VISION COMPUTERS VARDHAN


Implementation of Stored Proc / System.Data.SqlClient:
Import the API: using System.Data.SqlClient; Prepare the Connection string:

D. HARSHA
Fun Handling with

string cnstr = data source=<name of the server>;user id=<user name>;password=<password>;initial catalog=<database name>; Construct the Connection class object: SqlConnection cn = new SqlConnection(cnstr);

Construct the Command class object: SqlCommand cmd = new SqlCommand(); Assign the reference of Connection class object to

Command class object: cmd.Connection = cn; Assign the procedure / function name that is to be executed: cmd.CommandText = xxxxx; Assign the command type to Command class object: cmd.CommandType = CommandType.StoredProcedure; Construct the Parameter class object(s): SqlParameter pobj1 = new SqlParameter(parameter name,value); ; ;

Assign the reference of Parameter class object(s) into cmd.Parameters.Add(pobj1); .; .; Open the connection: cn.Open();

Command class object:

.NET 3.5 and Visual Studio 2008

Hour 67 - Page 2 of 8

VISION COMPUTERS VARDHAN

D. HARSHA

Execute the procedure / function: cmd.ExecuteNonQuery(); Close the connection: cn.Close();

Implementation

of

Stored

Proc

Fun

Handling

with

System.Data.OleDb:
Import the API: using System.Data.OleDb; Prepare the Connection string:

string

cnstr = string cnstr = provider=<provider name>;data of the server>;user id=<user

source=<name

name>;password=<password>; Construct the Connection class object: OleDbConnection cn = new OleDbConnection(cnstr);

Construct the Command class object: OleDbCommand cmd = new OleDbCommand(); Assign the reference of Connection class object to

Command class object: cmd.Connection = cn; Assign the procedure / function name that is to be executed: cmd.CommandText = xxxxx; Assign the command type to Command class object: cmd.CommandType = CommandType.StoredProcedure; Construct the Parameter class object(s): OleDbParameter name,value); ; ; pobj1 = new OleDbParameter(parameter

.NET 3.5 and Visual Studio 2008

Hour 67 - Page 3 of 8

VISION COMPUTERS VARDHAN

D. HARSHA

Assign the reference of Parameter class object(s) into cmd.Parameters.Add(pobj1); .; .; Open the connection: cn.Open();

Command class object:

Execute the procedure / function: cmd.ExecuteNonQuery(); Close the connection: cn.Close();

Application 130: Demo on Stored Procedure Calling


Create the following table at Sql Server in demo database and in oracle.

Create the following procedure at Sql Server in demo database.


create procedure calculateresults as begin

update students set result = 'Distinction' where marks>=80 update students set result = 'First Class' where marks>=60 and marks<80 update students set result = 'Second Class' where marks>=50 and marks<60 update students set result = 'Third Class' where marks>=35 and marks<50 update students set result = 'Fail' where marks<35 end Create the following procedure at Oracle. create procedure calculateresults is begin update students set result = 'Distinction' where marks>=80; update students set result = 'First Class' where marks>=60 and marks<80; update students set result = 'Second Class' where marks>=50 and marks<60; update students Studio = 'Third .NET 3.5 and Visual set result 2008 Class' where marks>=35 - Page Hour 67 and marks<50; update students set result = 'Fail' where marks<35; end;

4 of 8

VISION COMPUTERS VARDHAN

D. HARSHA

Sql Server Code


using System.Data.SqlClient; private void btnDisplayStudents_Click(object sender, EventArgs e) { string cnstr = "data source=.;user id=sa;password=123;initial catalog=demo"; SqlConnection cn = new SqlConnection(cnstr); string sqlstr = "select * from students"; SqlDataAdapter adp = new SqlDataAdapter(sqlstr, cn); DataSet ds = new DataSet(); DataTable dt; cn.Open(); adp.Fill(ds); dt = ds.Tables[0]; gridStudents.DataSource = dt; cn.Close(); } private void btnCalculateResults_Click(object sender, EventArgs e) Oracle Code { using System.Data.OleDb; source=.;user id=sa;password=123;initial string cnstr = "data catalog=demo"; btnDisplayStudents_Click(object sender, EventArgs e) private void { SqlConnection cn = new SqlConnection(cnstr); SqlCommand cmd = new SqlCommand(); string cnstr = "provider=oraoledb.oracle.1;data source=.;user cmd.Connection = cn; id=scott;password=tiger"; cmd.CommandText == new OleDbConnection(cnstr); OleDbConnection cn "calculateresults"; cmd.CommandType = CommandType.StoredProcedure; string sqlstr = "select * from students"; cn.Open(); OleDbDataAdapter adp = new OleDbDataAdapter(sqlstr, cn); cmd.ExecuteNonQuery(); DataSet ds = new DataSet(); MessageBox.Show("Results Calculated!"); DataTable dt; cn.Close(); cn.Open(); } adp.Fill(ds); dt = ds.Tables[0]; gridStudents.DataSource = dt; cn.Close();

private void btnCalculateResults_Click(object sender, EventArgs e) { string cnstr = "provider=oraoledb.oracle.1;data source=.;user id=scott;password=tiger"; OleDbConnection cn = new OleDbConnection(cnstr); OleDbCommand cmd = new OleDbCommand(); cmd.Connection = cn; cmd.CommandText = "calculateresults"; cmd.CommandType = CommandType.StoredProcedure; cn.Open(); cmd.ExecuteNonQuery(); MessageBox.Show("Results Calculated!"); .NET 3.5 and Visual Studio 2008 Hour 67 - Page cn.Close(); }

5 of 8

VISION COMPUTERS VARDHAN

D. HARSHA

Application 131: Demo on Stored Function Calling

.NET 3.5 and Visual Studio 2008

Hour 67 - Page 6 of 8

VISION COMPUTERS VARDHAN

D. HARSHA

Create the following function at Sql Server in demo database.


create function getmaximum(@a numeric,@b numeric,@c numeric) returns numeric as begin declare @big numeric if @a>@b and @a>@c set @big=@a else if @b>@a and @b>@c set @big=@b else set @big=@c return @big end

Create the following function at Oracle.


create function getmaximum(a number,b number,c number) return number is big number; begin if a>b and a>c then big:=a; elsif b>a and b>c then big:=b; else big:=c; end if; return big; end;

Sql Server Code


using System.Data.SqlClient; private void btnGetMaximum_Click(object sender, EventArgs e) { string cnstr = "data source=.;user id=sa;password=123;initial catalog=demo"; SqlConnection cn = new SqlConnection(cnstr); SqlCommand cmd = new SqlCommand(); cmd.Connection = cn; cmd.CommandText = "getmaximum"; cmd.CommandType = CommandType.StoredProcedure; int a = Convert.ToInt32(txtFirstValue.Text); int b = Convert.ToInt32(txtSecondValue.Text); int c = Convert.ToInt32(txtThirdValue.Text); SqlParameter p1 = new SqlParameter("@a", a); SqlParameter p2 = new SqlParameter("@b", b); cmd.Parameters.Add(p4); SqlParameter p3 = new SqlParameter("@c", c); cmd.Parameters.Add(p1); SqlParameter p4 = new SqlParameter(); cmd.Parameters.Add(p2); p4.Direction = ParameterDirection.ReturnValue; cmd.Parameters.Add(p3); cn.Open(); cmd.ExecuteNonQuery(); MessageBox.Show("Function Executed Successfully!"); .NET 3.5 and Visual Convert.ToString(p4.Value); Hour 67 txtResult.Text = Studio 2008 cn.Close(); }

Page 7 of 8

VISION COMPUTERS VARDHAN

D. HARSHA

Sql Server Code


using System.Data.OleDb; private void btnGetMaximum_Click(object sender, EventArgs e) { string cnstr = "provider=oraoledb.oracle.1;data source=.;user id=scott;password=tiger"; OleDbConnection cn = new OleDbConnection(cnstr); OleDbCommand cmd = new OleDbCommand(); cmd.Connection = cn; cmd.CommandText = "getmaximum"; cmd.CommandType = CommandType.StoredProcedure; int a = Convert.ToInt32(txtFirstValue.Text); int b = Convert.ToInt32(txtSecondValue.Text); int c = Convert.ToInt32(txtThirdValue.Text); OleDbParameter p1 = new OleDbParameter("a", a); OleDbParameter p2 = new OleDbParameter("b", b); OleDbParameter p3 = new OleDbParameter("c", c); OleDbParameter p4 = new OleDbParameter(); p4.Direction = ParameterDirection.ReturnValue; cmd.Parameters.Add(p4); cmd.Parameters.Add(p1); cmd.Parameters.Add(p2); cmd.Parameters.Add(p3); cn.Open(); cmd.ExecuteNonQuery(); MessageBox.Show("Function Executed Successfully!"); txtResult.Text = Convert.ToString(p4.Value); cn.Close(); } Note: While adding the parameter objects to the Command object, at first, you have to add the return value parameter object reference, before adding the other parameter objects.

.NET 3.5 and Visual Studio 2008

Hour 67 - Page 8 of 8

You might also like