sql-server 我在返回计数中收到错误消息“无法将类型”int“隐式转换为”Microsoft.AspNetCore.Mvc. IActionResult“”

brgchamk  于 2022-10-31  发布在  其他
关注(0)|答案(2)|浏览(318)
public IActionResult DeluxRoomCount()
{
   string deluxRoom = "select COUNT(type) from Rooms where Type='Delxu' And Avilability='True'";
   int count = 0;
   using (SqlConnection thisConnection = new SqlConnection("Data Source=HootelReservationDb1"))
        {
           using (SqlCommand cmdCount = new SqlCommand(deluxRoom, thisConnection))
                {
                    thisConnection.Open();
                    count = (int)cmdCount.ExecuteScalar();
                }
                return count;
        }
 }

我希望会有一些想法来计算sql数据,我正在特灵了解这个代码有什么问题

9rygscc1

9rygscc11#

您需要强制转换为实现IActionResult驱动程序类。请尝试此操作。

public IActionResult DeluxRoomCount()
{
   string deluxRoom = "select COUNT(type) from Rooms where Type='Delxu' And Avilability='True'";
   int count = 0;
   using (SqlConnection thisConnection = new SqlConnection("Data Source=HootelReservationDb1"))
        {
           using (SqlCommand cmdCount = new SqlCommand(deluxRoom, thisConnection))
                {
                    thisConnection.Open();
                    count = (int)cmdCount.ExecuteScalar();
                }
        }

    return Ok(count)
 }
tct7dpnv

tct7dpnv2#

你的方法的返回类型是IActionResult,但是你返回的是一个int,为了让你的方法正常工作,你需要返回一个IActionResult。下面是一些关于如何做的参考:https://learn.microsoft.com/en-us/aspnet/core/web-api/action-return-types?view=aspnetcore-6.0

相关问题