简体   繁体   English

执行内联sql以使用nHibernate更新表

[英]Execute inline sql to update a table using nHibernate

Sorry if this has been asked before, I did a search but couldn't find anything. 对不起,如果之前有人询问,我做了搜索但找不到任何东西。

Is it possible to execute inline sql in nHibernate? 是否可以在nHibernate中执行内联sql? I have something like this which I would like to run against the dB: 我有类似的东西,我想对dB运行:

_session.CreateSQLQuery(
              @"update things
                set defaultThing = 0 where parentId = :parentId AND thingId <> :thingId")
                .SetInt32("parentId ", parent.Id)
                .SetInt32("thingId", thing.Id)
                ;

I suppose I could loop through a bunch of 'things' and set the defaultThing setting to false then call _session.Update(thing) , but if I can do it how I outlined above, that would be great. 我想我可以遍历一堆“事物”并将defaultThing设置为false然后调用_session.Update(thing) ,但如果我能按照上面的描述进行操作,那就太棒了。

Yes, just use ExecuteUpdate() on that query. 是的,只需对该查询使用ExecuteUpdate() It's the equivalent of IDbCommand.ExecuteNonQuery() . 它相当于IDbCommand.ExecuteNonQuery()

As kay mentioned, you can use HQL too. 正如kay所提到的,你也可以使用HQL。 Check 12.3. 检查12.3。 DML-style operations DML风格的操作

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM