使用 TVP 参数执行StoreQuery

发布于 2024-11-06 13:40:07 字数 1488 浏览 1 评论 0原文

我的数据库中有一个存储过程,它采用表值参数,即包含单个整数 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

戏舞 2024-11-13 13:40:07

事实证明 ExecuteStoreQuery 调用不正确,应该是这样

SqlParameter stations = new SqlParameter { ParameterName = "p0", Value = ids, TypeName = "[dbo].[IdTable]", SqlDbType = SqlDbType.Structured };

var parameters = new object[] { stations };

var results = projectEntities.ExecuteStoreQuery<ProjectSummary>("exec ProjectSummary @p0", parameters);

,所以我需要命名参数并将 @p0 添加到 exec 命令中。

Turns out the ExecuteStoreQuery call was incorrect, it should be

SqlParameter stations = new SqlParameter { ParameterName = "p0", Value = ids, TypeName = "[dbo].[IdTable]", SqlDbType = SqlDbType.Structured };

var parameters = new object[] { stations };

var results = projectEntities.ExecuteStoreQuery<ProjectSummary>("exec ProjectSummary @p0", parameters);

So I needed to name parameter and add the @p0 to the exec command.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文