mvc 使用存储过程

来源:互联网 发布:淘宝云客服兼职 编辑:程序博客网 时间:2024/05/01 20:45

先创建上下文对象

 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


原创粉丝点击