我有一个基本的存储过程SearchMovies
alter PROCEDURE SearchMovies
@CategoryIds varchar(50) = null
AS ...
我从数据层做了一些调用:
var categoryIds = new SqlParameter("CategoryIds", "6");
var aaa = entities.FromSql("EXECUTE dbo.SearchMovies @CategoryIds", categoryIds ); => empty values
var bbb= entities.FromSql("EXECUTE dbo.SearchMovies @CategoryIds={0}", categoryIds ); => error: Must declare the scalar variable "@p0".
var ccc= entities.FromSql("EXECUTE dbo.SearchMovies @CategoryIds={0}", "6"); => works
那么前两个电话如何不起作用,我跟着https://docs.microsoft.com/en-us/ef/core/querying/raw-sql
谢谢大家。
更新:
看起来我搞砸了。
var categoryIds = new SqlParameter("@CategoryIds", "6");
entities.FromSql("EXECUTE dbo.SearchMovies @CategoryIds", categoryIds )
工作正常。
谢谢大家的帮助。
答案 0 :(得分:1)
参数的名称为@CategoryIds
,而不是CategoryIds
,因此请将第一行更改为:
var categoryIds = new SqlParameter("@CategoryIds", 6);
答案 1 :(得分:-1)
您是否尝试将值转换为字符串,因为您的过程需要VARCHAR?
var categoryIds = new SqlParameter("CategoryIds", Convert.ToString(6));