我已经创建了这样的存储过程:
ALTER procedure [dbo].[takeOrder]
(
@id int = 0,
@userid int = 0,
@diningid int,
@amount float
)
as
begin
declare @countt as int
select @countt = COUNT(*)
from Order_master
where dining_Id = @diningid and isActive = 1
if @countt <> 0
begin
update Order_master
set amount = @amount
where dining_Id = @diningid and isActive = 1;
end
else
begin
insert into Order_master(userid, dining_Id, amount, [date], isActive)
values (@userid, @diningid, @amount, GETDATE(), 1)
end
--select amount from Order_master where dining_Id=@diningid and isActive=1
select oid
from Order_master
where dining_Id = @diningid and isActive = 1
end
在控制器中,我试图获得返回结果,如下所述:
[HttpPost]
public ActionResult takeOrder(Order_master order,List<Menu_info> menu)
{
String msg = "";
int oid;
if (ModelState.IsValid)
{
try
{
Order_master objOreder = new Order_master
{
amount = order.amount,
isActive = 1,
date = order.date,
dining_Id = order.dining_Id,
userId = order.userId
};
object orderId = db.takeOrder(objOreder.oid, objOreder.userId, objOreder.dining_Id, objOreder.amount);
oid = (int)orderId;
msg = "success..!!";
}
catch (Exception ex)
{
msg = "error...";
}
}
else
{
msg = "please provide info";
}
if (Request.IsAjaxRequest())
{
return new JsonResult { Data = msg, JsonRequestBehavior = JsonRequestBehavior.AllowGet };
}
else
{
return View();
}
}
但它返回异常
无法转换类型&#39; System.Data.Objects.ObjectResult&#39;到&#39; int&#39;
我无法从存储过程中获得单个返回值。
希望我得到正确的解决方案。
答案 0 :(得分:0)
db.TakeOrder()。FirstOrDefault()。ToString() =一个以字符串形式返回的值
db.TakeOrder()。FirstOrDefault()。ToList() =以列表形式返回的行。
答案 1 :(得分:0)
创建视图
创建视图[dbo]。[vScalar] 如 选择''AS值
在存储过程中返回
SELECT CAST(orderId AS VARCHAR(10))AS值
在Entity Framework中,设置函数return use entity vScalar
代码
vScalar orderId = db.takeOrder(objOreder.oid,objOreder.userId,objOreder.dining_Id,objOreder.amount).FirstOrDefault;
oid =(int)orderId.value;
答案 2 :(得分:-1)
object orderId = db.takeOrder(objOreder.oid, objOreder.userId, objOreder.dining_Id, objOreder.amount);
oid = (int)orderId;
尝试这种转换方法。
oid = Convert.ToInt32(orderId);
答案 3 :(得分:-1)
您可以使用
public int InsertWebProperty(int orgid, int? userid, string address, string property_Id, string latitude, string longitude, int? compamyid)
{
return Convert.ToInt32((db.InsertWebProperty(orgid, userid, address, property_Id, latitude, longitude, 0)).FirstOrDefault().recidentity.Value);
}
如果不适合你,请告诉我.. 感谢