标题:MVC中通过.edmx调用存储过程处理返回值
只看楼主
Sephirose
Rank: 1
来 自:四川-乐山
等 级:新手上路
帖 子:51
专家分:0
注 册:2013-3-26
结帖率:55.56%
 问题点数:0 回复次数:0 
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 编辑 ]
搜索更多相关主题的帖子: Values public Movies 数据库 
2013-10-31 09:21



参与讨论请移步原网站贴子:https://bbs.bccn.net/thread-422819-1-1.html




关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.058713 second(s), 7 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved