EF 저장 프로세스 실행
4602 단어 MVC+EF
1. insert 저장 프로세스를 실행합니다. (반환값 없음)
CREATE PROCEDURE [dbo].[proc_AddSysUser01]
@Name nvarchar(50),
@Phone nvarchar(50)
AS
BEGIN
--SET NOCOUNT ON;
-- Insert statements for procedure here
insert into Sys_User values(@Name,@Phone,' ',GETDATE())
END
public ActionResult ExecuteInsertProc(string name, string phone)
{
using(NHibernateContext context = new NHibernateContext())
{
SqlParameter pp_name = new SqlParameter("@Name", name);
SqlParameter pp_phone = new SqlParameter("@Phone", phone);
int count = context.Database.ExecuteSqlCommand("exec [proc_AddSysUser01] @Name,@Phone", pp_name, pp_phone);
context.SaveChanges();
}
return View("Index");
}
2. insert 저장 프로세스를 실행합니다 (out 매개 변수가 메인 키를 되돌려줍니다)
CREATE PROCEDURE [dbo].[proc_AddSysUser02]
@Name nvarchar(50),
@Phone nvarchar(50),
@Id int output
AS
BEGIN
--SET NOCOUNT ON;
-- Insert statements for procedure here
insert into Sys_User values (@Name,@Phone,' ',GETDATE());
select @Id=SCOPE_IDENTITY();
END
public ActionResult ExecuteInsertProc(string name, string phone)
{
using (NHibernateContext context = new NHibernateContext())
{
SqlParameter pp_id = new SqlParameter("@Id", SqlDbType.Int);
pp_id.Direction = ParameterDirection.Output;
SqlParameter pp_name = new SqlParameter("@Name", name);
SqlParameter pp_phone = new SqlParameter("@Phone", phone);
//count 1,out
int count = context.Database.ExecuteSqlCommand("exec [proc_AddSysUser02] @Name,@Phone,@Id out", pp_id, pp_name, pp_phone);
//id 10010
int id = int.Parse(pp_id.Value.ToString());
context.SaveChanges();
}
return View("Index");
}
3. delete 저장 프로세스 실행
CREATE PROCEDURE [dbo].[proc_DeleteSysUser]
@Id int,
@Name nvarchar(50)
AS
BEGIN
--SET NOCOUNT ON;
-- Insert statements for procedure here
delete from Sys_User where Id>@Id and Name like '%'+@Name+'%'
END
public ActionResult ExecuteDeleteProc(int id, string name)
{
using (NHibernateContext context = new NHibernateContext())
{
SqlParameter pp_id = new SqlParameter("@Id", id);
SqlParameter pp_name = new SqlParameter("@Name", name);
//count 2
int count = context.Database.ExecuteSqlCommand("exec [proc_DeleteSysUser] @Id,@Name", pp_id, pp_name);
context.SaveChanges();
}
return View("Index");
}
4. 업데이트 저장 프로세스 실행
CREATE PROCEDURE [dbo].[proc_UpdateSysUser]
@Id int,
@Name nvarchar(50),
@Phone nvarchar(50)
AS
BEGIN
--SET NOCOUNT ON;
-- Insert statements for procedure here
update Sys_User set Phone=@Phone where Id>@Id and Name like '%'+@Name+'%'
END
public ActionResult ExecuteUpdateProc(int id, string name, string phone)
{
using (NHibernateContext context = new NHibernateContext())
{
SqlParameter pp_id = new SqlParameter("@Id", id);
SqlParameter pp_name = new SqlParameter("@Name", name);
SqlParameter pp_phone = new SqlParameter("@Phone", phone);
//count 2
int count = context.Database.ExecuteSqlCommand("exec [proc_UpdateSysUser] @Id,@Name,@Phone", pp_id, pp_name, pp_phone);
context.SaveChanges();
}
return View("Index");
}
5. select 저장 프로세스 실행
CREATE PROCEDURE [dbo].[proc_GetSysUser]
@Id int,
@Name nvarchar(50)
AS
BEGIN
SET NOCOUNT ON;
-- Insert statements for procedure here
select * from Sys_User where Id
public ActionResult ExecuteSelectProc(int id, string name)
{
using (NHibernateContext context = new NHibernateContext())
{
SqlParameter pp_id = new SqlParameter("@Id", id);
SqlParameter pp_name = new SqlParameter("@Name", name);
//userList.Count 96
List userList = context.Database.SqlQuery("exec [proc_GetSysUser] @Id,@Name", pp_id, pp_name).Cast().ToList();
context.SaveChanges();
}
return View("Index");
}