gpt4 book ai didi

c# - ExecuteStoreQuery 将参数传递给 SQL 语句

转载 作者:太空宇宙 更新时间:2023-11-03 22:06:43 25 4
gpt4 key购买 nike

有人可以帮助将参数对象传递到我的 ExecuteStoreQuery 中吗?我正在从我的 Entities 数据库执行此操作,然后动态创建我的过程是可能的并且我的 SQL 是正确的。我需要将 Id 参数值传递到我的 SQL 语句中

var node = db.ExecuteStoreQuery<Node>(@"
@Id int // not sure about this declaration for parameter

with c as (
select Id, Name, ParentId,[0] as level
from Department d
union all
select d.Id, d.Name, d.ParentId, [level] + 1
from Department d
join c on d.ParentId = c.Id)
select * from c where c.Id = @Id"
"Departments",
System.Data.Objects.MergeOption.NoTracking,
new object{ Id: department.Id} // <--parameter object not working);

最佳答案

根据MSDN documentation ,您的声明需要更像:

var node = db.ExecuteStoreQuery<Node>(@"
with c as (
select Id, Name, ParentId,[0] as level
from Department d
union all
select d.Id, d.Name, d.ParentId, [level] + 1
from Department d
join c on d.ParentId = c.Id)
select * from c where c.Id = @Id"
"Departments",
System.Data.Objects.MergeOption.NoTracking,
new SqlParameter{ ParameterName = "id", Value = department.Id});

关于c# - ExecuteStoreQuery 将参数传递给 SQL 语句,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8191212/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com