写了一个数据库操作类,达到通过实体自动映射数据库进行查询,添加,删除,修改操作,啥也不说了,直接上代码:
反回一个实体,通过枚举参数确定使用属性/特性进行字段映射(转款做表和主键映射)
Code
///
/// 获取单个实体
///
/// 实体(泛型)
/// 命令
/// 命令类型
/// 按属性/特性映射
/// 参数数组
/// 实体
public static TEntity GetEntity<TEntity>(string cmdText, CommandType commandType,AppEnum.ExeEntityType exeEntityType,params DbParameter[] paramers) where TEntity:new()
{
TEntity entity = new TEntity();
using (IDataReader reader = DbHelper.CreateDataReader(cmdText, commandType, paramers))
{
if (reader.Read())
{
Type entityType = entity.GetType();
PropertyInfo[] propertyInfos=entityType.GetProperties();
foreach(PropertyInfo property in propertyInfos)
{
if (exeEntityType == AppEnum.ExeEntityType.isAttribute)
{
DataFieldAttribute datafieldAttribute = GetDataFieldAttribute(property);
if (datafieldAttribute == null)
throw new AssionException("AppEnum.ExeEntityType枚举为 isAttribute 时,实体类需要指定特性!");
if (!(reader[datafieldAttribute.FieldName] is DBNull))
property.SetValue(entity, reader[datafieldAttribute.FieldName], null);
}
else if (exeEntityType == AppEnum.ExeEntityType.isProperty)
{
if (!(reader[property.Name] is DBNull))
property.SetValue(entity, reader[property.Name], null);
}
}
}
reader.Close();
reader.Dispose();
}
return entity;
}
///
/// 获取单个实体
///
///
/// 命令
/// 命令类型
/// 按属性/特性映射
/// 参数数组
///
public static TEntity GetEntity<TEntity>(string cmdText, CommandType commandType,AppEnum.ExeEntityType exeEntityType,params DbParameter[] paramers) where TEntity:new()
{
TEntity entity = new TEntity();
using (IDataReader reader = DbHelper.CreateDataReader(cmdText, commandType, paramers))
{
if (reader.Read())
{
Type entityType = entity.GetType();
PropertyInfo[] propertyInfos=entityType.GetProperties();
foreach(PropertyInfo property in propertyInfos)
{
if (exeEntityType == AppEnum.ExeEntityType.isAttribute)
{
DataFieldAttribute datafieldAttribute = GetDataFieldAttribute(property);
if (datafieldAttribute == null)
throw new AssionException("AppEnum.ExeEntityType枚举为 isAttribute 时,实体类需要指定特性!");
if (!(reader[datafieldAttribute.FieldName] is DBNull))
property.SetValue(entity, reader[datafieldAttribute.FieldName], null);
}
else if (exeEntityType == AppEnum.ExeEntityType.isProperty)
{
if (!(reader[property.Name] is DBNull))
property.SetValue(entity, reader[property.Name], null);
}
}
}
reader.Close();
reader.Dispose();
}
return entity;
}
返回一个实体集合,类似上面的
Code
///
/// 获取实体集合
///
/// 实体(泛型)
/// 命令
/// 命令类型
/// 按属性/特性映射
/// 参数数组
/// 实体集合
public static IList<TEntity> GetEntityList<TEntity>(string cmdText, CommandType commandType, AppEnum.ExeEntityType exeEntityType, params DbParameter[] paramers) where TEntity : new()
{
IList<TEntity> entityList = new List<TEntity>();
using (IDataReader reader = DbHelper.CreateDataReader(cmdText, commandType, paramers))
{
while (reader.Read())
{
TEntity entity = new TEntity();
Type entityType = entity.GetType();
PropertyInfo[] propertyInfos = entityType.GetProperties();
foreach (PropertyInfo property in propertyInfos)
{
if (exeEntityType == AppEnum.ExeEntityType.isAttribute)
{
DataFieldAttribute datafieldAttribute = GetDataFieldAttribute(property);
if (datafieldAttribute == null)
throw new AssionException("AppEnum.ExeEntityType枚举为 isAttribute 时,实体类需要指定特性!");
if (!(reader[datafieldAttribute.FieldName] is DBNull))
property.SetValue(entity, reader[datafieldAttribute.FieldName], null);
}
else if (exeEntityType == AppEnum.ExeEntityType.isProperty)
{
if (!(reader[property.Name] is DBNull))
property.SetValue(entity, reader[property.Name], null);
}
}
entityList.Add(entity);
}
}
return entityList;
}
///
/// 获取实体集合
///
///
/// 命令
/// 命令类型
/// 按属性/特性映射
/// 参数数组
///
public static IList<TEntity> GetEntityList<TEntity>(string cmdText, CommandType commandType, AppEnum.ExeEntityType exeEntityType, params DbParameter[] paramers) where TEntity : new()
{
IList<TEntity> entityList = new List<TEntity>();
using (IDataReader reader = DbHelper.CreateDataReader(cmdText, commandType, paramers))
{
while (reader.Read())
{
TEntity entity = new TEntity();
Type entityType = entity.GetType();
PropertyInfo[] propertyInfos = entityType.GetProperties();
foreach (PropertyInfo property in propertyInfos)
{
if (exeEntityType == AppEnum.ExeEntityType.isAttribute)
{
DataFieldAttribute datafieldAttribute = GetDataFieldAttribute(property);
if (datafieldAttribute == null)
throw new AssionException("AppEnum.ExeEntityType枚举为 isAttribute 时,实体类需要指定特性!");
if (!(reader[datafieldAttribute.FieldName] is DBNull))
property.SetValue(entity, reader[datafieldAttribute.FieldName], null);
}
else if (exeEntityType == AppEnum.ExeEntityType.isProperty)
{
if (!(reader[property.Name] is DBNull))
property.SetValue(entity, reader[property.Name], null);
}
}
entityList.Add(entity);
}
}
return entityList;
}
执行SQL实体映射操作,可以写INSERT、UPDATE、DELETE 操作,这样灵活些,抽时间再完成个自动映射生成SQL的,不过灵活性就差些了
Code
///
/// 插入,更新,删除数据库实体
///
/// 实体(泛型)
/// 命令
/// 命令类型
/// 按属性/特性映射
/// 实体
/// 影响行数
public static int ExeEntity<TEntity>(string cmdText, CommandType commandType, AppEnum.ExeEntityType exeEntityType,TEntity entity) where TEntity : new()
{
Type entityType = entity.GetType();
PropertyInfo[] propertyInfos = entityType.GetProperties();
List<DbParameter> paramerList = new List<DbParameter>();
foreach (PropertyInfo property in propertyInfos)
{
if (exeEntityType == AppEnum.ExeEntityType.isAttribute)
{
DataFieldAttribute datafieldAttribute = GetDataFieldAttribute(property);
if (datafieldAttribute == null)
throw new AssionException("AppEnum.ExeEntityType枚举为 isAttribute 时,实体类需要指定特性!");
object oval=property.GetValue(entity,null);
oval = oval == null ? DBNull.Value : oval;
paramerList.Add(DbHelper.CreateParamer("@" + datafieldAttribute.FieldName, oval));
}
else if (exeEntityType == AppEnum.ExeEntityType.isProperty)
{
object oval = property.GetValue(entity, null);
oval = oval == null ? DBNull.Value : oval;
paramerList.Add(DbHelper.CreateParamer("@" + property.Name, oval));
}
}
int retval=DbHelper.ExecuteNonQuery(cmdText, commandType, paramerList.ToArray());
return retval;
}
///
/// 插入,更新,删除数据库实体
///
///
/// 命令
/// 命令类型
/// 按属性/特性映射
/// 实体
///
public static int ExeEntity<TEntity>(string cmdText, CommandType commandType, AppEnum.ExeEntityType exeEntityType,TEntity entity) where TEntity : new()
{
Type entityType = entity.GetType();
PropertyInfo[] propertyInfos = entityType.GetProperties();
List<DbParameter> paramerList = new List<DbParameter>();
foreach (PropertyInfo property in propertyInfos)
{
if (exeEntityType == AppEnum.ExeEntityType.isAttribute)
{
DataFieldAttribute datafieldAttribute = GetDataFieldAttribute(property);
if (datafieldAttribute == null)
throw new AssionException("AppEnum.ExeEntityType枚举为 isAttribute 时,实体类需要指定特性!");
object oval=property.GetValue(entity,null);
oval = oval == null ? DBNull.Value : oval;
paramerList.Add(DbHelper.CreateParamer("@" + datafieldAttribute.FieldName, oval));
}
else if (exeEntityType == AppEnum.ExeEntityType.isProperty)
{
object oval = property.GetValue(entity, null);
oval = oval == null ? DBNull.Value : oval;
paramerList.Add(DbHelper.CreateParamer("@" + property.Name, oval));
}
}
int retval=DbHelper.ExecuteNonQuery(cmdText, commandType, paramerList.ToArray());
return retval;
}
下面是字段的自定义特性和获取特性的代码:
Code
///
/// 自定义特性:映射数据库字段名
///
[AttributeUsage(AttributeTargets.Struct|AttributeTargets.Property|AttributeTargets.Field)]
public class DataFieldAttribute:Attribute
{
private string _fieldName; //字段名
public string FieldName
{
get { return _fieldName; }
set { _fieldName = value; }
}
public DataFieldAttribute(string fieldName)
{
this._fieldName = fieldName;
}
}
///
/// 获取DataFieldAttribute特性
///
/// 属性
/// DataFieldAttribute
private static DataFieldAttribute GetDataFieldAttribute(PropertyInfo property)
{
object[] oArr=property.GetCustomAttributes(true);
for(int i=0;i<oArr.Length;i++)
{
if(oArr[i] is DataFieldAttribute)
return (DataFieldAttribute)oArr[i];
}
return null;
}
///
/// 自定义特性:映射数据库字段名
///
[AttributeUsage(AttributeTargets.Struct|AttributeTargets.Property|AttributeTargets.Field)]
public class DataFieldAttribute:Attribute
{
private string _fieldName; //字段名
public string FieldName
{
get { return _fieldName; }
set { _fieldName = value; }
}
public DataFieldAttribute(string fieldName)
{
this._fieldName = fieldName;
}
}
///
/// 获取DataFieldAttribute特性
///
/// 属性
///
private static DataFieldAttribute GetDataFieldAttribute(PropertyInfo property)
{
object[] oArr=property.GetCustomAttributes(true);
for(int i=0;i<oArr.Length;i++)
{
if(oArr[i] is DataFieldAttribute)
return (DataFieldAttribute)oArr[i];
}
return null;
}
使用示例:
首先实体加上特性:
Code
public class People
{
[PrimaryKey()]
[DataField("DbName")]
public string Name { get; set; }
[DataField("DbTel")]
public string tel { get; set; }
}
调用代码:
public class People
{
[PrimaryKey()]
[DataField("DbName")]
public string Name { get; set; }
[DataField("DbTel")]
public string tel { get; set; }
}
获取一个实体集合:
IList
<
People
>
peopleList
=
ExecuteEntity.GetEntityList
<
People
>
(
"
SELECT DbName,DbTel FROM Test
"
, CommandType.Text, AppEnum.ExeEntityType.isAttribute,
null
);
向数据库插入实体数据:
ExecuteEntity.ExeEntity
<
People
>
(
"
INSERT INTO Test (DbName,DbTel) VALUES (@DbName,@DbTel)
"
, CommandType.Text, AppEnum.ExeEntityType.isAttribute, people);
最后再附上一个基础的数据库操作类:
Code
///
/// 通用数据库帮助类
///
public static class DbHelper
{
#region 连接配置读取
///
/// 数据库类型
///
private static readonly string dataProvider = ConfigurationManager.ConnectionStrings["ConnectionString"].ProviderName;
///
/// 反射数据库类型
///
private static readonly DbProviderFactory dataFactory = DbProviderFactories.GetFactory(dataProvider);
///
/// 数据库连接字符串
///
private static readonly string connectionString = ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString;
#endregion
#region 连接命令
///
/// 创建连接
///
/// dbconnection
public static DbConnection CreateConnection()
{
DbConnection dbConn = dataFactory.CreateConnection();
dbConn.ConnectionString = connectionString;
return dbConn;
}
///
/// 创建命令
///
/// 命令
/// 命令类型
/// 参数数组
/// command
public static DbCommand CreateCommand(string cmdText,CommandType commandType,params DbParameter[] paramers)
{
DbConnection dbConn=CreateConnection();
dbConn.Open();
DbCommand dbCmd = dataFactory.CreateCommand();
dbCmd.Connection = dbConn;
dbCmd.CommandText = cmdText;
dbCmd.CommandType = commandType;
if(paramers!=null)
dbCmd.Parameters.AddRange(paramers);
return dbCmd;
}
#endregion
#region 数据执行方法
///
/// 创建带参数的只读器
///
/// 命令
/// 命令类型
/// 参数数组
/// reader
public static DbDataReader CreateDataReader(string cmdText, CommandType commandType, params DbParameter[] paramers)
{
DbDataReader reader = null;
reader = CreateCommand(cmdText, commandType, paramers).ExecuteReader(CommandBehavior.CloseConnection);
return reader;
}
///
/// 创建无参数的只读器
///
/// 命令
/// 命令类型
/// reader
public static DbDataReader CreateDataReader(string cmdText, CommandType commandType)
{
DbDataReader reader = null;
reader = CreateCommand(cmdText, commandType, null).ExecuteReader(CommandBehavior.CloseConnection);
return reader;
}
///
/// 执行一个带参数的SQL/存储过程
///
/// 命令
/// 命令类型
/// 参数数组
/// 影响行数
public static int ExecuteNonQuery(string cmdText, CommandType commandType, params DbParameter[] paramers)
{
int retval = CreateCommand(cmdText, commandType, paramers).ExecuteNonQuery();
return retval;
}
///
/// 执行一个不带参数的SQL/存储过程
///
/// 命令
/// 命令类型
/// 影响行数
public static int ExecuteNonQuery(string cmdText, CommandType commandType)
{
int retval = CreateCommand(cmdText, commandType, null).ExecuteNonQuery();
return retval;
}
///
/// 执行一个带参数的SQL/存储过程有事务的
///
/// 事务
/// 命令
/// 命令类型
/// 参数数组
/// 影响行数
public static int ExecuteNonQuery(DbTransaction tran,string cmdText,CommandType commmandType,params DbParameter[] paramers)
{
DbConnection dbConn = tran.Connection;
DbCommand dbCmd=dataFactory.CreateCommand();
dbCmd.Connection=dbConn;
dbCmd.CommandType=commmandType;
dbCmd.CommandText=cmdText;
dbCmd.Parameters.AddRange(paramers);
dbCmd.Transaction = tran;
int retval=dbCmd.ExecuteNonQuery();
return retval;
}
///
/// 执行一个无参数的SQL/存储过程有事务的
///
/// 事务
/// 命令
/// 命令类型
/// 影响行数
public static int ExecuteNonQuery(DbTransaction tran, string cmdText, CommandType commmandType)
{
DbConnection dbConn = tran.Connection;
DbCommand dbCmd = dataFactory.CreateCommand();
dbCmd.Connection = dbConn;
dbCmd.CommandType = commmandType;
dbCmd.CommandText = cmdText;
dbCmd.Transaction = tran;
int retval = dbCmd.ExecuteNonQuery();
return retval;
}
///
/// 执行一个带参数的SQL/存储过程返回首行首列
///
/// 命令
/// 命令类型
/// 参数数组
/// 值
public static object ExecuteScalar(string cmdText, CommandType commandType, params DbParameter[] paramers)
{
object retval = CreateCommand(cmdText,commandType,paramers).ExecuteScalar();
return retval;
}
///
/// 执行一个无参数的SQL/存储过程返回首行首列
///
/// 命令
/// 命令类型
/// 参数数组
/// 值
public static object ExecuteScalar(string cmdText, CommandType commandType)
{
object retval = CreateCommand(cmdText,commandType,null).ExecuteScalar();
return retval;
}
#endregion
#region 创建参数方法
///
/// 创建参数
///
/// 参数名
/// 参数值
/// 参数
public static DbParameter CreateParamer(string pName, object pValue)
{
DbParameter paramer = dataFactory.CreateParameter();
paramer.ParameterName = pName;
paramer.Value = pValue;
return paramer;
}
///
/// 创建参数
///
/// 参数名
/// 参数值
/// 参数类型
/// 参数
public static DbParameter CreateParamer(string pName, object pValue, DbType pType)
{
DbParameter paramer = dataFactory.CreateParameter();
paramer.ParameterName = pName;
paramer.Value = pValue;
paramer.DbType = pType;
return paramer;
}
///
/// 创建参数
///
/// 参数名
/// 参数值
/// 参数类型
/// 长度
/// 参数
public static DbParameter CreateParamer(string pName, object pValue, DbType pType, int pSize)
{
DbParameter paramer = dataFactory.CreateParameter();
paramer.ParameterName = pName;
paramer.Value = pValue;
paramer.DbType = pType;
paramer.Size = pSize;
return paramer;
}
#endregion
}
///
/// 通用数据库帮助类
///
public static class DbHelper
{
#region 连接配置读取
///
/// 数据库类型
///
private static readonly string dataProvider = ConfigurationManager.ConnectionStrings["ConnectionString"].ProviderName;
///
/// 反射数据库类型
///
private static readonly DbProviderFactory dataFactory = DbProviderFactories.GetFactory(dataProvider);
///
/// 数据库连接字符串
///
private static readonly string connectionString = ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString;
#endregion
#region 连接命令
///
/// 创建连接
///
///
public static DbConnection CreateConnection()
{
DbConnection dbConn = dataFactory.CreateConnection();
dbConn.ConnectionString = connectionString;
return dbConn;
}
///
/// 创建命令
///
/// 命令
/// 命令类型
/// 参数数组
///
public static DbCommand CreateCommand(string cmdText,CommandType commandType,params DbParameter[] paramers)
{
DbConnection dbConn=CreateConnection();
dbConn.Open();
DbCommand dbCmd = dataFactory.CreateCommand();
dbCmd.Connection = dbConn;
dbCmd.CommandText = cmdText;
dbCmd.CommandType = commandType;
if(paramers!=null)
dbCmd.Parameters.AddRange(paramers);
return dbCmd;
}
#endregion
#region 数据执行方法
///
/// 创建带参数的只读器
///
/// 命令
/// 命令类型
/// 参数数组
///
public static DbDataReader CreateDataReader(string cmdText, CommandType commandType, params DbParameter[] paramers)
{
DbDataReader reader = null;
reader = CreateCommand(cmdText, commandType, paramers).ExecuteReader(CommandBehavior.CloseConnection);
return reader;
}
///
/// 创建无参数的只读器
///
/// 命令
/// 命令类型
///
public static DbDataReader CreateDataReader(string cmdText, CommandType commandType)
{
DbDataReader reader = null;
reader = CreateCommand(cmdText, commandType, null).ExecuteReader(CommandBehavior.CloseConnection);
return reader;
}
///
/// 执行一个带参数的SQL/存储过程
///
/// 命令
/// 命令类型
/// 参数数组
///
public static int ExecuteNonQuery(string cmdText, CommandType commandType, params DbParameter[] paramers)
{
int retval = CreateCommand(cmdText, commandType, paramers).ExecuteNonQuery();
return retval;
}
///
/// 执行一个不带参数的SQL/存储过程
///
/// 命令
/// 命令类型
///
public static int ExecuteNonQuery(string cmdText, CommandType commandType)
{
int retval = CreateCommand(cmdText, commandType, null).ExecuteNonQuery();
return retval;
}
///
/// 执行一个带参数的SQL/存储过程有事务的
///
/// 事务
/// 命令
/// 命令类型
/// 参数数组
///
public static int ExecuteNonQuery(DbTransaction tran,string cmdText,CommandType commmandType,params DbParameter[] paramers)
{
DbConnection dbConn = tran.Connection;
DbCommand dbCmd=dataFactory.CreateCommand();
dbCmd.Connection=dbConn;
dbCmd.CommandType=commmandType;
dbCmd.CommandText=cmdText;
dbCmd.Parameters.AddRange(paramers);
dbCmd.Transaction = tran;
int retval=dbCmd.ExecuteNonQuery();
return retval;
}
///
/// 执行一个无参数的SQL/存储过程有事务的
///
/// 事务
/// 命令
/// 命令类型
///
public static int ExecuteNonQuery(DbTransaction tran, string cmdText, CommandType commmandType)
{
DbConnection dbConn = tran.Connection;
DbCommand dbCmd = dataFactory.CreateCommand();
dbCmd.Connection = dbConn;
dbCmd.CommandType = commmandType;
dbCmd.CommandText = cmdText;
dbCmd.Transaction = tran;
int retval = dbCmd.ExecuteNonQuery();
return retval;
}
///
/// 执行一个带参数的SQL/存储过程返回首行首列
///
/// 命令
/// 命令类型
/// 参数数组
///
public static object ExecuteScalar(string cmdText, CommandType commandType, params DbParameter[] paramers)
{
object retval = CreateCommand(cmdText,commandType,paramers).ExecuteScalar();
return retval;
}
///
/// 执行一个无参数的SQL/存储过程返回首行首列
///
/// 命令
/// 命令类型
/// 参数数组
///
public static object ExecuteScalar(string cmdText, CommandType commandType)
{
object retval = CreateCommand(cmdText,commandType,null).ExecuteScalar();
return retval;
}
#endregion
#region 创建参数方法
///
/// 创建参数
///
/// 参数名
/// 参数值
///
public static DbParameter CreateParamer(string pName, object pValue)
{
DbParameter paramer = dataFactory.CreateParameter();
paramer.ParameterName = pName;
paramer.Value = pValue;
return paramer;
}
///
/// 创建参数
///
/// 参数名
/// 参数值
/// 参数类型
///
public static DbParameter CreateParamer(string pName, object pValue, DbType pType)
{
DbParameter paramer = dataFactory.CreateParameter();
paramer.ParameterName = pName;
paramer.Value = pValue;
paramer.DbType = pType;
return paramer;
}
///
/// 创建参数
///
/// 参数名
/// 参数值
/// 参数类型
/// 长度
///
public static DbParameter CreateParamer(string pName, object pValue, DbType pType, int pSize)
{
DbParameter paramer = dataFactory.CreateParameter();
paramer.ParameterName = pName;
paramer.Value = pValue;
paramer.DbType = pType;
paramer.Size = pSize;
return paramer;
}
#endregion
}
暂时就实现了这么多,继续开发,感觉一个强大的数据库操作类就是要大,要多,要全,要爆力!