EF执行存储过程

摘要:
1//EF执行存储过程与执行Sql语句非常相似。2//插入、删除和更新操作通过ExecuteSqlCommand()执行,3//选择操作通过SqlQuery()执行1。执行插入存储过程(无返回值)1CREATEPROCEDURE[dbo][proc_AddSysUser01]@NameNVARCHAR(50),@PhoneNVARCHA
1 //EF执行存储过程与执行Sql语句非常类似,
2 //insert、delete、update操作通过ExecuteSqlCommand()执行,
3 //select操作通过SqlQuery<Sys_User>()执行

一、执行insert存储过程(无返回值)

EF执行存储过程第1张EF执行存储过程第2张
1 CREATE PROCEDURE [dbo].[proc_AddSysUser01] @Name NVARCHAR(50), @Phone NVARCHAR(50)
2 AS BEGIN
3     --SET NOCOUNT ON;
4 
5     -- Insert statements for procedure here
6     INSERT INTO Sys_User VALUES(@Name, @Phone, '耶路撒冷', GETDATE());
7 END;
View Code
EF执行存储过程第3张EF执行存储过程第4张
 1 public ActionResult ExecuteInsertProc(string name, string phone)
 2 {
 3     using(NHibernateContext context = new NHibernateContext())
 4     {
 5         SqlParameter pp_name = new SqlParameter("@Name", name);
 6         SqlParameter pp_phone = new SqlParameter("@Phone", phone);
 7         int count = context.Database.ExecuteSqlCommand("exec [proc_AddSysUser01] @Name,@Phone", pp_name, pp_phone);
 8         context.SaveChanges();
 9     }
10     return View("Index");
11 }
View Code

二、执行insert存储过程(out参数返回主键)

EF执行存储过程第5张EF执行存储过程第6张
 1 CREATE PROCEDURE [dbo].[proc_AddSysUser02]
 2     @Name nvarchar(50),
 3     @Phone nvarchar(50),
 4     @Id int output
 5 AS
 6 BEGIN
 7     --SET NOCOUNT ON;
 8 
 9     -- Insert statements for procedure here
10     insert into Sys_User
11     values
12         (@Name, @Phone, '安曼酒店', GETDATE());
13     select @Id=SCOPE_IDENTITY();
14 END
View Code
EF执行存储过程第7张EF执行存储过程第8张
 1 public ActionResult ExecuteInsertProc (string name, string phone) {
 2     using (NHibernateContext context = new NHibernateContext ()) {
 3         SqlParameter pp_id = new SqlParameter ("@Id", SqlDbType.Int);
 4         pp_id.Direction = ParameterDirection.Output;
 5         SqlParameter pp_name = new SqlParameter ("@Name", name);
 6         SqlParameter pp_phone = new SqlParameter ("@Phone", phone);
 7         //count值为1,out参数需要放在最后
 8         int count = context.Database.ExecuteSqlCommand ("exec [proc_AddSysUser02] @Name,@Phone,@Id out", pp_id, pp_name, pp_phone);
 9         //id值为10010
10         int id = int.Parse (pp_id.Value.ToString ());
11         context.SaveChanges ();
12     }
13     return View ("Index");
14 }
View Code

三、执行delete存储过程

EF执行存储过程第9张EF执行存储过程第10张
 1 CREATE PROCEDURE [dbo].[proc_DeleteSysUser]
 2     @Id int,
 3     @Name nvarchar(50)
 4 AS
 5 BEGIN
 6     --SET NOCOUNT ON;
 7 
 8     -- Insert statements for procedure here
 9     delete from Sys_User where Id>@Id and Name like '%'+@Name+'%'
10 END
View Code
EF执行存储过程第11张EF执行存储过程第12张
 1 public ActionResult ExecuteDeleteProc (int id, string name) {
 2     using (NHibernateContext context = new NHibernateContext ()) {
 3         SqlParameter pp_id = new SqlParameter ("@Id", id);
 4         SqlParameter pp_name = new SqlParameter ("@Name", name);
 5         //count值为2
 6         int count = context.Database.ExecuteSqlCommand ("exec [proc_DeleteSysUser] @Id,@Name", pp_id, pp_name);
 7         context.SaveChanges ();
 8     }
 9     return View ("Index");
10 }
View Code

四、执行update存储过程

EF执行存储过程第13张EF执行存储过程第14张
 1 CREATE PROCEDURE [dbo].[proc_UpdateSysUser]
 2     @Id int,
 3     @Name nvarchar(50),
 4     @Phone nvarchar(50)
 5 AS
 6 BEGIN
 7     --SET NOCOUNT ON;
 8 
 9     -- Insert statements for procedure here
