使用从MVC4中的存储过程返回的结果

时间:2021-12-11 16:37:28

I have created stored procedure like this:

我已经创建了这样的存储过程:

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

In controller I am trying to get return result as mention below:

在控制器中我试图获得返回结果,如下所述:

[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();
            }
}

But it returns an exception

但它返回一个例外

Cannot convert type 'System.Data.Objects.ObjectResult' to 'int'

无法将类型'System.Data.Objects.ObjectResult'转换为'int'

I am not able to get single return value from stored procedure.

我无法从存储过程中获得单个返回值。

Hope I get the right solution.

希望我得到正确的解决方案。

4 个解决方案

#1


0  

db.TakeOrder().FirstOrDefault().ToString() = One value returned as a string

db.TakeOrder()。FirstOrDefault()。ToString()=以字符串形式返回的一个值

db.TakeOrder().FirstOrDefault().ToList() = Row returned as a list.

db.TakeOrder()。FirstOrDefault()。ToList()=以列表形式返回的行。

#2


0  

  1. Create view as

    创建视图为

    CREATE VIEW [dbo].[vScalar] AS SELECT '' AS Value

    CREATE VIEW [dbo]。[vScalar] AS SELECT''AS Value

  2. In Stored Procedure return

    在存储过程中返回

    SELECT CAST(orderId AS VARCHAR(10)) AS value

    SELECT CAST(orderId AS VARCHAR(10))AS值

  3. In Entity Framework, set function return use entity vScalar

    在Entity Framework中,设置函数return use entity vScalar

  4. In code

    vScalar orderId = db.takeOrder(objOreder.oid, objOreder.userId, objOreder.dining_Id, objOreder.amount).FirstOrDefault;

    vScalar orderId = db.takeOrder(objOreder.oid,objOreder.userId,objOreder.dining_Id,objOreder.amount).FirstOrDefault;

    oid = (int)orderId.value;

    oid =(int)orderId.value;

#3


-1  

object orderId = db.takeOrder(objOreder.oid, objOreder.userId, objOreder.dining_Id, objOreder.amount);                                     
oid = (int)orderId;

Try this conversion method.

试试这种转换方法。

oid = Convert.ToInt32(orderId);

#4


-1  

You can use like

你可以使用喜欢

 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);
    }

Please let me know if it not worked for you.. Thanks

如果它不适合你,请告诉我..谢谢

#1


0  

db.TakeOrder().FirstOrDefault().ToString() = One value returned as a string

db.TakeOrder()。FirstOrDefault()。ToString()=以字符串形式返回的一个值

db.TakeOrder().FirstOrDefault().ToList() = Row returned as a list.

db.TakeOrder()。FirstOrDefault()。ToList()=以列表形式返回的行。

#2


0  

  1. Create view as

    创建视图为

    CREATE VIEW [dbo].[vScalar] AS SELECT '' AS Value

    CREATE VIEW [dbo]。[vScalar] AS SELECT''AS Value

  2. In Stored Procedure return

    在存储过程中返回

    SELECT CAST(orderId AS VARCHAR(10)) AS value

    SELECT CAST(orderId AS VARCHAR(10))AS值

  3. In Entity Framework, set function return use entity vScalar

    在Entity Framework中,设置函数return use entity vScalar

  4. In code

    vScalar orderId = db.takeOrder(objOreder.oid, objOreder.userId, objOreder.dining_Id, objOreder.amount).FirstOrDefault;

    vScalar orderId = db.takeOrder(objOreder.oid,objOreder.userId,objOreder.dining_Id,objOreder.amount).FirstOrDefault;

    oid = (int)orderId.value;

    oid =(int)orderId.value;

#3


-1  

object orderId = db.takeOrder(objOreder.oid, objOreder.userId, objOreder.dining_Id, objOreder.amount);                                     
oid = (int)orderId;

Try this conversion method.

试试这种转换方法。

oid = Convert.ToInt32(orderId);

#4


-1  

You can use like

你可以使用喜欢

 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);
    }

Please let me know if it not worked for you.. Thanks

如果它不适合你,请告诉我..谢谢