zoukankan      html  css  js  c++  java
  • 更新部分字段 NHibernate

    概述:

         在有些情况下,我只想更新记录中的一个字段的值.比如:浏览完这条记录后,我把其中的是否浏览置为1.

    Nhibernate中提供了Native SQL,其中有一个方法ExecuteUpdate()来实现这个功能.

    代码如下:   

     public static bool UpdateIsBrowse(decimal id)
            {
                
    bool IsSuccess = false;

                ITransaction trans 
    = session.BeginTransaction();
                
    try
                {
                    
    //2 修改记录
                    string sql = " update jkpt_oaxt_weatherforecast set Isbrowse=1 where Weatherforecastid=" + id;           

                    ISQLQuery Query 
    = session.CreateSQLQuery(sql).AddEntity(typeof(JkptOaxtWeatherforecast));
                    Query.ExecuteUpdate();             
                    session.Flush();
                    trans.Commit();
                    
    //写日志
                    Helpers.SaveInfo("Update weatherforecast 's isbrowse Success!");
                    IsSuccess 
    = true;
                }
                
    catch (Exception ex)
                {
                    IsSuccess 
    = false;
                    trans.Rollback();
                    
    //写日志
                    Helpers.SaveInfo("更新是否浏览失败!错误提示如下:" + ex.Message);
                }
                
    finally
                {
                    
    if (session != null)
                    {
                        session.Clear();
                    }
                }
                
    return IsSuccess;
            }
  • 相关阅读:
    DB2 for Z/os Statement prepare
    Foreign key (referential) constraints on DB2 LUW v105
    复制Informational constraints on LUW DB2 v105
    DB2 SQL Mixed data in character strings
    DB2 create partitioned table
    MVC中使用EF的技巧集(一)
    Asp.Net MVC 开发技巧(二)
    Linq使用技巧及查询示例(一)
    Asp.Net MVC 开发技巧(一)
    Asp.Net MVC Identity 2.2.1 使用技巧(八)
  • 原文地址:https://www.cnblogs.com/abcdwxc/p/1435457.html
Copyright © 2011-2022 走看看