ExecuteStoreQuery传递参数的SQL语句 [英] ExecuteStoreQuery passing parameter to SQL statement
本文介绍了ExecuteStoreQuery传递参数的SQL语句的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
有人能帮助传递参数对象到我的 ExecuteStoreQuery
。我是从我的实体数据库执行此操作,然后创建我的飞行过程这是可能的,是我的SQL正确的。我需要一个编号
参数值传递到我的SQL语句
VAR节点= db.ExecuteStoreQuery<节点>(@
@id INT //不能确定该声明的参数 与C作为(
选择ID,姓名,的ParentId,[0]的水平
从部门Ð
UNION ALL
选择d.Id,d.Name,d.ParentId,[等级] + 1
从部门Ð
加入C对d.ParentId = c.Id)
从C中选择*其中c.Id = @Id
部门,
System.Data.Objects.MergeOption.NoTracking,
新对象{ID:department.Id} //< - 不工作参数对象);
解决方案
据该的 MSDN文档,您的发言需要阅读更像是:
VAR节点= db.ExecuteStoreQuery<节点>(@
与C作为(
选择ID,姓名,的ParentId,[0]的水平
从部门Ð
UNION ALL
选择d.Id,d.Name,d.ParentId,[等级] + 1
从部门Ð
加入C对d.ParentId = c.Id)
从C中选择*其中c.Id = @Id
部门,
System.Data.Objects.MergeOption.NoTracking,
新的SqlParameter {参数名称=ID,值= department.Id});
Could someone help with passing a parameter object into my ExecuteStoreQuery
. I'm executing this from my Entities db and then creating my procedure on the fly is this possible and is my SQL correct. I need to pass a Id
parameter value into my SQL statement
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);
解决方案
According to the MSDN documentation, your statement needs to read more like:
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});
这篇关于ExecuteStoreQuery传递参数的SQL语句的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文