首页 / 网页编程 / ASP.NET / 数据库组件 Hxj.Data (二十八)
数据库组件 Hxj.Data (二十八)2011-07-26 博客园 steven hu数据库组件 Hxj.Data (二十八)(事务中的查询,查询字段字中的子查询,WhereClip的隐式转换)本节讲述新版本中新加的功能,以及如何使用。事务中的查询应用场景,有时我们需要事务更新、添加、删除表的操作,在提交事务又需要对这些进行查询(不想 降低事务级别让其他连接查询事务中的数据变动),在事务内部进行的查询。其实只是在DbTrans中增加了FromSection方法。示例代码如下:using (DbTrans trans = DbSession.Default.BeginTransaction())
{
//修改了第一条记录
trans.Update<Products>(Products._.ProductName, "productname", Products._.ProductID == 1);
//事务内查询
Products product = trans.From<Products>().ToFirst();
trans.Commit();
}使用的时候和普通查询时一模一样的。查询字段中的子查询应用场景,在查询中需要在结果中有一列是通过子查询获取的。例如sql:select customerid,customername,(select count(*) from order where order.customerid=customer.customerid) as ordercount from customer在FromSection查询中增加了AddSelect方法,AddSelect(FromSection fromSection)AddSelect(FromSection fromSection, string aliasName)第二个参数为该子查询后的别名,不然则没有别名。示例代码如下:DbSession.Default.From<Customers>()
.Select(Customers._.CustomerID, Customers._.ContactName)
.AddSelect(DbSession.Default.From<Orders>().Select(Field.All.Count()).Where (Customers._.CustomerID == Orders._.CustomerID), "ordercount")
.Page(10, 6)
.ToDataTable();生成的sql语句:Text: SELECT TOP 10 * FROM
( SELECT TOP 41 [Customers].[CustomerID],[Customers].[ContactName],
( SELECT count(*) AS [cnt] FROM [Orders] WHERE ([Customers].[CustomerID] = [Orders]. [CustomerID]) ) AS [ordercount]
FROM [Customers] ORDER BY [Customers].[CustomerID] DESC ) AS tempIntable
ORDER BY [CustomerID] ASC