mvc 使用存储过程

先创建上下文对象

 Procmodel pcontext=new Procmodel ();

public ActionResult p()

{

   SqlParameter name = new SqlParameter("@name", "test");//参数化防止sql注入

   SqlParameter msg = new SqlParameter("@missage", SqlDbType.NVarChar, 50);

   msg.Direction = ParameterDirection.Output;//指明为输出参数

   pcontext.Database.ExecuteSqlCommand("exec ws3 @name,@missage output", name, msg);//还有一个Database.SqlQuery()应该也可以

   string result=msg.Value;//获取输出参数

}



这里是存储过程

create proc ptest
@name nvarchar(20),
@missage nvarchar(50) output
as
begin
 select @missage=id from users where name=@name
end


  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
在 ASP.NET MVC 中,可以使用 Entity Framework 来调用和执行存储过程。下面是一种在 ASP.NET MVC 中编写存储过程的方法: 1. 创建数据库上下文类(DbContext): ```csharp public class YourDbContext : DbContext { public YourDbContext() : base("YourConnectionString") { } // 声明用于执行存储过程的方法 public virtual int ExecuteStoredProcedure(string storedProcedureName, SqlParameter[] parameters) { return Database.ExecuteSqlCommand(storedProcedureName, parameters); } } ``` 上述代码中,你需要替换 "YourConnectionString" 为你自己的数据库连接字符串,并添加适当的命名空间引用。 2. 在你的控制器中使用存储过程: ```csharp public class YourController : Controller { private YourDbContext db = new YourDbContext(); public ActionResult YourAction() { // 创建存储过程参数 var parameters = new[] { new SqlParameter("@Parameter1", value1), new SqlParameter("@Parameter2", value2), // 添加其他参数... }; // 执行存储过程 int result = db.ExecuteStoredProcedure("YourStoredProcedureName", parameters); // 处理存储过程执行结果 return View(); } } ``` 在上面的代码中,你需要将 "YourStoredProcedureName" 替换为你要执行存储过程的名称,并根据需要添加参数和处理结果。 请注意,这只是一种在 ASP.NET MVC使用 Entity Framework 调用存储过程的方法之一。你还可以使用其他 ORM 工具或直接使用 ADO.NET 进行存储过程的编写和调用。根据你的实际需求,选择适合的方法来编写和执行存储过程

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值