10     update Sys_User set Phone=@Phone where Id>@Id and Name like '%'+@Name+'%'
11 END
View Code
EF执行存储过程第15张EF执行存储过程第16张
 1 public ActionResult ExecuteUpdateProc (int id, string name, string phone) {
 2     using (NHibernateContext context = new NHibernateContext ()) {
 3         SqlParameter pp_id = new SqlParameter ("@Id", id);
 4         SqlParameter pp_name = new SqlParameter ("@Name", name);
 5         SqlParameter pp_phone = new SqlParameter ("@Phone", phone);
 6         //count值为2
 7         int count = context.Database.ExecuteSqlCommand ("exec [proc_UpdateSysUser] @Id,@Name,@Phone", pp_id, pp_name, pp_phone);
 8         context.SaveChanges ();
 9     }
10     return View ("Index");
11 }
View Code

五、执行select存储过程

EF执行存储过程第17张EF执行存储过程第18张
 1 CREATE PROCEDURE [dbo].[proc_GetSysUser]
 2     @Id int,
 3     @Name nvarchar(50)
 4 AS
 5 BEGIN
 6     SET NOCOUNT ON;
 7 
 8     -- Insert statements for procedure here
 9     select *
10     from Sys_User
11     where Id<@Id and Name like '%'+@Name+'%'
12 END
View Code
EF执行存储过程第19张EF执行存储过程第20张
 1 public ActionResult ExecuteSelectProc (int id, string name) {
 2     using (NHibernateContext context = new NHibernateContext ()) {
 3         SqlParameter pp_id = new SqlParameter ("@Id", id);
 4         SqlParameter pp_name = new SqlParameter ("@Name", name);
 5         //userList.Count值为96
 6         List<Sys_User> userList = context.Database.SqlQuery<Sys_User> ("exec [proc_GetSysUser] @Id,@Name", pp_id, pp_name).Cast<Sys_User> ().ToList ();
 7         context.SaveChanges ();
 8     }
 9     return View ("Index");
10 }
View Code



免责声明:文章转载自《EF执行存储过程》仅用于学习参考。如对内容有疑问,请及时联系本站处理。

上篇ModelViewSet的使用PCB文件过大的解决方法下篇

宿迁高防,2C2G15M,22元/月;香港BGP,2C5G5M,25元/月 雨云优惠码:MjYwNzM=

相关文章

sql编程命名规范

< DOCTYPE html PUBLIC -WCDTD XHTML StrictEN httpwwwworgTRxhtmlDTDxhtml-strictdtd> 一、表名1、基础表(一般整个系统都要用到,三个子系统以上用到都算作基础表) ba开头,后面用英文单词,首字母大写,需要多个英文词的每个单词首字母大写 如baDepartment b...

Oracle基础 存储过程和事务

一、事务和存储过程   在存储过程中如何使用事务。当需要在存储过程中同时执行多条添加、修改、删除SQL语句时,为了保证数据完整性,我们需要使用事务。使用方式和在PL-SQL中非常相似,但也有一些区别。   --带事务的存储过程 CREATE OR REPLACE PROCEDURE Account_zhuanzhang(fromuser NUMBER,to...

Sqlserver存储过程及其创建(转)

存储过程可以使得对数据库的管理、以及显示关于数据库及其用户信息的工作容易得多。存储过程是 SQL 语句和可选控制流语句的预编译集合,以一个名称存储并作为一个单元处理。存储过程存储在数据库内,可由应用程序通过一个调用执行,而且允许用户声明变量、有条件执行以及其它强大的编程功能。 存储过程可包含程序流、逻辑以及对数据库的查询。它们可以接受参数、输出参数、返回单...

存储过程语法

CREATE PROCEDURE 创建存储过程,存储过程是保存起来的可以接受和返回用户提供的参数的 Transact-SQL 语句的集合。可以创建一个过程供永久使用,或在一个会话中临时使用(局部临时过程),或在所有会话中临时使用(全局临时过程)。也可以创建在 Microsoft SQL Server启动时自动运行的存储过程。 语法 CREATE PROC...

SqlDateTime 溢出。必须介于 1/1/1753 12:00:00 AM 和 12/31/9999 11:59:59 PM之间

错误场景:用ASP.NET制作web网站的添加用户功能时,调用写好的存储过程出错 错误提示: 出错原因:出现这种问题多半是因为你插入或者更新数据库时,datetime字段值为空默认插入0001年01月01日造成datetime类型溢出。 传给数据库表的时间类型值是null值。这里的null指的是程序代码中的null,多数出现这种情况的场景是:在程序里面定...

存储过程中SELECT INTO的使用

在MySQL存储过程中使用SELECT …INTO语句为变量赋值:   用来将查询返回的一行的各个列值保存到局部变量中。 要求:   查询的结果集中只能有1行。 SELECT col_name[,...] INTO var_name[,...] table_expr 使用SELECT …INTO语句在数据库中进行查询,并将得到的结果赋值给变量。   ①co...