Professional Documents
Culture Documents
D. HARSHA
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.
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
Comman d
.NET Applicatio n
Hour 67 - Page 1 of 8
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();
Hour 67 - Page 2 of 8
D. HARSHA
Implementation
of
Stored
Proc
Fun
Handling
with
System.Data.OleDb:
Import the API: using System.Data.OleDb; Prepare the Connection string:
string
source=<name
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
Hour 67 - Page 3 of 8
D. HARSHA
Assign the reference of Parameter class object(s) into cmd.Parameters.Add(pobj1); .; .; Open the connection: cn.Open();
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
D. HARSHA
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
D. HARSHA
Hour 67 - Page 6 of 8
D. HARSHA
Page 7 of 8
D. HARSHA
Hour 67 - Page 8 of 8