1. 程式人生 > >mvc 使用儲存過程

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

[email protected]
end