尝试在 .NET Core 项目中调用/执行存储过程时遇到 Microsoft.Data.SqlClient.SqlException

问题描述 投票:0回答:1

我正在尝试通过 ADO.NET 代码调用存储过程(需要输入参数)。

我得到的异常如下:

Microsoft.Data.SqlClient.SqlException(0x80131904):找不到存储过程“EXEC FindRecipesByTitle”。
在Microsoft.Data.SqlClient.SqlConnection.OnError(SqlException异常,布尔breakConnection,Action

1 wrapCloseInAction)     at Microsoft.Data.SqlClient.SqlInternalConnection.OnError(SqlException exception, Boolean breakConnection, Action
1wrappCloseInAction)

我的存储过程:

CREATE procedure [dbo].[FindRecipesByTitle]
    @title nvarchar(max)
AS
    SET NOCOUNT ON;
BEGIN
    SELECT
        Recipes.Id,
        Recipes.Name AS 'RecipeName',
        Recipes.PostedById,
        CASE
            WHEN Recipes.DishTypeId = 1 THEN 'Appetizers, Beverages'
            WHEN Recipes.DishTypeId = 2 THEN 'Soups, Salads'
            WHEN Recipes.DishTypeId = 3 THEN 'Vegetables'
            ELSE Recipes.DishTypeId 
        END AS DishTypeId,
        CASE
            WHEN Recipes.VegNonVeg = 0 THEN 'Vegetarian'
            WHEN Recipes.VegNonVeg = 1 THEN 'Non Vegetarian'
            ELSE Recipes.VegNonVeg 
        END AS VegNonVeg,
        Recipes.EstimatedTimeInMinutes,
        Recipes.Ingredients,
        Recipes.Description,
        Recipes.ImagePath,
        Recipes.CreatedAt,
        DishTypes.Name AS 'DishName'
    FROM
        [Recipes]
    JOIN
        [DishTypes] ON Recipes.DishTypeId = DishTypes.Id
    WHERE
        Recipes.Name LIKE '%' + @title + '%'
END

我调用存储过程的代码:

public async Task<List<RecipeViewModel>> GetRecipeByTitleFromSP(string title)
{
    List<RecipeViewModel> lstRcpVM = new List<RecipeViewModel>();

    using (var cmd=_dbContext.Database.GetDbConnection().CreateCommand())
    {
        cmd.CommandText = $@"EXEC FindRecipesByTitle";
        cmd.CommandType = CommandType.StoredProcedure;

        cmd.Parameters.Add(new SqlParameter("@title", title));

        _dbContext.Database.OpenConnection();

        using (var reader = cmd.ExecuteReader())
        {
            while(reader.Read())
            {
                lstRcpVM.Add(new RecipeViewModel
                {
                    RecipeName = reader.GetString("RecipeName"),
                    PostedById = reader.GetString("PostedById"),
                    DishTypeId = reader.GetString("DishTypeId"),
                    VegNonVeg = reader.GetString("VegNonVeg"),
                    EstimatedTimeInMinutes = reader.GetInt32("EstimatedTimeInMinutes"),
                    Ingredients = reader.GetString("Ingredients"),
                    Description = reader.GetString("Description"),
                    ImagePath = reader.GetString("ImagePath"),
                    CreatedAt = Convert.ToDateTime(reader["CreatedAt"]),
                    DishName = reader.GetString("DishName")
                });
            }
        }

        _dbContext.Database.CloseConnection();
    }

    return lstRcpVM;
}

其中

_dbContext
是我的 Entity Framework Core
dbContext
RecipeViewModel
是 DTO,为了简洁起见,我没有发布它。

当我尝试运行代码时,我收到了上面发布的异常,这真的很奇怪,因为我可以看到存储过程在那里很好。

我做错了什么?你能告诉我吗?我缺少什么?这是我将参数传递给存储过程的方式吗?还是别的什么?

你能帮我解决这个问题吗?

谢谢大家

stored-procedures entity-framework-core ado.net asp.net-core-webapi sqlexception
1个回答
0
投票

使用 CommandType.StoredProcedure,您不包含“EXEC”。该模式的全部要点在于,它为您构建 RPC 调用,而不是您提供包含“EXEC”和参数标记的 TSQL 批处理。

所以只需将 CommandText 设置为存储过程的名称即可。

cmd.CommandText = $@"FindRecipesByTitle";
cmd.CommandType = CommandType.StoredProcedure;
© www.soinside.com 2019 - 2024. All rights reserved.