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");
}

EF执行存储过程(转载)的更多相关文章

  1. EF执行存储过程(带输出参数)

    1.不含动态sql.带输出参数存储过程调用实例 1.存储过程代码:   2.EF自动生成代码(包括对应ObjectResult的实体模型): 3.调用存储过程代码实例: 总结: ObjectParam ...

  2. EF执行存储过程时超时问题

    异常信息:Message = EF "Timeout 时间已到.在操作完成之前超时时间已过或服务器未响应." ((IObjectContextAdapter);

  3. EF执行存储过程

    //执行strSql/procSql //返回受影响的行数 int i = dbsql.Database.ExecuteSqlCommand("exec getActionUrlId @na ...

  4. EF 执行存储过程

  5. easyui datagrid 禁止选中行 EF的增删改查(转载) C# 获取用户IP地址(转载) MVC EF 执行SQL语句(转载) 在EF中执行SQL语句(转载) EF中使用SQL语句或存储过程 .net MVC使用Session验证用户登录 PowerDesigner 参照完整性约束(转载)

    easyui datagrid 禁止选中行   没有找到可以直接禁止的属性,但是找到两个间接禁止的方式. 方式一: //onClickRow: function (rowIndex, rowData) ...

  6. MVC EF 执行SQL语句(转载)

    MVC EF 执行SQL语句 最近悟出来一个道理,在这儿分享给大家:学历代表你的过去,能力代表你的现在,学习代表你的将来. 十年河东十年河西,莫欺少年穷 学无止境,精益求精 闲着没事,看了一篇关于LI ...

  7. EF中执行存储过程,获取output返回值

    EF不能直接支持执行存储过程,于是使用转化成执行SQL语句的形式,却怎么也获取不到output的值,折腾的好久,终于解决了,分享下曲折的经历: public int AddVote(int title ...

  8. ASP.NET MVC5+EF6+EasyUI 后台管理系统(89)-EF执行SQL语句与存储过程

    这一节,我们来看看EF如何执行SQL语句与读取存储过程的数据,可能有一部分人,还不知道EF如何执行存储过程与原生SQL语句! 我们什么时候要直接使用原生的SQL语句? 返回值过于复杂 过于复杂的联合查 ...

  9. 关于EF执行返回表的存储过程

    1.关于EF执行返回表的存储过程 不知道为什么EF生成的存储过程方法会报错,以下方法可以使用,call是MySQL执行存储过程的命令 [HttpGet] public HttpResponseMess ...

随机推荐

  1. 全新思维导图 XMind ZEN v10.0.0 中文破解版

    http://www.carrotchou.blog/20331.html 官网 https://www.xmind.cn/ 注意事项 破解版本已经去除了全部的官方试用版的限制,让大家可以像正版用户一 ...

  2. Python3基础 from...import 局部导入

             Python : 3.7.3          OS : Ubuntu 18.04.2 LTS         IDE : pycharm-community-2019.1.3    ...

  3. invalid argument (errno: 22)

    socket通信示例中,当accept客户端时,经常报这个错误. 并且是第一次没有问题,第二次或后面几次都会出现如下问题, 错误码为22, 错误描述为invalid argument. 问题解决如下: ...

  4. WMS培训20190907

    SELECT * FROM WMSADMIN.SPROCEDUREMAP WHERE THEPROCNAME ='NSPBEFOREORDERWRITE' 二,. RF中页面中需要增加申请人,而成品仓 ...

  5. Linux 命令行作弊工具安利

    本文转自 微信公众号<Linux爱好者>的一篇文章,觉得工具非常好使,且极具使用价值,所以在此安利一下 Linux 用户的福音,记忆力解放!快速调用复杂命令 刚学的一句新命令,才用完就忘了 ...

  6. MySQL创建触发器的时候报1419错误( 1419 - You do not have the SUPER privilege and binary logging is enabled )

    mysql创建触发器的时候报错: 解决方法:第一步,用root用户登录:mysql -u root -p第二步,设置参数log_bin_trust_function_creators为1:set gl ...

  7. [LeetCode] 334. Increasing Triplet Subsequence 递增三元子序列

    Given an unsorted array return whether an increasing subsequence of length 3 exists or not in the ar ...

  8. MySQL Community Server 8.0.16

    1 首先 我们需要先下载一个 Mysql 点击这个网址进入 Mysql 的官网的下载地址: https://dev.mysql.com/downloads/mysql/ 首先 根据你的电脑的操作系统选 ...

  9. Toping Kagglers:Bestfitting,目前世界排名第一

    Toping Kagglers:Bestfitting,目前世界排名第一 Kaggle团队 |2018年5月7日   我们在排行榜上排名第一 - 这是两年前令人惊讶地加入该平台的竞争对手.Shubin ...

  10. json对象转js对象

    json数据: { "YD1": 0, "YD2": 0, "YD3": 0, "YD4": 0, "YD5& ...