ExecuteStoreQuery passing parameter to SQL statement

别说谁变了你拦得住时间么 提交于 2019-12-23 01:36:15

问题


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);               

回答1:


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


来源:https://stackoverflow.com/questions/8191212/executestorequery-passing-parameter-to-sql-statement

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!