https://blog.csdn.net/xiaouncle/article/details/82914255html
相關文章:.net
https://www.cnblogs.com/Coder-ru/archive/2012/10/29/2744800.htmlhtm
https://www.cnblogs.com/tuqun/p/3661062.htmlblog
https://www.cnblogs.com/lyj/archive/2008/03/17/1110374.htmlstring
https://www.cnblogs.com/Coder-ru/archive/2012/10/29/2744800.htmlio
EF執行存儲過程與執行Sql語句很是相似,insert、delete、update操做經過ExecuteSqlCommand()執行,select操做經過SqlQuery<Sys_User>()執行。ast
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、執行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");
}
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<@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");
}
class