Hxj.Data 数据库组件文档 联系客服

发布时间 : 星期一 文章Hxj.Data 数据库组件文档更新完毕开始阅读1c2abf54a8114431b90dd871

{

return new Field[] { _.ProductID}; }

///

/// 获取列信息 ///

public override Field[] GetFields() {

return new Field[] { _.ProductID, _.ProductName, _.SupplierID, _.CategoryID, _.QuantityPerUnit, _.UnitPrice, _.UnitsInStock, _.UnitsOnOrder, _.ReorderLevel, _.Discontinued}; }

///

/// 获取值信息 ///

public override object[] GetValues() {

return new object[] { this._ProductID, this._ProductName, this._SupplierID, this._CategoryID, this._QuantityPerUnit, this._UnitPrice, this._UnitsInStock, this._UnitsOnOrder, this._ReorderLevel, this._Discontinued}; }

///

/// 给当前实体赋值 ///

public override void SetPropertyValues(IDataReader reader) {

this._ProductID = DataUtils.ConvertValue(reader[\]);

9

this._ProductName = DataUtils.ConvertValue(reader[\]); this._SupplierID = DataUtils.ConvertValue(reader[\]); this._CategoryID = DataUtils.ConvertValue(reader[\]); this._QuantityPerUnit =

DataUtils.ConvertValue(reader[\]);

this._UnitPrice = DataUtils.ConvertValue(reader[\]); this._UnitsInStock = DataUtils.ConvertValue(reader[\]); this._UnitsOnOrder = DataUtils.ConvertValue(reader[\]); this._ReorderLevel = DataUtils.ConvertValue(reader[\]); this._Discontinued = DataUtils.ConvertValue(reader[\]); }

///

/// 给当前实体赋值 ///

public override void SetPropertyValues(DataRow row) {

this._ProductID = DataUtils.ConvertValue(row[\]);

this._ProductName = DataUtils.ConvertValue(row[\]); this._SupplierID = DataUtils.ConvertValue(row[\]); this._CategoryID = DataUtils.ConvertValue(row[\]); this._QuantityPerUnit =

DataUtils.ConvertValue(row[\]);

this._UnitPrice = DataUtils.ConvertValue(row[\]); this._UnitsInStock = DataUtils.ConvertValue(row[\]); this._UnitsOnOrder = DataUtils.ConvertValue(row[\]); this._ReorderLevel = DataUtils.ConvertValue(row[\]); this._Discontinued = DataUtils.ConvertValue(row[\]); }

#endregion

#region _Field ///

/// 字段信息 /// public class _ {

public readonly static Field All = new Field(\,\); public readonly static Field ProductID = new Field(\,\,\);

public readonly static Field ProductName = new Field(\,\,\);

public readonly static Field SupplierID = new Field(\,\,\);

public readonly static Field CategoryID = new

10

Field(\,\,\);

public readonly static Field QuantityPerUnit = new Field(\,\,\); public readonly static Field UnitPrice = new Field(\,\,\);

public readonly static Field UnitsInStock = new Field(\,\,\);

public readonly static Field UnitsOnOrder = new Field(\,\,\);

public readonly static Field ReorderLevel = new Field(\,\,\);

public readonly static Field Discontinued = new Field(\,\,\); }

#endregion } }

数据组件默认入口为:Hxj.Data.DbSession.Default 会自动读取config文件中connectionStrings节点的最后一个连接配置。 当然可根据不同的数据连接实例化新的DbSession。 查询示例:

1、查询Products表所有数据的信息,返回实体列表。

List list = DbSession.Default.From().ToList();

2、查询其他的简单示例。

DbSession.Default.From()

//.Select(Products._.ProductID)

//.GroupBy(Products._.CategoryID.GroupBy && Products._.ProductName.GroupBy) // .InnerJoin(Suppliers._.SupplierID == Products._.SupplierID) // .Select(Products._.CategoryID, Products._.ProductName, Products._.ProductID)//, Suppliers._.CompanyName, Suppliers._.ContactName //.OrderBy(Products._.ProductID.Asc)

//.Where((Products._.ProductName.Contain(null) && Products._.UnitPrice > 1) || Products._.CategoryID == 2)

//.UnionAll(DbSession.Default.From().Select(Products._.ProductID)) .Page(10, 1)

11

//.ToList(); .ToDataSet();

添加示例:

例子是web下的。

//新建一个实体

Products p = new Products();

//开启修改 (开启修改后的添加操作将只insert赋值过的字段) p.Attach();

//获取页面中输入的值

EntityUtils.UpdateModel(p, Request.Form, \);

//返回值 如果有自增长字段,则返回自增长字段的值

int returnValue = DbSession.Default.Insert(p);

修改示例:

修改Products表第一条数据的ProductName的值。

//获取Products表第一行

Products p = DbSession.Default.From().ToFirst();

//开启修改 (修改操作之前 必须执行此方法) p.Attach();

p.ProductName = txtValue.Text.Trim(); //更新

//返回0表示更新失败 组件有事务会自动回滚 //返回1表示更新成功

//更新成功返回值就是受影响的条数

int returnvalue = DbSession.Default.Update(p);

删除示例:

参数为主键的值,也可传入实体。

//删除 条件 ProductID=2

//返回0表示删除失败 组件有事务会自动回滚 //返回1表示删除成功

//删除成功返回值就是受影响的条数

int returnvalue = DbSession.Default.Delete(2);

组件还支持事务,批处理等功能。

12