ExecuteStoreQuery将参数传递给SQL语句

时间:2011-11-19 01:43:31

标签: c# asp.net

有人可以帮助将参数对象传递到我的ExecuteStoreQuery。我正在从我的实体数据库执行此操作,然后动态创建我的程序这是可能的,并且我的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);               

1 个答案:

答案 0 :(得分:4)

根据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});