0% found this document useful (0 votes)
43 views6 pages

Manu Dubey 18840 Entity Framework Assignment

The document discusses four stored procedures created in SQL Server: 1. GetAnnualSalaryByID retrieves annual salary by employee ID through an output parameter. 2. updateEmpNameByID updates the employee name by ID. 3. deleteEmpByID deletes an employee by ID. 4. InsertAnEmp inserts a new employee by passing all column values as parameters. The document also shows the C# code to call each stored procedure using Entity Framework Core.

Uploaded by

ManuDubey
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
43 views6 pages

Manu Dubey 18840 Entity Framework Assignment

The document discusses four stored procedures created in SQL Server: 1. GetAnnualSalaryByID retrieves annual salary by employee ID through an output parameter. 2. updateEmpNameByID updates the employee name by ID. 3. deleteEmpByID deletes an employee by ID. 4. InsertAnEmp inserts a new employee by passing all column values as parameters. The document also shows the C# code to call each stored procedure using Entity Framework Core.

Uploaded by

ManuDubey
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 6

Manu Dubey 18840 Entity Framework Assignment

Que 1 Create a stored procedure which accepts empno as input and returns annual salary though
out mode parameter

Call procedure in entity framework core

SQL Code
ALTER procedure [dbo].[GetAnnualSalaryByID](@empno int, @sal int output)
as
begin
select @sal = sal*12
from emp
WHERE EmpNo = @empno
end

VS Entity Code

using EntityFWCoreAssignmentII.Models;
using Microsoft.Data.SqlClient;
using Microsoft.EntityFrameworkCore;
using System;

