Presentation is loading. Please wait.

Presentation is loading. Please wait.

Stored Procedures 2018-11-18.

Similar presentations


Presentation on theme: "Stored Procedures 2018-11-18."— Presentation transcript:

1 Stored Procedures

2 Stored Procedure – Förarbete
CREATE TABLE tbl_Students ( [Studentid] [int] IDENTITY(1,1) NOT NULL, [Firstname] [nvarchar](200) NOT NULL, [Lastname] [nvarchar](200) NULL, [ ] [nvarchar](100) NULL ) Insert into tbl_Students (Firstname, lastname, ) Values('Vivek', 'Johari', Values('Pankaj', 'Kumar', Values('Amit', 'Singh', Values('Manish', 'Kumar',

3 Hur ser en Stored Procedure ut?
Create Procedure Procedure-name ( Input parameters , Output Parameters (If required) ) As Begin Sql statement used in the stored procedure End

4 Exempel /* GetstudentnameInOutputVariable is the name of the stored procedure which uses output to collect the student name returns by the stored procedure */ Create PROCEDURE GetstudentnameInOutputVariable ( @studentid INT, -- Input parameter, Studentid of the student @studentname VARCHAR(200) OUT -- Out parameter declared with the help of OUT keyword ) AS BEGIN Firstname+' '+Lastname FROM tbl_Students WHERE END

5 /* This Stored procedure is used to Insert value into the table tbl_students. */ Create Procedure InsertStudentrecord Varchar(50) ) As Begin Insert into tbl_Students (Firstname, lastname, ) End

6 Hur ser det ut när vi använder C#
string connectionString ” + ”Initial Catalog=SPO12; ” + ”Integrated Security=True;"; SqlConnection conn = new SqlConnection(connectionString); SqlCommand cmd = new SqlCommand("dbo.GetstudentnameInOutputVariable"); try { cmd.CommandType = CommandType.StoredProcedure; cmd.Parameters.Add(new 1)); SqlParameter nameParameter = new 0); nameParameter.Direction = ParameterDirection.Output; cmd.Parameters.Add(nameParameter); conn.Open(); cmd.Connection = conn;

7 Hur ser det ut när vi använder C#
cmd.ExecuteNonQuery(); string studentName = Response.Write("<p>Namnet: " + studentName.ToString()); } Finaly { conn.Close();

8 Var kan jag finna mera? CodeProject StackOverFlow


Download ppt "Stored Procedures 2018-11-18."

Similar presentations


Ads by Google