EF执行存储过程(转载)

https://blog.csdn.net/xiaouncle/article/details/82914255

 相关文章:

https://www.cnblogs.com/Coder-ru/archive/2012/10/29/2744800.html

https://www.cnblogs.com/tuqun/p/3661062.html

https://www.cnblogs.com/lyj/archive/2008/03/17/1110374.html

https://www.cnblogs.com/Coder-ru/archive/2012/10/29/2744800.html

EF执行存储过程与执行Sql语句非常类似,insert、delete、update操作通过ExecuteSqlCommand()执行,select操作通过SqlQuery<Sys_User>()执行。

一、执行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");
}
二、执行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");
}
三、执行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");
}
四、执行update存储过程
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");
}
五、执行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<@Id and Name like '%'+@Name+'%'
END
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<Sys_User> userList = context.Database.SqlQuery<Sys_User>("exec [proc_GetSysUser] @Id,@Name", pp_id, pp_name).Cast<Sys_User>().ToList();
context.SaveChanges();
}
return View("Index");
}
 

原文地址:https://www.cnblogs.com/hao-1234-1234/p/11454689.html