namespace EntityFWCoreAssignmentII
{
class Program
{
static void Main(string[] args)
{
GetAnnualSalaryByID(18840);

private static void GetAnnualSalaryByID(int id)


{
try
{
var ctx = new TRAININGContext();

var param = new SqlParameter[]


{
new SqlParameter() {
ParameterName = "@empno",
SqlDbType = System.Data.SqlDbType.Int,
Direction = System.Data.ParameterDirection.Input,
Value = id
},
new SqlParameter() {
ParameterName = "@sal",
SqlDbType = System.Data.SqlDbType.Int,
Direction = System.Data.ParameterDirection.Output
}
};

ctx.Database.ExecuteSqlRaw("GetAnnualSalaryByID @empno, @sal out",


param);

Console.WriteLine($"Emp sal is {param[1].Value}");


}
catch (Exception ex)
{
Console.WriteLine(ex.Message); ;
}
}
}
}

2. create a SP which accepts the empno as paramter and update the empname

call this sp in ef core

SQL Code

ALTER procedure [dbo].[updateEmpNameByID](@empno int, @ename varchar(20))


as
begin
update emp
set EName = @ename
WHERE EmpNo = @empno
end

VS Entity Code

using EntityFWCoreAssignmentII.Models;
using Microsoft.Data.SqlClient;
using Microsoft.EntityFrameworkCore;
using System;

namespace EntityFWCoreAssignmentII
{
class Program
{
static void Main(string[] args)
{
updateEmpNameByID(18840, "Manu Dubey");

private static void updateEmpNameByID(int id, string name)


{
try
{
var ctx = new TRAININGContext();

var param = new SqlParameter[]


{
new SqlParameter() {
ParameterName = "@empno",
SqlDbType = System.Data.SqlDbType.Int,
Size = 100,
Direction = System.Data.ParameterDirection.Input,
Value = id
},
new SqlParameter() {
ParameterName = "@ename",
SqlDbType = System.Data.SqlDbType.VarChar,
Size = 100,
Direction = System.Data.ParameterDirection.Input,
Value = name
}
};

ctx.Database.ExecuteSqlRaw("updateEmpNameByID @empno,@ename",
param);
Console.WriteLine($"{param[0].Value} id's Name is updated to
{param[1].Value}");
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
}
}
}
}

3. create a SP which accepts the empno as paramter and which delete the emp

SQL Code

ALTER procedure [dbo].[deleteEmpByID](@empno int)


as
begin
delete from emp
where Empno = @empno
end
VS Entity Code

using EntityFWCoreAssignmentII.Models;
using Microsoft.Data.SqlClient;
using Microsoft.EntityFrameworkCore;
using System;

namespace EntityFWCoreAssignmentII
{
class Program
{
static void Main(string[] args)
{
deleteEmpByID(18840);

private static void deleteEmpByID(int id)


{
try
{
var ctx = new TRAININGContext();

var param = new SqlParameter[]


{
new SqlParameter() {
ParameterName = "@empno",
SqlDbType = System.Data.SqlDbType.Int,
Size = 100,
Direction = System.Data.ParameterDirection.Input,
Value = id
}
};

ctx.Database.ExecuteSqlRaw("deleteEmpByID @empno", param);


Console.WriteLine($"Employee deleted from database for employee id
{param[0]}");

}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
}
}
}
}

4. create a SP which which accepts all the column values and insert

SQL Code

ALTER procedure [dbo].[InsertAnEmp](@empno int , @ename varchar(10),@job varchar(9),


@mgr int, @hiredate Date, @sal int, @comm int, @deptno int)
as
begin
Insert into EMP Values (@empno,@ename,@job,@mgr,@hiredate,@sal,@comm,@deptno)
end

VS Entity Code

using EntityFWCoreAssignmentII.Models;
using Microsoft.Data.SqlClient;
using Microsoft.EntityFrameworkCore;
using System;

namespace EntityFWCoreAssignmentII
{
class Program
{
static void Main(string[] args)
{
InsertAnEmp(18841, "Manu D", "TTS", 7839, new DateTime(2020, 11, 02),
3001, 101, 10);
}

private static void InsertAnEmp(int id, string name, string job, int mgr,
DateTime hiredate, int sal, int comm, int deptno)
{
try
{
var ctx = new TRAININGContext();
var param = new SqlParameter[]
{
new SqlParameter() {
ParameterName = "@empno",
SqlDbType = System.Data.SqlDbType.Int,
Size = 100,
Direction = System.Data.ParameterDirection.Input,
Value = id
},
new SqlParameter() {
ParameterName = "@ename",
SqlDbType = System.Data.SqlDbType.VarChar,
Size = 100,
Direction = System.Data.ParameterDirection.Input,
Value = name
},
new SqlParameter() {
ParameterName = "@job",
SqlDbType = System.Data.SqlDbType.VarChar,
Size = 100,
Direction = System.Data.ParameterDirection.Input,
Value = job
},
new SqlParameter() {
ParameterName = "@mgr",
SqlDbType = System.Data.SqlDbType.Int,
Size = 100,
Direction = System.Data.ParameterDirection.Input,
Value = mgr
},
new SqlParameter() {
ParameterName = "@hiredate",
SqlDbType = System.Data.SqlDbType.DateTime,
Size = 100,
Direction = System.Data.ParameterDirection.Input,
Value = hiredate
},
new SqlParameter() {
ParameterName = "@sal",
SqlDbType = System.Data.SqlDbType.Int,
Size = 100,
Direction = System.Data.ParameterDirection.Input,
Value = sal
},
new SqlParameter() {
ParameterName = "@comm",
SqlDbType = System.Data.SqlDbType.Int,
Size = 100,
Direction = System.Data.ParameterDirection.Input,
Value = comm
},
new SqlParameter() {
ParameterName = "@deptno",
SqlDbType = System.Data.SqlDbType.Int,
Size = 100,
Direction = System.Data.ParameterDirection.Input,
Value = deptno
},
};

ctx.Database.ExecuteSqlRaw("InsertAnEmp
@empno,@ename,@job,@mgr,@hiredate,@sal,@comm,@deptno", param);
Console.WriteLine($"A new Employee is added with id
={param[0].Value}, Name = {param[1].Value}, is added as a {param[2].Value},
reporting to {param[3].Value}, hired on {param[4].Value}, getting salary
{param[5].Value}, earning commision of{param[6].Value}, working in dept no
{param[7].Value}");
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
}
}
}
}

You might also like