|
2012-04-19, 09:45 上午
|
失去的画布
 等级: 10级
注册: 2011年12月2日
经验: 12
积分: 12
精华: 0
发贴: 13
排名: 2,663
|

|
|
|
说明:此篇文章是给那些和我一样仍在使用ADO.NET访问数据库的.NET开发人员写的,因为某些原因,比如还在使用.NET3.0以下版本开发.NET应用或者所使用的数据库对ORM支持不是很好,或者是对ORM的性能有所怀疑(其实在访问量不是很大的情况下没有必要过分追求性能的极限)等等,这部分人仍在使用传统的ADO.NET来访问数据库,他们或手写或采用代码生成工具来生成实体类和增删改查的SQL语句,在将DataTable或者DataReader转换成对应的实体类的时候仍需要一行行写代码,本类就是为了解决这个问题的,可以用几个类来实现方便快捷的转换。本类库在SQL Server/MySQL/SQLite下测试通过,由于条件有限未在所有的数据库下测试,如果有问题请在此留言或者在周公的微博留言(http://weibo.com/zhoufoxcn)。 其实在写这套类库之前,去年周公就写了两篇有关的文章,一篇叫《用C#打造自己的实体转换器》,另一篇叫《利用ADO.NET的体系架构打造通用的数据库访问通用类》,本篇的代码就是在这两篇文章的基础上经过实际应用修改而成,主要是修正了几个问题:1.如果在SELECT子句的字段中不包含对应实体类的某个属性,那么该属性的值就为它对应Type的默认值(如int,short为0,引用类型为null),而不是像以前那样用Ignorable,因为有些属性可能在某个查询中需要而在另外一个查询中不需要,采用Ignorable这样的Attribute的话太武断;2.修正了在泛型类型时的错误;3.在类的属性类型中除了支持常见数据类型(数值类型、可空类型和string)之外,还支持byte[]这种常见的数据类型。 本类库共有5个类:DbProviderType、ProviderFactory、DbUtility、EntityReader、ColumnNameAttribute。 其中DbProviderType、ProviderFactory、DbUtility三个类的相关代码如下: - using System;
- using System.Collections.Generic;
- using System.Data;
- using System.Data.Common;
-
- namespace NetSkycn.Data
- {
-
-
-
-
-
-
-
- public sealed class DbUtility
- {
- public string ConnectionString { get; set; }
- private DbProviderFactory providerFactory;
-
-
-
-
-
- public DbUtility(string connectionString, DbProviderType providerType)
- {
- ConnectionString = connectionString;
- providerFactory = ProviderFactory.GetDbProviderFactory(providerType);
- if (providerFactory == null)
- {
- throw new ArgumentException("Can't load DbProviderFactory for given value of providerType");
- }
- }
-
-
-
-
-
-
- public int ExecuteNonQuery(string sql, IList<DbParameter> parameters)
- {
- return ExecuteNonQuery(sql, parameters, CommandType.Text);
- }
-
-
-
-
-
-
-
- public int ExecuteNonQuery(string sql, IList<DbParameter> parameters, CommandType commandType)
- {
- using (DbCommand command = CreateDbCommand(sql, parameters, commandType))
- {
- command.Connection.Open();
- int affectedRows = command.ExecuteNonQuery();
- command.Connection.Close();
- return affectedRows;
- }
- }
-
-
-
-
-
-
-
- public DbDataReader ExecuteReader(string sql, IList<DbParameter> parameters)
- {
- return ExecuteReader(sql, parameters, CommandType.Text);
- }
-
-
-
-
-
-
-
-
- public DbDataReader ExecuteReader(string sql, IList<DbParameter> parameters, CommandType commandType)
- {
- DbCommand command = CreateDbCommand(sql, parameters, commandType);
- command.Connection.Open();
- return command.ExecuteReader(CommandBehavior.CloseConnection);
- }
-
-
-
-
-
-
-
- public DataTable ExecuteDataTable(string sql, IList<DbParameter> parameters)
- {
- return ExecuteDataTable(sql, parameters, CommandType.Text);
- }
-
-
-
-
-
-
-
- public DataTable ExecuteDataTable(string sql, IList<DbParameter> parameters, CommandType commandType)
- {
- using (DbCommand command = CreateDbCommand(sql, parameters, commandType))
- {
- using (DbDataAdapter adapter = providerFactory.CreateDataAdapter())
- {
- adapter.SelectCommand = command;
- DataTable data = new DataTable();
- adapter.Fill(data);
- return data;
- }
- }
- }
-
-
-
-
-
-
-
- public Object ExecuteScalar(string sql, IList<DbParameter> parameters)
- {
- return ExecuteScalar(sql, parameters, CommandType.Text);
- }
-
-
-
-
-
-
-
-
- public Object ExecuteScalar(string sql, IList<DbParameter> parameters, CommandType commandType)
- {
- using (DbCommand command = CreateDbCommand(sql, parameters, commandType))
- {
- command.Connection.Open();
- object result = command.ExecuteScalar();
- command.Connection.Close();
- return result;
- }
- }
-
-
-
-
-
-
-
-
- public List<T> QueryForList<T>(string sql, IList<DbParameter> parameters) where T : new()
- {
- return QueryForList<T>(sql, parameters, CommandType.Text);
- }
-
-
-
-
-
-
-
-
-
- public List<T> QueryForList<T>(string sql, IList<DbParameter> parameters, CommandType commandType) where T : new()
- {
- DataTable data = ExecuteDataTable(sql, parameters, commandType);
- return EntityReader.GetEntities<T>(data);
- }
-
-
-
-
-
-
-
- public T QueryForObject<T>(string sql, IList<DbParameter> parameters) where T : new()
- {
- return QueryForObject<T>(sql, parameters, CommandType.Text);
- }
-
-
-
-
-
-
-
-
-
- public T QueryForObject<T>(string sql, IList<DbParameter> parameters, CommandType commandType) where T : new()
- {
- return QueryForList<T>(sql, parameters, commandType)[0];
- }
-
- public DbParameter CreateDbParameter(string name, object value)
- {
- return CreateDbParameter(name, ParameterDirection.Input, value);
- }
-
- public DbParameter CreateDbParameter(string name, ParameterDirection parameterDirection, object value)
- {
- DbParameter parameter = providerFactory.CreateParameter();
- parameter.ParameterName = name;
- parameter.Value = value;
- parameter.Direction = parameterDirection;
- return parameter;
- }
-
-
-
-
-
-
-
-
- private DbCommand CreateDbCommand(string sql, IList<DbParameter> parameters, CommandType commandType)
- {
- DbConnection connection = providerFactory.CreateConnection();
- DbCommand command = providerFactory.CreateCommand();
- connection.ConnectionString = ConnectionString;
- command.CommandText = sql;
- command.CommandType = commandType;
- command.Connection = connection;
- if (!(parameters == null || parameters.Count == 0))
- {
- foreach (DbParameter parameter in parameters)
- {
- command.Parameters.Add(parameter);
- }
- }
- return command;
- }
- }
-
-
-
- public enum DbProviderType : byte
- {
- SqlServer,
- MySql,
- SQLite,
- Oracle,
- ODBC,
- OleDb,
- Firebird,
- PostgreSql,
- DB2,
- Informix,
- SqlServerCe
- }
-
-
-
- public class ProviderFactory
- {
- private static Dictionary<DbProviderType, string> providerInvariantNames = new Dictionary<DbProviderType, string>();
- private static Dictionary<DbProviderType, DbProviderFactory> providerFactoies = new Dictionary<DbProviderType, DbProviderFactory>(20);
- static ProviderFactory()
- {
-
- providerInvariantNames.Add(DbProviderType.SqlServer, "System.Data.SqlClient");
- providerInvariantNames.Add(DbProviderType.OleDb, "System.Data.OleDb");
- providerInvariantNames.Add(DbProviderType.ODBC, "System.Data.ODBC");
- providerInvariantNames.Add(DbProviderType.Oracle, "Oracle.DataAccess.Client");
- providerInvariantNames.Add(DbProviderType.MySql, "MySql.Data.MySqlClient");
- providerInvariantNames.Add(DbProviderType.SQLite, "System.Data.SQLite");
- providerInvariantNames.Add(DbProviderType.Firebird, "FirebirdSql.Data.Firebird");
- providerInvariantNames.Add(DbProviderType.PostgreSql, "Npgsql");
- providerInvariantNames.Add(DbProviderType.DB2, "IBM.Data.DB2.iSeries");
- providerInvariantNames.Add(DbProviderType.Informix, "IBM.Data.Informix");
- providerInvariantNames.Add(DbProviderType.SqlServerCe, "System.Data.SqlServerCe");
- }
-
-
-
-
-
- public static string GetProviderInvariantName(DbProviderType providerType)
- {
- return providerInvariantNames[providerType];
- }
-
-
-
-
-
- public static DbProviderFactory GetDbProviderFactory(DbProviderType providerType)
- {
-
- if (!providerFactoies.ContainsKey(providerType))
- {
- providerFactoies.Add(providerType, ImportDbProviderFactory(providerType));
- }
- return providerFactoies[providerType];
- }
-
-
-
-
-
- private static DbProviderFactory ImportDbProviderFactory(DbProviderType providerType)
- {
- string providerName = providerInvariantNames[providerType];
- DbProviderFactory factory = null;
- try
- {
-
- factory = DbProviderFactories.GetFactory(providerName);
- }
- catch (ArgumentException e)
- {
- factory = null;
- }
- return factory;
- }
- }
- }
其中EntityReader、ColumnNameAttribute的代码如下: - using System;
- using System.Collections.Generic;
- using System.Data;
- using System.Data.Common;
- using System.Reflection;
-
- namespace NetSkycn.Data
- {
-
-
-
-
-
-
-
- public sealed class EntityReader
- {
- private const BindingFlags BindingFlag = BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance;
-
- private static Dictionary<Type, Dictionary<string, PropertyInfo>> propertyMappings = new Dictionary<Type, Dictionary<string, PropertyInfo>>();
-
-
-
-
-
-
-
- public static List<T> GetEntities<T>(DataTable dataTable) where T : new()
- {
- if (dataTable == null)
- {
- throw new ArgumentNullException("dataTable");
- }
-
- if (typeof(T) == typeof(string)||typeof(T)==typeof(byte[])|| typeof(T).IsValueType)
- {
- return GetSimpleEntities<T>(dataTable);
- }
- else
- {
- return GetComplexEntities<T>(dataTable);
- }
- }
-
-
-
-
-
-
- public static List<T> GetEntities<T>(DbDataReader reader) where T : new()
- {
- List<T> list = new List<T>();
- if (reader == null)
- {
- throw new ArgumentNullException("reader");
- }
-
- if (typeof(T) == typeof(string) || typeof(T).IsValueType)
- {
- return GetSimpleEntities<T>(reader);
- }
- else
- {
- return GetComplexEntities<T>(reader);
- }
-
- }
-
-
-
-
-
-
- private static List<T> GetSimpleEntities<T>(DataTable dataTable) where T : new()
- {
- List<T> list = new List<T>();
- foreach (DataRow row in dataTable.Rows)
- {
- list.Add((T)GetValueFromObject(row[0], typeof(T)));
- }
- return list;
- }
-
-
-
-
-
-
- private static object GetValueFromObject(object value, Type targetType)
- {
- if (targetType == typeof(string))
- {
- return GetString(value);
- }
- else if(targetType==typeof(byte[]))
- {
- return GetBinary(value);
- }
- else if (targetType.IsGenericType)
- {
- return GetGenericValueFromObject(value, targetType);
- }
- else
- {
- return GetNonGenericValueFromObject(value, targetType);
- }
- }
-
-
-
-
-
-
-
- private static List<T> GetComplexEntities<T>(DataTable dataTable) where T : new()
- {
- if (!propertyMappings.ContainsKey(typeof(T)))
- {
- GenerateTypePropertyMapping(typeof(T));
- }
- List<T> list = new List<T>();
- Dictionary<string, PropertyInfo> properties = propertyMappings[typeof(T)];
- T t;
- foreach (DataRow row in dataTable.Rows)
- {
- t = new T();
- foreach (KeyValuePair<string, PropertyInfo> item in properties)
- {
-
- if (row[item.Key] != null)
- {
- item.Value.SetValue(t, GetValueFromObject(row[item.Key], item.Value.PropertyType), null);
- }
- }
- list.Add(t);
- }
- return list;
- }
-
-
-
-
-
-
-
- private static List<T> GetComplexEntities<T>(DbDataReader reader) where T : new()
- {
- if (!propertyMappings.ContainsKey(typeof(T)))
- {
- GenerateTypePropertyMapping(typeof(T));
- }
- List<T> list = new List<T>();
- Dictionary<string, PropertyInfo> properties = propertyMappings[typeof(T)];
- T t;
- while (reader.Read())
- {
- t = new T();
- foreach (KeyValuePair<string, PropertyInfo> item in properties)
- {
-
- if (reader[item.Key] != null)
- {
- item.Value.SetValue(t, GetValueFromObject(reader[item.Key], item.Value.PropertyType), null);
- }
- }
- list.Add(t);
- }
- return list;
- }
-
-
-
-
-
-
- private static List<T> GetSimpleEntities<T>(DbDataReader reader)
- {
- List<T> list = new List<T>();
- while (reader.Read())
- {
- list.Add((T)GetValueFromObject(reader[0], typeof(T)));
- }
- return list;
- }
-
-
-
-
-
- private static object GetString(object value)
- {
- return Convert.ToString(value);
- }
-
-
-
-
-
-
-
- private static object GetEnum(object value, Type targetType)
- {
- return Enum.P***(targetType, value.ToString());
- }
-
-
-
-
-
-
- private static object GetBoolean(object value)
- {
- if (value is Boolean)
- {
- return value;
- }
- else
- {
- byte byteValue = (byte)GetByte(value);
- if (byteValue == 0)
- {
- return false;
- }
- else
- {
- return true;
- }
- }
- }
-
-
-
-
-
-
- private static object GetByte(object value)
- {
- if (value is Byte)
- {
- return value;
- }
- else
- {
- return byte.P***(value.ToString());
- }
- }
-
-
-
-
-
-
- private static object GetSByte(object value)
- {
- if (value is SByte)
- {
- return value;
- }
- else
- {
- return SByte.P***(value.ToString());
- }
- }
-
-
-
-
-
-
- private static object GetChar(object value)
- {
- if (value is Char)
- {
- return value;
- }
- else
- {
- return Char.P***(value.ToString());
- }
- }
-
-
-
-
-
-
- private static object GetGuid(object value)
- {
- if (value is Guid)
- {
- return value;
- }
- else
- {
- return new Guid(value.ToString());
- }
- }
-
-
-
-
-
-
- private static object GetInt16(object value)
- {
- if (value is Int16)
- {
- return value;
- }
- else
- {
- return Int16.P***(value.ToString());
- }
- }
-
-
-
-
-
-
- private static object GetUInt16(object value)
- {
- if (value is UInt16)
- {
- return value;
- }
- else
- {
- return UInt16.P***(value.ToString());
- }
- }
-
-
-
-
-
-
- private static object GetInt32(object value)
- {
- if (value is Int32)
- {
- return value;
- }
- else
- {
- return Int32.P***(value.ToString());
- }
- }
-
-
-
-
-
-
- private static object GetUInt32(object value)
- {
- if (value is UInt32)
- {
- return value;
- }
- else
- {
- return UInt32.P***(value.ToString());
- }
- }
-
-
-
-
-
-
- private static object GetInt64(object value)
- {
- if (value is Int64)
- {
- return value;
- }
- else
- {
- return Int64.P***(value.ToString());
- }
- }
-
-
-
-
-
-
- private static object GetUInt64(object value)
- {
- if (value is UInt64)
- {
- return value;
- }
- else
- {
- return UInt64.P***(value.ToString());
- }
- }
-
-
-
-
-
-
- private static object GetSingle(object value)
- {
- if (value is Single)
- {
- return value;
- }
- else
- {
- return Single.P***(value.ToString());
- }
- }
-
-
-
-
-
-
- private static object GetDouble(object value)
- {
- if (value is Double)
- {
- return value;
- }
- else
- {
- return Double.P***(value.ToString());
- }
- }
-
-
-
-
-
-
- private static object GetDecimal(object value)
- {
- if (value is Decimal)
- {
- return value;
- }
- else
- {
- return Decimal.P***(value.ToString());
- }
- }
-
-
-
-
-
-
- private static object GetDateTime(object value)
- {
- if (value is DateTime)
- {
- return value;
- }
- else
- {
- return DateTime.P***(value.ToString());
- }
- }
-
-
-
-
-
-
- private static object GetTimeSpan(object value)
- {
- if (value is TimeSpan)
- {
- return value;
- }
- else
- {
- return TimeSpan.P***(value.ToString());
- }
- }
-
-
-
-
-
-
- private static byte[] GetBinary(object value)
- {
-
- if (value == DBNull.Value)
- {
- return null;
- }
- else if (value is Byte[])
- {
- return (byte[])(value);
- }
- else
- {
- return null;
- }
- }
-
-
-
-
-
-
-
- private static object GetGenericValueFromObject(object value, Type targetType)
- {
- if (value == DBNull.Value)
- {
- return null;
- }
- else
- {
-
-
- Type nonGenericType = targetType.GetGenericArguments()[0];
- return GetNonGenericValueFromObject(value, nonGenericType);
- }
- }
-
-
-
-
-
-
-
- private static object GetNonGenericValueFromObject(object value, Type targetType)
- {
- if (targetType.IsEnum)
- {
- return GetEnum(value, targetType);
- }
- else
- {
- switch (targetType.Name)
- {
- case "Byte": return GetByte(value);
- case "SByte": return GetSByte(value);
- case "Char": return GetChar(value);
- case "Boolean": return GetBoolean(value);
- case "Guid": return GetGuid(value);
- case "Int16": return GetInt16(value);
- case "UInt16": return GetUInt16(value);
- case "Int32": return GetInt32(value);
- case "UInt32": return GetUInt32(value);
- case "Int64": return GetInt64(value);
- case "UInt64": return GetUInt64(value);
- case "Single": return GetSingle(value);
- case "Double": return GetDouble(value);
- case "Decimal": return GetDecimal(value);
- case "DateTime": return GetDateTime(value);
- case "TimeSpan": return GetTimeSpan(value);
- default: return null;
- }
- }
- }
-
-
-
-
-
- private static void GenerateTypePropertyMapping(Type type)
- {
- if (type != null)
- {
- PropertyInfo[] properties = type.GetProperties(BindingFlag);
- Dictionary<string, PropertyInfo> propertyColumnMapping = new Dictionary<string, PropertyInfo>(properties.Length);
- string description = string.Empty;
- Attribute[] attibutes = null;
- string columnName = string.Empty;
- foreach (PropertyInfo p in properties)
- {
- columnName = string.Empty;
- attibutes = Attribute.GetCustomAttributes(p);
- foreach (Attribute attribute in attibutes)
- {
-
- if (attribute.GetType() == typeof(ColumnNameAttribute))
- {
- columnName = ((ColumnNameAttribute)attribute).ColumnName;
- break;
- }
- }
-
- if (p.CanWrite)
- {
-
- if (string.IsNullOrEmpty(columnName))
- {
- columnName = p.Name;
- }
- propertyColumnMapping.Add(columnName, p);
- }
- }
- propertyMappings.Add(type, propertyColumnMapping);
- }
- }
- }
-
-
-
-
- public class ColumnNameAttribute : Attribute
- {
-
-
-
- public string ColumnName { get; set; }
-
-
-
-
- public ColumnNameAttribute(string columnName)
- {
- ColumnName = columnName;
- }
- }
- }
本类库经过NUnit测试通过,测试截图如下:
 因为测试用的代码涉及到下一篇博文的内容,所以会将测试代码放到下一篇博文中。
|
|
|
分享按钮 |
IP 地址: 已登录
来自: 已登录
|
|
|
|
|
|
|
|