Hxj.Data经过几个月的开发,终于发布了一个版本,并同时发布实体生成工具。在开发过程参考了多个ORM框架,特别是NBear,MySoft等。吸取了他们的一些精华,加入自己的新思想。
目前支持Sql2000\2005,MsAccess,Oracle,SQLite,MySQL,如有需要可再扩展。
数据库实体生成的代码Northwind数据库中Products表:
//------------------------------------------------------------------------------
// <auto-generated>
// 此代码由工具生成。
// 运行时版本:2.0.50727.4200
//
// 对此文件的更改可能会导致不正确的行为,并且如果
// 重新生成代码,这些更改将会丢失。
// </auto-generated>
//------------------------------------------------------------------------------
using System;
using System.Data;
using System.Data.Common;
using Hxj.Data;
using Hxj.Data.Common;
namespace Hxj.Model
{
/// <summary>
/// 实体类Products 。(属性说明自动提取数据库字段的描述信息)
/// </summary>
[Serializable]
public class Products : Entity
{
public Products():base("Products") {}
#region Model
private int _ProductID;
private string _ProductName;
private int? _SupplierID;
private int? _CategoryID;
private string _QuantityPerUnit;
private decimal? _UnitPrice;
private int? _UnitsInStock;
private int? _UnitsOnOrder;
private int? _ReorderLevel;
private bool _Discontinued;
/// <summary>
///
/// </summary>
public int ProductID
{
get{ return _ProductID; }
set
{
this.OnPropertyValueChange(_.ProductID,_ProductID,value);
this._ProductID=value;
}
}
/// <summary>
///
/// </summary>
public string ProductName
{
get{ return _ProductName; }
set
{
this.OnPropertyValueChange(_.ProductName,_ProductName,value);
this._ProductName=value;
}
}
/// <summary>
///
/// </summary>
public int? SupplierID
{
get{ return _SupplierID; }
set
{
this.OnPropertyValueChange(_.SupplierID,_SupplierID,value);
this._SupplierID=value;
}
}
/// <summary>
///
/// </summary>
public int? CategoryID
{
get{ return _CategoryID; }
set
{
this.OnPropertyValueChange(_.CategoryID,_CategoryID,value);
this._CategoryID=value;
}
}
/// <summary>
///
/// </summary>
public string QuantityPerUnit
{
get{ return _QuantityPerUnit; }
set
{
this.OnPropertyValueChange(_.QuantityPerUnit,_QuantityPerUnit,value);
this._QuantityPerUnit=value;
}
}
/// <summary>
///
/// </summary>
public decimal? UnitPrice
{
get{ return _UnitPrice; }
set
{
this.OnPropertyValueChange(_.UnitPrice,_UnitPrice,value);
this._UnitPrice=value;
}
}
/// <summary>
///
/// </summary>
public int? UnitsInStock
{
get{ return _UnitsInStock; }
set
{
this.OnPropertyValueChange(_.UnitsInStock,_UnitsInStock,value);
this._UnitsInStock=value;
}
}
/// <summary>
///
/// </summary>
public int? UnitsOnOrder
{
get{ return _UnitsOnOrder; }
set
{
this.OnPropertyValueChange(_.UnitsOnOrder,_UnitsOnOrder,value);
this._UnitsOnOrder=value;
}
}
/// <summary>
///
/// </summary>
public int? ReorderLevel
{
get{ return _ReorderLevel; }
set
{
this.OnPropertyValueChange(_.ReorderLevel,_ReorderLevel,value);
this._ReorderLevel=value;
}
}
/// <summary>
///
/// </summary>
public bool Discontinued
{
get{ return _Discontinued; }
set
{
this.OnPropertyValueChange(_.Discontinued,_Discontinued,value);
this._Discontinued=value;
}
}
#endregion
#region Method
/// <summary>
/// 获取实体中的标识列
/// </summary>
public override Field GetIdentityField()
{
return _.ProductID;
}
/// <summary>
/// 获取实体中的主键列
/// </summary>
public override Field[] GetPrimaryKeyFields()
{
return new Field[] {
_.ProductID};
}
/// <summary>
/// 获取列信息
/// </summary>
public override Field[] GetFields()
{
return new Field[] {
_.ProductID,
_.ProductName,
_.SupplierID,
_.CategoryID,
_.QuantityPerUnit,
_.UnitPrice,
_.UnitsInStock,
_.UnitsOnOrder,
_.ReorderLevel,
_.Discontinued};
}
/// <summary>
/// 获取值信息
/// </summary>
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};
}
/// <summary>
/// 给当前实体赋值
/// </summary>
public override void SetPropertyValues(IDataReader reader)
{
this._ProductID = DataUtils.ConvertValue<int>(reader["ProductID"]);
this._ProductName = DataUtils.ConvertValue<string>(reader["ProductName"]);
this._SupplierID = DataUtils.ConvertValue<int?>(reader["SupplierID"]);
this._CategoryID = DataUtils.ConvertValue<int?>(reader["CategoryID"]);
this._QuantityPerUnit = DataUtils.ConvertValue<string>(reader["QuantityPerUnit"]);
this._UnitPrice = DataUtils.ConvertValue<decimal?>(reader["UnitPrice"]);
this._UnitsInStock = DataUtils.ConvertValue<int?>(reader["UnitsInStock"]);
this._UnitsOnOrder = DataUtils.ConvertValue<int?>(reader["UnitsOnOrder"]);
this._ReorderLevel = DataUtils.ConvertValue<int?>(reader["ReorderLevel"]);
this._Discontinued = DataUtils.ConvertValue<bool>(reader["Discontinued"]);
}
/// <summary>
/// 给当前实体赋值
/// </summary>
public override void SetPropertyValues(DataRow row)
{
this._ProductID = DataUtils.ConvertValue<int>(row["ProductID"]);
this._ProductName = DataUtils.ConvertValue<string>(row["ProductName"]);
this._SupplierID = DataUtils.ConvertValue<int?>(row["SupplierID"]);
this._CategoryID = DataUtils.ConvertValue<int?>(row["CategoryID"]);
this._QuantityPerUnit = DataUtils.ConvertValue<string>(row["QuantityPerUnit"]);
this._UnitPrice = DataUtils.ConvertValue<decimal?>(row["UnitPrice"]);
this._UnitsInStock = DataUtils.ConvertValue<int?>(row["UnitsInStock"]);
this._UnitsOnOrder = DataUtils.ConvertValue<int?>(row["UnitsOnOrder"]);
this._ReorderLevel = DataUtils.ConvertValue<int?>(row["ReorderLevel"]);
this._Discontinued = DataUtils.ConvertValue<bool>(row["Discontinued"]);
}
#endregion
#region _Field
/// <summary>
/// 字段信息
/// </summary>
public class _
{
public readonly static Field All = new Field("*","Products");
public readonly static Field ProductID = new Field("ProductID","Products","ProductID");
public readonly static Field ProductName = new Field("ProductName","Products","ProductName");
public readonly static Field SupplierID = new Field("SupplierID","Products","SupplierID");
public readonly static Field CategoryID = new Field("CategoryID","Products","CategoryID");
public readonly static Field QuantityPerUnit = new Field("QuantityPerUnit","Products","QuantityPerUnit");
public readonly static Field UnitPrice = new Field("UnitPrice","Products","UnitPrice");
public readonly static Field UnitsInStock = new Field("UnitsInStock","Products","UnitsInStock");
public readonly static Field UnitsOnOrder = new Field("UnitsOnOrder","Products","UnitsOnOrder");
public readonly static Field ReorderLevel = new Field("ReorderLevel","Products","ReorderLevel");
public readonly static Field Discontinued = new Field("Discontinued","Products","Discontinued");
}
#endregion
}
}
数据组件默认入口为:Hxj.Data.DbSession.Default 会自动读取config文件中connectionStrings节点的最后一个连接配置。
当然可根据不同的数据连接实例化新的DbSession。
查询示例:
1、查询Products表所有数据的信息,返回实体列表。
List<Products> list = DbSession.Default.From<Products>().ToList();
2、查询其他的简单示例。
DbSession.Default.From<Products>()
//.Select(Products._.ProductID)
//.GroupBy(Products._.CategoryID.GroupBy && Products._.ProductName.GroupBy)
// .InnerJoin<Suppliers>(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<Products>().Select(Products._.ProductID))
.Page(10, 1)
//.ToList();
.ToDataSet();
//新建一个实体
Products p = new Products();
//开启修改 (开启修改后的添加操作将只insert赋值过的字段)
p.Attach();
//获取页面中输入的值
EntityUtils.UpdateModel<Products>(p, Request.Form, "txt_");
//返回值 如果有自增长字段,则返回自增长字段的值
int returnValue = DbSession.Default.Insert<Products>(p);
修改示例:
修改Products表第一条数据的ProductName的值。
//获取Products表第一行
Products p = DbSession.Default.From<Products>().ToFirst();
//开启修改 (修改操作之前 必须执行此方法)
p.Attach();
p.ProductName = txtValue.Text.Trim();
//更新
//返回0表示更新失败 组件有事务会自动回滚
//返回1表示更新成功
//更新成功返回值就是受影响的条数
int returnvalue = DbSession.Default.Update<Products>(p);
删除示例:
参数为主键的值,也可传入实体。
//删除 条件 ProductID=2
//返回0表示删除失败 组件有事务会自动回滚
//返回1表示删除成功
//删除成功返回值就是受影响的条数
int returnvalue = DbSession.Default.Delete<Products>(2);
组件还支持事务,批处理等功能。
下载