NHibernate SQL 语句

本文介绍了使用NHibernate进行常见的数据库操作,包括增删改查等基本操作,并提供了具体的代码示例。通过这些示例,读者可以更好地理解如何在.NET环境中使用NHibernate框架。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

删除语句

string hql = @"delete from Inventory where OrgId = :OrgId and Id = :Id";

this.Session.CreateQuery(hql)
	.SetInt32("OrgId", orgId)
	.SetInt32("Id", id)
	.ExecuteUpdate();

string hql = @"delete from Goods where Id in (:ids)";

return this.Session.CreateQuery(hql)
	.SetParameterList("ids", ids)
	.ExecuteUpdate();

查询语句

string hql = @"from Inventory where OrgId = :OrgId and Status = :Status";

Inventory model = this.Session.CreateQuery(hql)
	.SetInt32("OrgId", orgId)
	.SetInt32("Status", 1)
	.UniqueResult<Inventory>();

string hql = @"from Account";

IList<Account> list = this.Session.CreateQuery(hql).List<Account>();

return this.Session.QueryOver<Goods>()
	.Where(x => x.ErpGoodsCode == code)
	.RowCount() > 0;

return this.Session.QueryOver<Goods>()
	.Where(x => x.ErpGoodsCode == erpCode)
	.AndNot(x => x.GoodsStatus == 9)
	.Take(1)
	.SingleOrDefault();

Goods goods = this.Session.QueryOver<Goods>()
	.Where(x => x.Id == GoodsId)
	.Take(1)
	.SingleOrDefault();

return this.Session.QueryOver<GoodsImg>()
	.Where(x => x.GoodsId == goodsId)
	.List();

IList<ICriterion> list = new List<ICriterion>();

list.Add(Restrictions.Eq("Name", name));

return this.GetByCriterion(list);

修改语句

string hql = @"update Inventory set Status = :Status where OrgId = :OrgId and Id = :Id";

this.Session.CreateQuery(hql)
	.SetInt32("Status", -1)
	.SetInt32("OrgId", orgId)
	.SetInt32("Id", id)
	.ExecuteUpdate();

另外还有 NHibernate 自带的Add, Save, SaveOrUpdate 语句。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值