MVC中通过.edmx调用存储过程处理返回值
MVC4中用实体模型添加了数据库中的存储过程,存储过程带返回值,在.edmx中被定义为ObjectParameter类型,那我在程序中该如何处理这个类型啊,代码:
存储过程
ALTER proc [dbo].[Movies_Create]
@Title nvarchar(100),
@ReleaseDate DateTime,
@GenreName nvarchar(50),
@Price decimal(18,2),
@Rating nvarchar(50),
@Result int output
as
set @Result=0
begin
insert Movies(Title,ReleaseDate,GenreName,Price,Rating) Values(@Title,@ReleaseDate,@GenreName,@Price,@Rating)
set @Result=1
end
在.edmx中生成的是
public virtual int Movies_Create(string title, Nullable<System.DateTime> releaseDate, string genreName, Nullable<decimal> price, string rating, ObjectParameter result)
{
var titleParameter = title != null ?
new ObjectParameter("Title", title) :
new ObjectParameter("Title", typeof(string));
var releaseDateParameter = releaseDate.HasValue ?
new ObjectParameter("ReleaseDate", releaseDate) :
new ObjectParameter("ReleaseDate", typeof(System.DateTime));
var genreNameParameter = genreName != null ?
new ObjectParameter("GenreName", genreName) :
new ObjectParameter("GenreName", typeof(string));
var priceParameter = price.HasValue ?
new ObjectParameter("Price", price) :
new ObjectParameter("Price", typeof(decimal));
var ratingParameter = rating != null ?
new ObjectParameter("Rating", rating) :
new ObjectParameter("Rating", typeof(string));
return ((IObjectContextAdapter)this).ObjectContext.ExecuteFunction("Movies_Create", titleParameter, releaseDateParameter, genreNameParameter, priceParameter, ratingParameter, result);
}
在程序中调用是:
public ActionResult Create(string Title, string ReleaseDate, string Genres_List, decimal Price, string Rating)
{
int Result = 0;
movies.Movies_Create(Title, Convert.ToDateTime("ReleaseDate"), Genres_List, Price, Rating, Result);//就是这里的Result报错,说的是类型无法转换
if (Result == 1)
{
MessageBox.Show("添加成功!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Asterisk, MessageBoxDefaultButton.Button1, MessageBoxOptions.DefaultDesktopOnly);
return RedirectToAction("Index");
}
else
{
MessageBox.Show("添加失败!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Asterisk, MessageBoxDefaultButton.Button1, MessageBoxOptions.DefaultDesktopOnly);
return View();
}
}
已经解决了
int r=0;
System.Data.Objects.ObjectParameter result = new System.Data.Objects.ObjectParameter("Result",System.Data.DbType.Int32);
movies.Movies_Create(Title, Convert.ToDateTime("ReleaseDate"), Genres_List, Price, Rating, result);
r=(int)result.Value;//r就是所需要的返回值
[ 本帖最后由 Sephirose 于 2013-10-31 09:51 编辑 ]