前面简单的修改功能已经实现了,下面是实现删除功能,代码1-1如下:
#region 删除实体对应数据库中的数据
public int Remove<T>(T entity)
{
TableInfo tableInfo = DbEntityUtils.GetTableInfo(entity, DbOperateType.DELETE);
string strSql = DbEntityUtils.GetDeleteByIdSql(tableInfo);
IDbDataParameter[] parms = DbFactory.CreateDbParameters(1);
parms[0].ParameterName = tableInfo.Id.Key;
parms[0].Value = tableInfo.Id.Value;
object val = AdoHelper.ExecuteNonQuery(transaction, CommandType.Text, strSql, parms);
return Convert.ToInt32(val);
}
#endregion
#region 根据主键id删除实体对应数据库中的数据
public int Remove<T>(object id) where T : new()
{
TableInfo tableInfo = DbEntityUtils.GetTableInfo(new T(), DbOperateType.DELETE);
string strSql = DbEntityUtils.GetDeleteByIdSql(tableInfo);
IDbDataParameter[] parms = DbFactory.CreateDbParameters(1);
parms[0].ParameterName = tableInfo.Id.Key;
parms[0].Value = id;
object val = AdoHelper.ExecuteNonQuery(transaction, CommandType.Text, strSql, parms);
return Convert.ToInt32(val);
}
#endregion
DbEntityUtils.GetDeleteByIdSql方法代码如下1-2:
public static string GetDeleteByIdSql(TableInfo tableInfo)
{
string strSql = "delete {0} where {1} =" + AdoHelper.DbParmChar + tableInfo.Id.Key;
strSql = string.Format(strSql, tableInfo.TableName, tableInfo.Id.Key);
return strSql;
}
到这里为止简单的增、删、改、查功能都已完成,还有许多复杂的操作需要完成,但需要大量的时间和精力,这里只是分享一种思路或是作技术交流,所以不再继续深入的研究下去了,如有兴趣的也可以继续研究和分享经验。
下面是几个类的代码,EntityManagerImpl类:
EntityManagerImplusing System;
using System.Collections.Generic;
using System.Text;
using System.Reflection;
using System.Orm.CustomAttributes;
using System.Data.SqlClient;
using System.Collections;
using System.Data;
using System.Orm.DBUtility;
using System.Orm.Common;
namespace System.Orm.EntityManager
{
public class EntityManagerImpl : EntityManager
{
IDbTransaction transaction = null;
#region 将实体数据保存到数据库
public int Save<T>(T entity)
{
TableInfo tableInfo = DbEntityUtils.GetTableInfo(entity, DbOperateType.INSERT);
String strSql = DbEntityUtils.GetInsertSql(tableInfo);
IDbDataParameter[] parms = DbFactory.CreateDbParameters(tableInfo.Columns.Count);
DbEntityUtils.SetParameters(tableInfo.Columns, parms);
object val = AdoHelper.ExecuteNonQuery(transaction, CommandType.Text, strSql, parms);
return Convert.ToInt32(val);
}
#endregion
#region 将实体数据修改到数据库
public int Update<T>(T entity)
{
TableInfo tableInfo = DbEntityUtils.GetTableInfo(entity, DbOperateType.UPDATE);
String strSql = DbEntityUtils.GetUpdateSql(tableInfo);
IDbDataParameter[] parms = DbFactory.CreateDbParameters(tableInfo.Columns.Count);
DbEntityUtils.SetParameters(tableInfo.Columns, parms);
object val = AdoHelper.ExecuteNonQuery(transaction, CommandType.Text, strSql, parms);
return Convert.ToInt32(val);
}
#endregion
#region 删除实体对应数据库中的数据
public int Remove<T>(T entity)
{
TableInfo tableInfo = DbEntityUtils.GetTableInfo(entity, DbOperateType.DELETE);
String strSql = DbEntityUtils.GetDeleteByIdSql(tableInfo);
IDbDataParameter[] parms = DbFactory.CreateDbParameters(1);
parms[0].ParameterName = tableInfo.Id.Key;
parms[0].Value = tableInfo.Id.Value;
object val = AdoHelper.ExecuteNonQuery(transaction, CommandType.Text, strSql, parms);
return Convert.ToInt32(val);
}
#endregion
#region 根据主键id删除实体对应数据库中的数据
public int Remove<T>(object id) where T : new()
{
TableInfo tableInfo = DbEntityUtils.GetTableInfo(new T(), DbOperateType.DELETE);
String strSql = DbEntityUtils.GetDeleteByIdSql(tableInfo);
IDbDataParameter[] parms = DbFactory.CreateDbParameters(1);
parms[0].ParameterName = tableInfo.Id.Key;
parms[0].Value = id;
object val = AdoHelper.ExecuteNonQuery(transaction, CommandType.Text, strSql, parms);
return Convert.ToInt32(val);
}
#endregion
#region 查询实体对应表的所有数据
public List<T> FindAll<T>() where T : new()
{
List<T> listArr = new List<T>();
PropertyInfo[] properties = ReflectionUtils.GetProperties(new T().GetType());
TableInfo tableInfo = DbEntityUtils.GetTableInfo(new T(), DbOperateType.SELECT);
String strSql = DbEntityUtils.GetFindAllSql(tableInfo);
IDataReader sdr = null;
try
{
sdr = AdoHelper.ExecuteReader(AdoHelper.ConnectionString, CommandType.Text, strSql);
while (sdr.Read())
{
T entity = new T();
foreach (PropertyInfo property in properties)
{
String name = tableInfo.PropToColumn[property.Name].ToString();
ReflectionUtils.SetPropertyValue(entity, property, sdr[name]);
}
listArr.Add(entity);
}
}
catch (Exception ex)
{
throw ex;
}
finally
{
if (sdr != null) sdr.Close();
}
return listArr;
}
#endregion
#region 通过自定义SQL语句查询数据
public List<T> FindBySql<T>(string strSql) where T : new()
{
List<T> listArr = new List<T>();
Type classType = new T().GetType();
PropertyInfo[] properties = ReflectionUtils.GetProperties(classType);
IDataReader sdr = null;
try
{
sdr = AdoHelper.ExecuteReader(AdoHelper.ConnectionString, CommandType.Text, strSql);
while (sdr.Read())
{
T entity = new T();
foreach (PropertyInfo property in properties)
{
ReflectionUtils.SetPropertyValue(entity, property, sdr[property.Name]);
}
listArr.Add(entity);
}
}
catch (Exception ex)
{
throw ex;
}
finally
{
if (sdr != null) sdr.Close();
}
return listArr;
}
#endregion
#region 通过主键ID查询数据
public List<T> FindById<T>(object id) where T : new()
{
List<T> listArr = new List<T>();
PropertyInfo[] properties = ReflectionUtils.GetProperties(new T().GetType());
TableInfo tableInfo = DbEntityUtils.GetTableInfo(new T(), DbOperateType.SELECT);
String strSql = DbEntityUtils.GetFindByIdSql(tableInfo);
IDbDataParameter[] parms = DbFactory.CreateDbParameters(1);
parms[0].ParameterName = tableInfo.Id.Key;
parms[0].Value = id;
IDataReader sdr = null;
try
{
sdr = AdoHelper.ExecuteReader(AdoHelper.ConnectionString, CommandType.Text, strSql, parms);
while (sdr.Read())
{
T entity = new T();
foreach (PropertyInfo property in properties)
{
String name = tableInfo.PropToColumn[property.Name].ToString();
ReflectionUtils.SetPropertyValue(entity, property, sdr[name]);
}
listArr.Add(entity);
}
}
catch (Exception ex)
{
throw ex;
}
finally
{
if (sdr != null) sdr.Close();
}
return listArr;
}
#endregion
#region Transaction 注入事物对象属性
public IDbTransaction Transaction
{
get
{
return transaction;
}
set
{
transaction = value;
}
}
#endregion
}
}
EntityManagerFactory类:
EntityManagerFactoryusing System;
using System.Collections.Generic;
using System.Text;
using System.Configuration;
namespace System.Orm.EntityManager
{
public class EntityManagerFactory
{
public static EntityManager CreateEntityManager()
{
return new EntityManagerImpl();
}
}
}
TransactionManager类:
TransactionManagerusing System;
using System.Collections.Generic;
using System.Text;
using System.Data;
using System.Configuration;
using System.Data.SqlClient;
using System.Data.OracleClient;
using System.Data.Common;
using System.Orm.DBUtility;
namespace System.Orm.DBTransaction
{
public class TransactionManager
{
public static IDbTransaction CreateTransaction()
{
return DbFactory.CreateDbTransaction();
}
}
}
StudentDAL调用类:
StudentDALusing System;
using System.Collections.Generic;
using System.Text;
using System.Data;
using Entity;
using System.Orm.EntityManager;
using System.Orm.DBTransaction;
namespace DAL
{
public class StudentDAL
{
EntityManager entityManager = EntityManagerFactory.CreateEntityManager();
public StudentDAL() { }
public StudentDAL(IDbTransaction transaction)
{
entityManager.Transaction = transaction;
}
public List<StudentEntity> FindAll()
{
return entityManager.FindAll<StudentEntity>();
}
public int Save(StudentEntity entity)
{
return entityManager.Save(entity);
}
public int Update(StudentEntity entity)
{
return entityManager.Update(entity);
}
public int Remove(StudentEntity entity)
{
return entityManager.Remove(entity);
}
public int Remove(object id)
{
return entityManager.Remove<StudentEntity>(id);
}
public List<StudentEntity> FindById(object id)
{
return entityManager.FindById<StudentEntity>(id);
}
}
}
有一些园友希望能提供源码,这里将提供源码下载地址:OrmTest.rar下载