使用 TVP 参数执行StoreQuery
我的数据库中有一个存储过程,它采用表值参数,即包含单个整数 Id 列的 IdTable 对象列表。
我有一个数据库实体模型,想要执行以下操作...
ProjectEntities projectEntities = new ProjectEntities ();
DataTable stationIds = new DataTable();
stationIds.Columns.Add("Id");
stationIds.Rows.Add(1);
stationIds.Rows.Add(2);
SqlParameter parameter = new SqlParameter("@stationIds",stationIds);
parameter.TypeName = "IdTable";
var parameters = new object[] {parameter};
var results = projectEntities .ExecuteStoreQuery<ProjectSummary>("exec ProjectSummary", parameters);
var count = results.Count();
它运行并且不返回任何结果,而它应该返回一堆 ProjectSummary 实体。
当我在 SQL Profiler 中对此进行分析时,我得到以下信息
declare @p3 IdTable
insert into @p3 values(N'1')
insert into @p3 values(N'2')
exec sp_executesql N'exec ProjectSummary',N'@stationIds [IdTable] READONLY',@stationIds=@p3
如果我将存储过程声明为
ALTER PROCEDURE [dbo].[ProjectSummary]
@stationIds [dbo].[IdTable] READONLY
AS
BEGIN
-- SET NOCOUNT ON added to prevent extra result sets from
-- interfering with SELECT statements.
SET NOCOUNT ON;
SELECT * FROM @stationIds
...
然后我没有返回结果,看起来 TVP 参数为空。
就好像我手动执行一样,
declare @p3 IdTable
insert into @p3 values(N'1')
insert into @p3 values(N'2')
EXEC [ProjectSummary]
@stationIds = @p3
GO
我得到了从 SELECT 查询返回的值 1 和 2。
因此,看起来我想在运行 ExecuteStoreCommand 时使用 EXEC 而不是 SP_EXECUTESQL。鉴于上面的代码示例,我到底该怎么做呢?
I have a stored procedure in my database that takes a table value parameter, a list of IdTable objects which contain a single integer Id column.
I have an entity model for the database and want to do the following...
ProjectEntities projectEntities = new ProjectEntities ();
DataTable stationIds = new DataTable();
stationIds.Columns.Add("Id");
stationIds.Rows.Add(1);
stationIds.Rows.Add(2);
SqlParameter parameter = new SqlParameter("@stationIds",stationIds);
parameter.TypeName = "IdTable";
var parameters = new object[] {parameter};
var results = projectEntities .ExecuteStoreQuery<ProjectSummary>("exec ProjectSummary", parameters);
var count = results.Count();
This runs and returns no results, when it should return a bunch of ProjectSummary entities.
When I profile this in SQL Profiler, I get the following
declare @p3 IdTable
insert into @p3 values(N'1')
insert into @p3 values(N'2')
exec sp_executesql N'exec ProjectSummary',N'@stationIds [IdTable] READONLY',@stationIds=@p3
If I declare the stored procedure to be
ALTER PROCEDURE [dbo].[ProjectSummary]
@stationIds [dbo].[IdTable] READONLY
AS
BEGIN
-- SET NOCOUNT ON added to prevent extra result sets from
-- interfering with SELECT statements.
SET NOCOUNT ON;
SELECT * FROM @stationIds
...
Then I get not results back, it looks like the TVP parameter is coming through empty.
Where as if I manually execute
declare @p3 IdTable
insert into @p3 values(N'1')
insert into @p3 values(N'2')
EXEC [ProjectSummary]
@stationIds = @p3
GO
I get the values 1 and 2 returned from the SELECT query.
So, it looks like I want to use EXEC rather than SP_EXECUTESQL when I run ExecuteStoreCommand. Given the code example above, how on earth do I do that?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
事实证明 ExecuteStoreQuery 调用不正确,应该是这样
,所以我需要命名参数并将 @p0 添加到 exec 命令中。
Turns out the ExecuteStoreQuery call was incorrect, it should be
So I needed to name parameter and add the @p0 to the exec command.