SlideShare a Scribd company logo
Hi friends
C# Tutorial
Part 38:
Stored procedure
www.siri-kt.blogspot.com
• Ado.net with stored procedure:
• Stored procedure maintains collection of sql statements.
Stored Procedures are a set of sql commands which are
compiled and are stored inside the database.
• Every time you execute a sql command, the command is
parsed, optimization is done and then the command is
executed.
• Parsing and optimization the command each time you run the
query is very expensive.
• To solve this we have a set of commands collectively called
as stored procedure, which are already parsed and optimized
and are executed when ever we call them.
• comparing with functions and with the stored procedure
improves the application.
• Ado.net with stored procedure:
• it is the collection of sql statements; comparing with functions the stored
procedure are very faster accessibility. stored procedure can returns
collections values. stored is pre-executed block of code. it executes the
first request only.
• second request onwards all requests communicates with the executed
block for getting the o/p.
• syntax:
• create procedure procedurename(@parameters) As begin
• sqlstatements;
• end
• ex:c=a+b;
• 30=10+20
• a=input
• b=input
• c=output
• stored procedure supports 3 types of parameters
• 1.In(input)
• 2.out(output type)
• 3.INOUt(input,output type)
• stored procedure is maintains 3 parameter values and sends every
parameter value in the parameter class.
• parameter class:
• this class is used to store one parameter from the stored procedures.
after storing parameter in the parameter class to set 3 values
• to each and every parameter
• 1.parameter datatype
• 2.parameter direction
• 3.parameter value
• how to create the stored procedure:
• insert student stored procedure:
• create procedure insertdata(@sno int,@sname
varchar(50),@saddress varchar(50))
• as
• begin
• insert into
student(sno,sname,saddress)values(@sno,@sname,
@saddress)
• end
• update student stored proceduere:
• create procedure updatedata(@sno int,@sname
varchar(50),@saddress varchar(50))
• as
• begin
• update student set sname=@sname,saddress=@saddress where
sno=@sno
• end
• delete student stored procedure:
• create procedure deletedata(@sno int)
• as
• begin
• delete from student where sno=@sno
• end
• Example on how to insert, update, delete values in
ado.net using
• Stored procedures.
• 1.take the one form
• 2.add 3 labels,3 text boxes and 3 buttons
• Connection flow:
• connection->con.open()->command->commandtext->
commandtype -> parameter datatype ->parameter
direction-> parameter value ->
cmd.executenonquery() -> UI
• using system.data;
• SqlConnection con = new SqlConnection("data source=sit-pc;database=suhashini;user
id=sa;password=123");
• SqlCommand cmd = new SqlCommand(); SqlParameter p;
• private void button1_Click(object sender, EventArgs e)
• { con.Open(); cmd.Connection = con;
• cmd.CommandText = "insertdata";//sp name
• cmd.CommandType = CommandType.StoredProcedure;//sp type
• p = cmd.Parameters.Add("@sno", SqlDbType.Int);//parameter datatype
• p.Direction = ParameterDirection.Input;//parameter direction
• p.Value = Convert.ToInt16(textBox1.Text);//parameter value
• p = cmd.Parameters.Add("@sname", SqlDbType.VarChar);//parameter datatype
• p.Direction = ParameterDirection.Input;//parameter direction
• p.Value =textBox2.Text;//parameter value
• p = cmd.Parameters.Add("@saddress", SqlDbType.VarChar);//parameter
datatype
• p.Direction = ParameterDirection.Input;//parameter direction
• p.Value = textBox3.Text;//parameter value
• private void button2_Click(object sender, EventArgs e)
• { con.Open();
• cmd.Connection = con; cmd.CommandText = "updatedata";//sp name
• cmd.CommandType = CommandType.StoredProcedure;//sp type
• p = cmd.Parameters.Add("@sno", SqlDbType.Int);//parameter datatype
• p.Direction = ParameterDirection.Input;//parameter direction
• p.Value = Convert.ToInt16(textBox1.Text);//parameter value
• p = cmd.Parameters.Add("@sname", SqlDbType.VarChar);//parameter datatype
• p.Direction = ParameterDirection.Input;//parameter direction
• p.Value = textBox2.Text;//parameter value
• p = cmd.Parameters.Add("@saddress", SqlDbType.VarChar);//parameter
datatype
• p.Direction = ParameterDirection.Input;//parameter direction
• p.Value = textBox3.Text;//parameter value
• cmd.ExecuteNonQuery(); MessageBox.Show("values are updated");
• }
• private void button3_Click(object sender, EventArgs e)
• { con.Open();
• cmd.Connection = con;
• cmd.CommandText = "deletedata";//sp name
• cmd.CommandType = CommandType.StoredProcedure;//sp
type
• p = cmd.Parameters.Add("@sno", SqlDbType.Int);//parameter
datatype
• p.Direction = ParameterDirection.Input;//parameter
direction
• p.Value = Convert.ToInt16(textBox1.Text);//parameter
value
• cmd.ExecuteNonQuery();
• MessageBox.Show("values are deleted");
• Example 2:
• how to add 2 numbers using stored procedure.
• 1.take the form
• 2.add 3 labels,3text boxes and only one button
• add 2 two numbers stored procedures:
• create procedure getvalues(@a int,@b int,@c int out)
• as
• begin
• select @c=@a+@b;
• end;
• source code:
• SqlConnection con = new SqlConnection("data source=sit-
pc;database=suhashini;user id=sa;password=123");
• SqlCommand cmd = new SqlCommand();
• SqlParameter p;
• private void button1_Click(object sender, EventArgs e)
• { con.Open(); cmd.Connection = con;
• cmd.CommandText = "getvalues";//sp name
• cmd.CommandType = CommandType.StoredProcedure;//sp type
• p = cmd.Parameters.Add("@a", SqlDbType.Int);//parameter datatype
• p.Direction = ParameterDirection.Input;//parameter direction
• p.Value = Convert.ToInt16(textBox1.Text);//parameter value
• p = cmd.Parameters.Add("@b", SqlDbType.Int);//parameter datatype
• p.Direction = ParameterDirection.Input;//parameter direction
• p.Value = Convert.ToInt16(textBox2.Text);//parameter value
• p = cmd.Parameters.Add("@c", SqlDbType.Int);//parameter datatype
• p.Direction = ParameterDirection.Output;//parameter direction
• cmd.ExecuteNonQuery();
• textBox3.Text = cmd.Parameters["@c"].Value.ToString();
• cmd.ExecuteNonQuery();
• MessageBox.Show("values are added");
• } }
For more visit our website www.siri-kt.blogspot.com
Thanks for
Watching
More Angular JS TutorialsMore C sharp (c#) tutorials

More Related Content

DOCX
Week 12 code
abhi7692271
 
PPTX
Introduction to MySQL in PHP
hamsa nandhini
 
DOCX
Logincode
RidwanSomali
 
PPT
FMDB - SLC-Cocoaheads
Dave Stevenson
 
PPTX
Single page application 05
Ismaeel Enjreny
 
PDF
C++ Windows Forms L07 - Collections
Mohammad Shaker
 
PPT
2310 b 11
Krazy Koder
 
PPTX
37c
Sireesh K
 
Week 12 code
abhi7692271
 
Introduction to MySQL in PHP
hamsa nandhini
 
Logincode
RidwanSomali
 
FMDB - SLC-Cocoaheads
Dave Stevenson
 
Single page application 05
Ismaeel Enjreny
 
C++ Windows Forms L07 - Collections
Mohammad Shaker
 
2310 b 11
Krazy Koder
 

Similar to 38c (20)

PDF
Stored procedure Notes By Durgesh Singh
imdurgesh
 
PPTX
Stored procedure
Deepak Sharma
 
PPTX
Sql server ___________session_18(stored procedures)
Ehtisham Ali
 
PPTX
Store procedures
Farzan Wadood
 
PDF
A set of SQL(structured query language) statements that are designat.pdf
info430661
 
PPTX
Sql storeprocedure
ftz 420
 
PPTX
Stored procedures with cursors
baabtra.com - No. 1 supplier of quality freshers
 
PPT
ASP.NET 09 - ADO.NET
Randy Connolly
 
PPTX
Stored procedures by thanveer danish melayi
Muhammed Thanveer M
 
PPTX
Chapter 3 stored procedures
baabtra.com - No. 1 supplier of quality freshers
 
DOCX
How to create Store Procedure
Durgaprasad Yadav
 
PPT
Intro to tsql
Syed Asrarali
 
PPT
Intro to tsql unit 14
Syed Asrarali
 
DOCX
Simple ado program by visual studio
Aravindharamanan S
 
DOCX
Simple ado program by visual studio
Aravindharamanan S
 
PPTX
Application of Insert and select notes.ppt x
doxafo4842
 
PPT
For Beginers - ADO.Net
Snehal Harawande
 
Stored procedure Notes By Durgesh Singh
imdurgesh
 
Stored procedure
Deepak Sharma
 
Sql server ___________session_18(stored procedures)
Ehtisham Ali
 
Store procedures
Farzan Wadood
 
A set of SQL(structured query language) statements that are designat.pdf
info430661
 
Sql storeprocedure
ftz 420
 
Stored procedures with cursors
baabtra.com - No. 1 supplier of quality freshers
 
ASP.NET 09 - ADO.NET
Randy Connolly
 
Stored procedures by thanveer danish melayi
Muhammed Thanveer M
 
How to create Store Procedure
Durgaprasad Yadav
 
Intro to tsql
Syed Asrarali
 
Intro to tsql unit 14
Syed Asrarali
 
Simple ado program by visual studio
Aravindharamanan S
 
Simple ado program by visual studio
Aravindharamanan S
 
Application of Insert and select notes.ppt x
doxafo4842
 
For Beginers - ADO.Net
Snehal Harawande
 
Ad

More from Sireesh K (20)

PPTX
Cn10
Sireesh K
 
PPTX
chanakya neeti
Sireesh K
 
PPTX
chanakya neeti
Sireesh K
 
DOCX
What is mvc
Sireesh K
 
PPTX
31c
Sireesh K
 
PPTX
31cs
Sireesh K
 
PPTX
45c
Sireesh K
 
PPTX
44c
Sireesh K
 
PPTX
43c
Sireesh K
 
PPTX
42c
Sireesh K
 
PPTX
41c
Sireesh K
 
PPTX
40c
Sireesh K
 
PPTX
39c
Sireesh K
 
PPTX
35c
Sireesh K
 
PPTX
34c
Sireesh K
 
PPTX
33c
Sireesh K
 
PPTX
30c
Sireesh K
 
PPTX
29c
Sireesh K
 
PPTX
27c
Sireesh K
 
PPTX
28c
Sireesh K
 
Cn10
Sireesh K
 
chanakya neeti
Sireesh K
 
chanakya neeti
Sireesh K
 
What is mvc
Sireesh K
 
31cs
Sireesh K
 
Ad

Recently uploaded (20)

PPTX
20250924 Navigating the Future: How to tell the difference between an emergen...
McGuinness Institute
 
DOCX
pgdei-UNIT -V Neurological Disorders & developmental disabilities
JELLA VISHNU DURGA PRASAD
 
PPTX
Artificial-Intelligence-in-Drug-Discovery by R D Jawarkar.pptx
Rahul Jawarkar
 
PPTX
Information Texts_Infographic on Forgetting Curve.pptx
Tata Sevilla
 
PPTX
Continental Accounting in Odoo 18 - Odoo Slides
Celine George
 
PPTX
How to Manage Leads in Odoo 18 CRM - Odoo Slides
Celine George
 
PPTX
INTESTINALPARASITES OR WORM INFESTATIONS.pptx
PRADEEP ABOTHU
 
PPTX
Cleaning Validation Ppt Pharmaceutical validation
Ms. Ashatai Patil
 
PDF
The Minister of Tourism, Culture and Creative Arts, Abla Dzifa Gomashie has e...
nservice241
 
DOCX
SAROCES Action-Plan FOR ARAL PROGRAM IN DEPED
Levenmartlacuna1
 
PPTX
How to Close Subscription in Odoo 18 - Odoo Slides
Celine George
 
PPTX
Basics and rules of probability with real-life uses
ravatkaran694
 
PDF
Antianginal agents, Definition, Classification, MOA.pdf
Prerana Jadhav
 
PPTX
PROTIEN ENERGY MALNUTRITION: NURSING MANAGEMENT.pptx
PRADEEP ABOTHU
 
PPTX
An introduction to Prepositions for beginners.pptx
drsiddhantnagine
 
PPTX
Applications of matrices In Real Life_20250724_091307_0000.pptx
gehlotkrish03
 
PPTX
CONCEPT OF CHILD CARE. pptx
AneetaSharma15
 
PPTX
Artificial Intelligence in Gastroentrology: Advancements and Future Presprec...
AyanHossain
 
PPTX
HISTORY COLLECTION FOR PSYCHIATRIC PATIENTS.pptx
PoojaSen20
 
PPTX
Dakar Framework Education For All- 2000(Act)
santoshmohalik1
 
20250924 Navigating the Future: How to tell the difference between an emergen...
McGuinness Institute
 
pgdei-UNIT -V Neurological Disorders & developmental disabilities
JELLA VISHNU DURGA PRASAD
 
Artificial-Intelligence-in-Drug-Discovery by R D Jawarkar.pptx
Rahul Jawarkar
 
Information Texts_Infographic on Forgetting Curve.pptx
Tata Sevilla
 
Continental Accounting in Odoo 18 - Odoo Slides
Celine George
 
How to Manage Leads in Odoo 18 CRM - Odoo Slides
Celine George
 
INTESTINALPARASITES OR WORM INFESTATIONS.pptx
PRADEEP ABOTHU
 
Cleaning Validation Ppt Pharmaceutical validation
Ms. Ashatai Patil
 
The Minister of Tourism, Culture and Creative Arts, Abla Dzifa Gomashie has e...
nservice241
 
SAROCES Action-Plan FOR ARAL PROGRAM IN DEPED
Levenmartlacuna1
 
How to Close Subscription in Odoo 18 - Odoo Slides
Celine George
 
Basics and rules of probability with real-life uses
ravatkaran694
 
Antianginal agents, Definition, Classification, MOA.pdf
Prerana Jadhav
 
PROTIEN ENERGY MALNUTRITION: NURSING MANAGEMENT.pptx
PRADEEP ABOTHU
 
An introduction to Prepositions for beginners.pptx
drsiddhantnagine
 
Applications of matrices In Real Life_20250724_091307_0000.pptx
gehlotkrish03
 
CONCEPT OF CHILD CARE. pptx
AneetaSharma15
 
Artificial Intelligence in Gastroentrology: Advancements and Future Presprec...
AyanHossain
 
HISTORY COLLECTION FOR PSYCHIATRIC PATIENTS.pptx
PoojaSen20
 
Dakar Framework Education For All- 2000(Act)
santoshmohalik1
 

38c

  • 1. Hi friends C# Tutorial Part 38: Stored procedure www.siri-kt.blogspot.com
  • 2. • Ado.net with stored procedure: • Stored procedure maintains collection of sql statements. Stored Procedures are a set of sql commands which are compiled and are stored inside the database. • Every time you execute a sql command, the command is parsed, optimization is done and then the command is executed. • Parsing and optimization the command each time you run the query is very expensive. • To solve this we have a set of commands collectively called as stored procedure, which are already parsed and optimized and are executed when ever we call them. • comparing with functions and with the stored procedure improves the application.
  • 3. • Ado.net with stored procedure: • it is the collection of sql statements; comparing with functions the stored procedure are very faster accessibility. stored procedure can returns collections values. stored is pre-executed block of code. it executes the first request only. • second request onwards all requests communicates with the executed block for getting the o/p. • syntax: • create procedure procedurename(@parameters) As begin • sqlstatements; • end • ex:c=a+b; • 30=10+20 • a=input • b=input • c=output
  • 4. • stored procedure supports 3 types of parameters • 1.In(input) • 2.out(output type) • 3.INOUt(input,output type) • stored procedure is maintains 3 parameter values and sends every parameter value in the parameter class. • parameter class: • this class is used to store one parameter from the stored procedures. after storing parameter in the parameter class to set 3 values • to each and every parameter • 1.parameter datatype • 2.parameter direction • 3.parameter value
  • 5. • how to create the stored procedure: • insert student stored procedure: • create procedure insertdata(@sno int,@sname varchar(50),@saddress varchar(50)) • as • begin • insert into student(sno,sname,saddress)values(@sno,@sname, @saddress) • end
  • 6. • update student stored proceduere: • create procedure updatedata(@sno int,@sname varchar(50),@saddress varchar(50)) • as • begin • update student set sname=@sname,saddress=@saddress where sno=@sno • end • delete student stored procedure: • create procedure deletedata(@sno int) • as • begin • delete from student where sno=@sno • end
  • 7. • Example on how to insert, update, delete values in ado.net using • Stored procedures. • 1.take the one form • 2.add 3 labels,3 text boxes and 3 buttons • Connection flow: • connection->con.open()->command->commandtext-> commandtype -> parameter datatype ->parameter direction-> parameter value -> cmd.executenonquery() -> UI
  • 8. • using system.data; • SqlConnection con = new SqlConnection("data source=sit-pc;database=suhashini;user id=sa;password=123"); • SqlCommand cmd = new SqlCommand(); SqlParameter p; • private void button1_Click(object sender, EventArgs e) • { con.Open(); cmd.Connection = con; • cmd.CommandText = "insertdata";//sp name • cmd.CommandType = CommandType.StoredProcedure;//sp type • p = cmd.Parameters.Add("@sno", SqlDbType.Int);//parameter datatype • p.Direction = ParameterDirection.Input;//parameter direction • p.Value = Convert.ToInt16(textBox1.Text);//parameter value • p = cmd.Parameters.Add("@sname", SqlDbType.VarChar);//parameter datatype • p.Direction = ParameterDirection.Input;//parameter direction • p.Value =textBox2.Text;//parameter value • p = cmd.Parameters.Add("@saddress", SqlDbType.VarChar);//parameter datatype • p.Direction = ParameterDirection.Input;//parameter direction • p.Value = textBox3.Text;//parameter value
  • 9. • private void button2_Click(object sender, EventArgs e) • { con.Open(); • cmd.Connection = con; cmd.CommandText = "updatedata";//sp name • cmd.CommandType = CommandType.StoredProcedure;//sp type • p = cmd.Parameters.Add("@sno", SqlDbType.Int);//parameter datatype • p.Direction = ParameterDirection.Input;//parameter direction • p.Value = Convert.ToInt16(textBox1.Text);//parameter value • p = cmd.Parameters.Add("@sname", SqlDbType.VarChar);//parameter datatype • p.Direction = ParameterDirection.Input;//parameter direction • p.Value = textBox2.Text;//parameter value • p = cmd.Parameters.Add("@saddress", SqlDbType.VarChar);//parameter datatype • p.Direction = ParameterDirection.Input;//parameter direction • p.Value = textBox3.Text;//parameter value • cmd.ExecuteNonQuery(); MessageBox.Show("values are updated"); • }
  • 10. • private void button3_Click(object sender, EventArgs e) • { con.Open(); • cmd.Connection = con; • cmd.CommandText = "deletedata";//sp name • cmd.CommandType = CommandType.StoredProcedure;//sp type • p = cmd.Parameters.Add("@sno", SqlDbType.Int);//parameter datatype • p.Direction = ParameterDirection.Input;//parameter direction • p.Value = Convert.ToInt16(textBox1.Text);//parameter value • cmd.ExecuteNonQuery(); • MessageBox.Show("values are deleted");
  • 11. • Example 2: • how to add 2 numbers using stored procedure. • 1.take the form • 2.add 3 labels,3text boxes and only one button • add 2 two numbers stored procedures: • create procedure getvalues(@a int,@b int,@c int out) • as • begin • select @c=@a+@b; • end; • source code: • SqlConnection con = new SqlConnection("data source=sit- pc;database=suhashini;user id=sa;password=123"); • SqlCommand cmd = new SqlCommand(); • SqlParameter p;
  • 12. • private void button1_Click(object sender, EventArgs e) • { con.Open(); cmd.Connection = con; • cmd.CommandText = "getvalues";//sp name • cmd.CommandType = CommandType.StoredProcedure;//sp type • p = cmd.Parameters.Add("@a", SqlDbType.Int);//parameter datatype • p.Direction = ParameterDirection.Input;//parameter direction • p.Value = Convert.ToInt16(textBox1.Text);//parameter value • p = cmd.Parameters.Add("@b", SqlDbType.Int);//parameter datatype • p.Direction = ParameterDirection.Input;//parameter direction • p.Value = Convert.ToInt16(textBox2.Text);//parameter value • p = cmd.Parameters.Add("@c", SqlDbType.Int);//parameter datatype • p.Direction = ParameterDirection.Output;//parameter direction • cmd.ExecuteNonQuery(); • textBox3.Text = cmd.Parameters["@c"].Value.ToString(); • cmd.ExecuteNonQuery(); • MessageBox.Show("values are added"); • } }
  • 13. For more visit our website www.siri-kt.blogspot.com Thanks for Watching More Angular JS TutorialsMore C sharp (c#) tutorials