Manu Dubey 18840 Entity Framework Assignment
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
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);
2. create a SP which accepts the empno as paramter and update the empname
SQL Code
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");
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
using EntityFWCoreAssignmentII.Models;
using Microsoft.Data.SqlClient;
using Microsoft.EntityFrameworkCore;
using System;
namespace EntityFWCoreAssignmentII
{
class Program
{
static void Main(string[] args)
{
deleteEmpByID(18840);
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
}
}
}
}
4. create a SP which which accepts all the column values and insert
SQL Code
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);
}
}
}
}