C# 使用 Fluent NHibernate 执行 Sql 语句

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/852720/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-05 04:13:19  来源:igfitidea点击:

Executing Sql statements with Fluent NHibernate

c#.netvb.netnhibernate

提问by Micah

Basically I want to be able to do this:

基本上我希望能够做到这一点:

session.ExecuteSql("...");

session.ExecuteSql("...");

I don't need it to map to any entities or return any values. Any suggestions?

我不需要它映射到任何实体或返回任何值。有什么建议?

采纳答案by Steven Lyons

As already mentioned, this is not a Fluent NHibernate issue but here is an example:

如前所述,这不是 Fluent NHibernate 问题,但这里有一个示例:

public int GetSqlCount<T>(Session session, string table)
{
    var sql = String.Format("SELECT Count(*) FROM {0}", table);
    var query = session.CreateSQLQuery(sql);
    var result = query.UniqueResult();
    // Could also use this if only updating values:
    //query.ExecuteUpdate();

    return Convert.ToInt32(result);
}

You will want to investigate the ISQLQuery interface, depending on your needs.

您将需要研究 ISQLQuery 接口,具体取决于您的需要。