[Table(Name = "EyouSoft_Customer")]
public class Customer
{
[Column(IsPrimaryKey=true,Name="Id")]
public int CustomerId {get;set; } [Column(Name = "CustomerName")]
public string CustomerName { get; set; } [Column(Name = "SystemID")]
public int SystemID { get; set; } public override string ToString()
{
return string.Format("客户编号:{0},客户名称:{1},系统编号:{2}",CustomerId.ToString(),CustomerName,SystemID.ToString());
}
}
这个类针对数据库中的某个表
我想通过反射去获得这个表中相对应列的名称,和列值
怎么样才能获得到
属性名称和表列名不一致!
求解
public class Customer
{
[Column(IsPrimaryKey=true,Name="Id")]
public int CustomerId {get;set; } [Column(Name = "CustomerName")]
public string CustomerName { get; set; } [Column(Name = "SystemID")]
public int SystemID { get; set; } public override string ToString()
{
return string.Format("客户编号:{0},客户名称:{1},系统编号:{2}",CustomerId.ToString(),CustomerName,SystemID.ToString());
}
}
这个类针对数据库中的某个表
我想通过反射去获得这个表中相对应列的名称,和列值
怎么样才能获得到
属性名称和表列名不一致!
求解
[Table(Name = "EyouSoft_Customer")]//这里是表名 一个名为 “TableAttribute”的特性类
public class Customer
{
/这里是属性对应列名 一个名为 “ColumnAttribute”的特性类
[Column(IsPrimaryKey=true,Name="Id")]
public int CustomerId {get;set; } [Column(Name = "CustomerName")]
public string CustomerName { get; set; } [Column(Name = "SystemID")]
public int SystemID { get; set; } public override string ToString()
{
return string.Format("客户编号:{0},客户名称:{1},系统编号:{2}",CustomerId.ToString(),CustomerName,SystemID.ToString());
}
}
获取表名: object[] arr = Model.GetType().GetCustomAttributes(typeof(TableAttribute), false);
获取每个属性的对应列名PropertyInfo[] propertyArr = Model.GetType().GetProperties();
object[] CustomAttributesArr = null;
foreach (PropertyInfo item in propertyArr)
{
CustomAttributesArr = item.GetCustomAttributes(typeof(ColumnAttribute), false);
}
把上面的Model 换成你的实体对象实例这是很典型的ORM映射机制,楼主可以了解一下
http://topic.csdn.net/u/20111108/11/439bb041-7043-4b58-8f5f-975821b8e320.html