📄 ormreader.cs
字号:
using System;
using System.Collections;
using System.Collections.Generic;
using System.Text;
using System.Data;
using System.Data.SqlClient;
using System.Reflection;
using System.Transactions;
namespace MiniORM
{
/// <summary>
/// ORM读取器,从数据库中读取指定的记录
/// </summary>
public class OrmReader
{
/// <summary>
/// 读取记录集并且返回
/// </summary>
/// <param name="id"></param>
/// <returns></returns>
public object Read(object ModelObject, int id)
{
List<SqlParameter> Params = new List<SqlParameter>();
object objKeyValue = new object();
string strKeyName = PubFuncs.GetKey(ModelObject.GetType());
//设置主键值
PubFuncs.SetKeyValue(ModelObject, id);
//读取数据的SQL,并且返回需要的SqlParameter
string strSQL = PubFuncs.GetReadSQL(ModelObject, ref Params);
SqlCommand cmd = new SqlCommand();
foreach (SqlParameter Param in Params)
{
cmd.Parameters.Add(Param);
}
// 建立数据库连接
using (SqlConnection conn = new SqlConnection(PubFuncs.ConnectionStr))
{
conn.Open();
cmd.Connection = conn;
cmd.CommandType = CommandType.Text;
cmd.CommandText = strSQL;
using (SqlDataReader rdr = cmd.ExecuteReader(CommandBehavior.CloseConnection))
{
rdr.Read();
//先把rdr获得的值全部保存到HashTable中,这样在后续对ModelObject属性付值的时候就可以直接通过在HashTable中取值,提高速度
Hashtable HashValues = new Hashtable();
for (int i = 0; i < rdr.FieldCount; i++)
{
if (rdr[i] != DBNull.Value)
{
HashValues[rdr.GetName(i).ToLower().Trim()] = rdr[i];
}
//KeyValue
if (rdr.GetName(i) == strKeyName)
{
objKeyValue = rdr[i];
}
}
//反射设置ModelObject的属性
PropertyInfo[] props = ModelObject.GetType().GetProperties();
MiniORMAttribute.DataFieldAttribute FieldAttr = null;
MiniORMAttribute.SubDataObjectAttribute SubDataAttr = null;
object[] CustomAttributes;
foreach (PropertyInfo prop in props)
{
CustomAttributes = prop.GetCustomAttributes(typeof(MiniORMAttribute.DataFieldAttribute), false);
if (CustomAttributes.Length > 0)
{
FieldAttr = CustomAttributes[0] as MiniORMAttribute.DataFieldAttribute;
if (FieldAttr != null)
{
//对ModelObject对象属性进行付值
if (HashValues.ContainsKey(prop.Name.ToLower()))
{
//比如数据库字段是decimal类型,而类属性是double类型,这里是会报错的,所以这里必须借助Convert.ChangeType来进行转换
//prop.SetValue(ModelObject, HashValues[prop.Name.ToLower()], null);
prop.SetValue(ModelObject, Convert.ChangeType(HashValues[prop.Name.ToLower()], prop.PropertyType), null);
}
}
}
#region 处理子对象
CustomAttributes = prop.GetCustomAttributes(typeof(MiniORMAttribute.SubDataObjectAttribute), false);
if (CustomAttributes.Length > 0)
{
SubDataAttr = CustomAttributes[0] as MiniORMAttribute.SubDataObjectAttribute;
if (SubDataAttr != null)
{
//ReadChild将根据prop的类型来做判断,读取子列表
prop.SetValue(ModelObject, ReadChild(prop, objKeyValue), null);
}
}
#endregion
}
cmd.Parameters.Clear();
}
}
return ModelObject;
}
/// <summary>
/// 读取子对象数据
/// </summary>
/// <param name="SubModelObject"></param>
/// <param name="foreignkey"></param>
/// <returns></returns>
private List<object> ReadChild(Type SubObjectType, object foreignkeyValue)
{
List<object> lstReturn = new List<object>();
object objKeyValue = new object();
string strTablename = PubFuncs.GetTableName(SubObjectType);
string strKeyName = PubFuncs.GetKey(SubObjectType);
string strForeignKey = PubFuncs.GetForeignKey(SubObjectType);
string strSelectSQL = "SELECT * FROM {0} WHERE {1};";
strSelectSQL = string.Format(strSelectSQL, new string[] { strTablename, strForeignKey + "= @" + strForeignKey });
SqlCommand cmd = new SqlCommand();
cmd.Parameters.Add(new SqlParameter("@" + strForeignKey, foreignkeyValue));
// 建立数据库连接
using (SqlConnection conn = new SqlConnection(PubFuncs.ConnectionStr))
{
conn.Open();
cmd.Connection = conn;
cmd.CommandType = CommandType.Text;
cmd.CommandText = strSelectSQL;
using (SqlDataReader rdr = cmd.ExecuteReader(CommandBehavior.CloseConnection))
{
while (rdr.Read())
{
//先把rdr获得的值全部保存到HashTable中
Hashtable HashValues = new Hashtable();
for (int i = 0; i < rdr.FieldCount; i++)
{
HashValues[rdr.GetName(i)] = rdr[i];
//KeyValue
if (rdr.GetName(i) == strKeyName)
{
objKeyValue = rdr[i];
}
}
//创建对象并且附值
object model = Activator.CreateInstance(SubObjectType);
PropertyInfo[] props = SubObjectType.GetProperties();
MiniORMAttribute.DataFieldAttribute FieldAttr = null;
MiniORMAttribute.SubDataObjectAttribute SubDataAttr = null;
object[] CustomAttributes;
foreach (PropertyInfo prop in props)
{
CustomAttributes = prop.GetCustomAttributes(typeof(MiniORMAttribute.DataFieldAttribute), false);
if (CustomAttributes.Length > 0)
{
FieldAttr = CustomAttributes[0] as MiniORMAttribute.DataFieldAttribute;
if (FieldAttr != null)
{
if (HashValues.ContainsKey(prop.Name))
{
prop.SetValue(model, HashValues[prop.Name], null);
}
}
}
#region 处理子对象
CustomAttributes = prop.GetCustomAttributes(typeof(MiniORMAttribute.SubDataObjectAttribute), false);
if (CustomAttributes.Length > 0)
{
SubDataAttr = CustomAttributes[0] as MiniORMAttribute.SubDataObjectAttribute;
if (SubDataAttr != null)
{
//ReadChild将根据prop的类型来做判断,读取子列表
prop.SetValue(model, ReadChild(prop, objKeyValue), null);
}
}
#endregion
}
lstReturn.Add(model);
}
}
}
return lstReturn;
}
/// <summary>
/// 根据prop的类型,读取满足foreignkeyValue的记录
/// </summary>
/// <param name="prop"></param>
/// <param name="foreignkeyValue"></param>
/// <returns></returns>
private object ReadChild(PropertyInfo prop, object foreignkeyValue)
{
MiniORMAttribute.SubDataObjectAttribute SubDataAttr = null;
object[] CustomAttributes;
CustomAttributes = prop.GetCustomAttributes(typeof(MiniORMAttribute.SubDataObjectAttribute), false);
if (CustomAttributes.Length > 0)
{
SubDataAttr = CustomAttributes[0] as MiniORMAttribute.SubDataObjectAttribute;
if (SubDataAttr != null)
{
Type type = PubFuncs.GetObjectType(SubDataAttr.AssemblyName, SubDataAttr.NamespaceName, SubDataAttr.ClassName);
switch (SubDataAttr.FieldType)
{
case MiniORMAttribute.SubDataObjectFieldType.Object:
{
//prop.SetValue(ModelObject, ReadChild(type, foreignkeyValue)[0], null);
return ReadChild(type, foreignkeyValue)[0];
}
break;
//case MiniORMAttribute.SubDataObjectFieldType.MyHashTable:
// {
// List<object> objs = ReadChild(type, foreignkeyValue);
// INS.Model.MyHashTable hashTmp = new INS.Model.MyHashTable();
// foreach (object obj in objs)
// {
// hashTmp[foreignkeyValue] = obj;
// }
// return hashTmp;
// //prop.SetValue(ModelObject, hashTmp, null);
// }
// break;
case MiniORMAttribute.SubDataObjectFieldType.HashTable:
{
List<object> objs = ReadChild(type, foreignkeyValue);
Hashtable hashTmp = new Hashtable();
foreach (object obj in objs)
{
hashTmp[foreignkeyValue] = obj;
}
return hashTmp;
//prop.SetValue(ModelObject, hashTmp, null);
}
break;
case MiniORMAttribute.SubDataObjectFieldType.List:
{
List<object> objs = ReadChild(type, foreignkeyValue);
List<object> lst = new List<object>();
foreach (object obj in objs)
{
lst.Add(obj);
}
return lst;
//prop.SetValue(ModelObject, lst, null);
}
break;
case MiniORMAttribute.SubDataObjectFieldType.ArrayList:
{
List<object> objs = ReadChild(type, foreignkeyValue);
ArrayList lst = new ArrayList();
foreach (object obj in objs)
{
lst.Add(obj);
}
return lst;
//prop.SetValue(ModelObject, lst, null);
}
break;
}
}
}
return null;
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -