标签:
/// <summary> /// 生产工厂对象 /// </summary> public class DBFactory { /// <summary> /// 创造实例 /// </summary> /// <typeparam name="T">类型</typeparam> /// <param name="assemblyString">程序集名称</param> /// <param name="typeName">类的全名</param> /// <param name="parameters">构造函数参数</param> /// <returns></returns> public T Creatobject<T>(string assemblyString, string typeName, Object[] parameters) { try { return (T)System.Reflection.Assembly.Load(assemblyString).CreateInstance(typeName, true, System.Reflection.BindingFlags.Default, null, parameters, null, null); } catch (Exception ex) { string s = ex.Message; } return default(T); } /// <summary> /// 创造实例 /// </summary> /// <param name="assemblyString">程序集名称</param> /// <param name="typeName">类的全名</param> /// <param name="parameters">构造函数参数</param> /// <returns></returns> public object Creatobject(string assemblyString, string typeName, Object[] parameters) { try { return System.Reflection.Assembly.Load(assemblyString).CreateInstance(typeName, true, System.Reflection.BindingFlags.Default, null, parameters, null, null); } catch (Exception ex) { } return null; } }//end
/// <summary> /// 数据库接口 /// </summary> public interface IDataBase { /// <summary> /// 关闭数据库 /// </summary> /// <param name="Link"></param> void CloseDataBase(DbConnection Link); /// <summary> /// 获取命令 /// </summary> /// <param name="query"></param> /// <returns></returns> DbCommand GetSqlStringCommand(string query); /// <summary> /// 获取存储过程执行命令 /// </summary> /// <param name="ProcName"></param> /// <returns></returns> DbCommand GetStoredProcCommand(string ProcName); /// <summary> /// 增加输入参数 /// </summary> /// <param name="cmd"></param> /// <param name="ParmsName"></param> /// <param name="ParmsType"></param> /// <param name="value"></param> void AddInParameter(DbCommand cmd, string ParmsName, DbType ParmsType, object value); /// <summary> /// 增加输出参数 /// </summary> /// <param name="cmd"></param> /// <param name="ParmsName"></param> /// <param name="ParmsType"></param> /// <param name="value"></param> void AddOutParameter(DbCommand cmd, string ParmsName, DbType ParmsType, object value); /// <summary> /// 添加 Database 参数信息 /// </summary> /// <typeparam name="T"></typeparam> /// <param name="cmd"></param> /// <param name="t"></param> void AddDataBaseParas<T>(DbCommand cmd, T t); /// <summary> /// 获取输出参数值或者输出参数的字典 /// </summary> /// <param name="cmd"></param> /// <param name="ParmsName"></param> /// <returns></returns> object GetParameterValueOrKeyValue(DbCommand cmd, string ParmsName = ""); /// <summary> /// 执行获取DataSet /// </summary> /// <param name="cmd"></param> /// <returns></returns> DataSet ExecuteDataSet(DbCommand cmd); /// <summary> /// 执行sql语句返回受影响的行数 /// </summary> /// <param name="cmd"></param> /// <returns></returns> int ExecuteNonQuery(DbCommand cmd); /// <summary> /// 返回结果集中的第一行第一列 /// </summary> /// <param name="cmd"></param> /// <returns></returns> object ExecuteScalar(DbCommand cmd); /// <summary> /// 返回可读数据集 /// </summary> /// <param name="cmd"></param> /// <returns></returns> IDataReader ExecuteReader(DbCommand cmd); /// <summary> /// 反转数据行到实体(IDataReader) /// </summary> /// <typeparam name="T"></typeparam> /// <param name="idr"></param> /// <param name="IsOther"></param> /// <returns></returns> T ConvertToEntity<T>(IDataReader idr, bool IsOther = false); /// <summary> /// 反转数据行到实体(DataRow) /// </summary> /// <typeparam name="T"></typeparam> /// <param name="dataRow"></param> /// <param name="IsOther"></param> /// <returns></returns> T ConvertToEntity<T>(DataRow dataRow, bool IsOther = false); }//end
/// <summary> /// sqlserver 实现接口 /// </summary> public class SQLHelper : IDataBase { internal SqlConnection Link; public SQLHelper() { } public SQLHelper(string ConnectionStrings, bool isAppConfig) { this.Link = null; if (isAppConfig == true) { CreateDataBaseByConnectionStringsName(ConnectionStrings); } else { CreateDatabaseByConnectionStrings(ConnectionStrings); } } #region 创建数据库实体 /// <summary> /// 创建数据库连接,传入配置文件字段名 /// </summary> /// <param name="ConnectionStringsName">数据库配置字段</param> /// <returns></returns> private void CreateDataBaseByConnectionStringsName(string ConnectionStringsName) { SqlConnection link = new SqlConnection(); string ConnectionStrings = ConfigurationManager.ConnectionStrings[ConnectionStringsName].ToString(); link.ConnectionString = ConnectionStrings; try { link.Open(); this.Link = link; } catch (Exception e) { throw new Exception(e.Message); } } /// <summary> /// 创建数据库连接,直接传入连接字符串 /// </summary> /// <param name="connectionString">数据库连接串</param> /// <returns></returns> private void CreateDatabaseByConnectionStrings(string ConnectionStrings) { SqlConnection link = new SqlConnection(); link.ConnectionString = ConnectionStrings; try { link.Open(); this.Link = link; } catch (Exception e) { throw new Exception(e.Message); } } #endregion /// <summary> /// 关闭数据库 /// </summary> public void CloseDataBase(DbConnection Link) { SqlConnection Link_ = (SqlConnection)Link; if (Link_ != null && Link_.State == ConnectionState.Open) { Link_.Close(); } } #region 获取cmd /// <summary> /// 获取数据库语句执行命令 /// </summary> /// <param name="query">查询语句</param> /// <returns></returns> public DbCommand GetSqlStringCommand(string query) { SqlCommand cmd = null; if (Link != null && query != "") cmd = new SqlCommand(query, Link); return cmd; } /// <summary> /// 获取数据库存储过程执行命令 /// </summary> /// <param name="ProcName">查询语句</param> /// <returns></returns> public DbCommand GetStoredProcCommand(string ProcName) { SqlCommand cmd = null; if (Link != null && ProcName != "") { cmd = new SqlCommand(ProcName, Link); cmd.CommandType = CommandType.StoredProcedure; } return cmd; } #endregion #region 输入输出参数 /// <summary> /// 增加输入参数 /// </summary> /// <param name="cmd">数据库执行命令</param> /// <param name="ParmsName">参数名</param> /// <param name="ParmsType">参数类型</param> /// <param name="value">参数值</param> public void AddInParameter(DbCommand cmd, string ParmsName, DbType ParmsType, object value) { try { SqlParameter parameter = new SqlParameter(ParmsName, ParmsType); parameter.Direction = ParameterDirection.Input; if (value == null) parameter.Value = DBNull.Value; else parameter.Value = value; cmd.Parameters.Add(parameter); } catch (Exception e) { throw new Exception(e.Message); } } /// <summary> /// 增加输出参数 /// </summary> /// <param name="cmd">数据库执行命令</param> /// <param name="ParmsName">参数名</param> /// <param name="ParmsType">参数类型</param> /// <param name="value">参数值</param> public void AddOutParameter(DbCommand cmd, string ParmsName, DbType ParmsType, object value) { try { SqlParameter parameter = new SqlParameter(ParmsName, ParmsType); parameter.Direction = ParameterDirection.Output; if (value == null) parameter.Value = DBNull.Value; else parameter.Value = value; cmd.Parameters.Add(parameter); } catch (Exception e) { throw new Exception(e.Message); } } /// <summary> /// 添加 Database 参数信息 /// </summary> /// <typeparam name="T">从何类型获取参数属性</typeparam> /// <param name="cmd">数据库执行命令</param> /// <param name="t">参数属性取值来源</param> public void AddDataBaseParas<T>(DbCommand cmd, T t) { Type type = typeof(T); PropertyInfo[] proinfos = type.GetProperties(); foreach (PropertyInfo p in proinfos) { AddInParameter(cmd, "@" + p.Name, BuildDbType(p.PropertyType), p.GetValue(t, null)); } } /// <summary> /// 获取输出参数值或者输出参数的字典 /// </summary> /// <param name="cmd">数据库执行命令</param> /// <param name="ParmsName">参数名</param> /// <returns></returns> public object GetParameterValueOrKeyValue(DbCommand cmd, string ParmsName = "") { Dictionary<string, object> dic = null; try { dic = new Dictionary<string, object>(); foreach (SqlParameter parameter in cmd.Parameters) { if (parameter.Direction == ParameterDirection.Output) { dic.Add(parameter.ParameterName, parameter.Value); } } if (dic != null && dic.Count > 0) { if (ParmsName != "" && dic.ContainsKey(ParmsName)) return dic[ParmsName]; } else dic = null; } catch (Exception e) { throw new Exception(e.Message); } return dic; } #endregion #region SQL语句执行 /// <summary> /// 执行获取DataSet /// </summary> /// <param name="cmd">数据库执行命令</param> /// <returns></returns> public DataSet ExecuteDataSet(DbCommand cmd_) { DataSet ds = null; SqlCommand cmd = cmd_ as SqlCommand; try { SqlDataAdapter adapter = new SqlDataAdapter(cmd); ds = new DataSet(); adapter.Fill(ds); cmd.Parameters.Clear(); } catch (Exception e) { throw new Exception(e.Message); } return ds; } /// <summary> /// 执行sql语句返回受影响的行数 /// </summary> /// <param name="cmd"></param> /// <returns></returns> public int ExecuteNonQuery(DbCommand cmd) { int returnCount = 0; try { returnCount = cmd.ExecuteNonQuery(); } catch (Exception e) { throw new Exception(e.Message); } return returnCount; } /// <summary> /// 返回结果集中的第一行第一列 /// </summary> /// <param name="cmd"></param> /// <returns></returns> public object ExecuteScalar(DbCommand cmd) { object obj = null; try { obj = cmd.ExecuteScalar(); cmd.Parameters.Clear(); } catch (Exception e) { throw new Exception(e.Message); } return obj; } /// <summary> /// 返回可读数据集 /// </summary> /// <param name="cmd"></param> /// <returns></returns> public IDataReader ExecuteReader(DbCommand cmd) { IDataReader idr = null; try { idr = cmd.ExecuteReader(); cmd.Parameters.Clear(); } catch (Exception e) { throw new Exception(e.Message); } return idr; } #endregion #region 反转实体 /// <summary> /// 反转数据行到实体(IDataReader) /// </summary> /// <typeparam name="T">实体类型</typeparam> /// <param name="idr">IDataReader</param> /// <param name="t">结果</param> /// <param name="IsOther">是否有Other字段,即(并将实体中不存在的列储存在名为Other的Dictionary(string, object)中)</param> public T ConvertToEntity<T>(IDataReader idr, bool IsOther = false) { T t = default(T);//default 关键字对于引用类型会返回 null,对于数值类型会返回零 try { Type type = typeof(T); ConstructorInfo SearchType = type.GetConstructor(new Type[0]); if (SearchType != null && idr != null) { t = (T)SearchType.Invoke(new Object[0]); ConvertToEntity<T>(idr, t, IsOther); } } catch (Exception e) { throw new Exception(e.Message); } return t; } /// <summary> /// 反转数据行到实体(DataRow) /// </summary> /// <typeparam name="T">实体类型</typeparam> /// <param name="dataRow">数据行</param> /// <param name="t">结果</param> /// <param name="IsOther">是否有Other字段,即(并将实体中不存在的列储存在名为Other的Dictionary(string, object)中)</param> public T ConvertToEntity<T>(DataRow dataRow, bool IsOther = false) { T t = default(T);//default 关键字对于引用类型会返回 null,对于数值类型会返回零 try { Type type = typeof(T); ConstructorInfo SearchType = type.GetConstructor(new Type[0]); if (SearchType != null && dataRow != null) { t = (T)SearchType.Invoke(new Object[0]); ConvertToEntity<T>(dataRow, t, IsOther); } } catch (Exception e) { throw new Exception(e.Message); } return t; } #endregion #region 私有函数 /// <summary> /// 创建 DbType 类型 /// </summary> /// <param name="t">System数据类型</param> /// <returns></returns> private DbType BuildDbType(Type t) { switch (t.Name) { case "Byte": return DbType.Byte; case "Byte[]": return DbType.Binary; case "Int32": return DbType.Int32; case "Int64": return DbType.Int64; case "UInt16": return DbType.UInt16; case "UInt32": return DbType.UInt32; case "UInt64": return DbType.UInt64; case "Decimal": return DbType.Decimal; case "Double": return DbType.Double; case "Guid": return DbType.Guid; case "Xml": return DbType.Xml; case "Object": return DbType.Binary; case "Boolean": return DbType.Boolean; case "String": return DbType.String; case "DateTime": return DbType.DateTime; default: return DbType.String; } } /// <summary> /// 反转数据行到现有实体 /// </summary> /// <typeparam name="T">实体类型</typeparam> /// <param name="idr">IDataReader</param> /// <param name="t">实体对象</param> /// <param name="IsOther">是否有Other字段,即(并将实体中不存在的列储存在名为Other的Dictionary(string, object)中)</param> private void ConvertToEntity<T>(IDataReader idr, T t, bool IsOther) { Type type = typeof(T); Dictionary<string, object> dic = new Dictionary<string, object>(); for (int i = 0, fieldCount = idr.FieldCount; i < fieldCount; i++) { string fieldName = idr.GetName(i); PropertyInfo pInfo = type.GetProperty(fieldName); if (pInfo != null && pInfo.PropertyType != dic.GetType() && pInfo.CanWrite) { object obj = idr.GetValue(i); if (obj != null && obj != DBNull.Value) { if (pInfo.PropertyType == typeof(String)) pInfo.SetValue(t, obj.ToString(), null); else pInfo.SetValue(t, obj, null); } } else dic.Add(fieldName, idr.GetValue(i)); } PropertyInfo dictInfo = type.GetProperty("Other", dic.GetType()); if (dictInfo != null && dictInfo.CanWrite && IsOther == true) dictInfo.SetValue(t, dic, null); } /// <summary> /// 反转数据行到实体 /// </summary> /// <typeparam name="T">实体类型</typeparam> /// <param name="dataRow">数据行</param> /// <param name="t">结果</param> /// <param name="IsOther">是否有Other字段,即(并将实体中不存在的列储存在名为Other的Dictionary(string, object)中)</param> private void ConvertToEntity<T>(DataRow dataRow, T t, bool IsOther) { Type type = typeof(T); Dictionary<string, object> dic = new Dictionary<string, object>(); for (int i = 0, colCount = dataRow.Table.Columns.Count; i < colCount; i++) { string colName = dataRow.Table.Columns[i].ColumnName; PropertyInfo pInfo = type.GetProperty(colName); if (pInfo != null && pInfo.PropertyType != dic.GetType() && pInfo.CanWrite) { object obj = dataRow[i]; if (obj != null && obj != DBNull.Value) { if (pInfo.PropertyType == typeof(String)) pInfo.SetValue(t, obj.ToString(), null); else pInfo.SetValue(t, obj, null); } } else dic.Add(colName, dataRow[i]); } PropertyInfo dictInfo = type.GetProperty("Other", dic.GetType()); if (dictInfo != null && dictInfo.CanWrite && IsOther == true) dictInfo.SetValue(t, dic, null); } #endregion }//end
/// <summary> /// mysql 实现接口 /// </summary> public class MySQLHelper : IDataBase { internal MySqlConnection Link; public MySQLHelper() { } public MySQLHelper(string ConnectionStrings, bool isAppConfig) { this.Link = null; if (isAppConfig == true) { CreateDataBaseByConnectionStringsName(ConnectionStrings); } else { CreateDatabaseByConnectionStrings(ConnectionStrings); } } #region 创建数据库实体 /// <summary> /// 创建数据库连接,传入配置文件字段名 /// </summary> /// <param name="ConnectionStringsName">数据库配置字段</param> /// <returns></returns> private void CreateDataBaseByConnectionStringsName(string ConnectionStringsName) { MySqlConnection link = new MySqlConnection(); string ConnectionStrings = ConfigurationManager.ConnectionStrings[ConnectionStringsName].ToString(); link.ConnectionString = ConnectionStrings; try { link.Open(); this.Link = link; } catch (Exception e) { throw new Exception(e.Message); } } /// <summary> /// 创建数据库连接,直接传入连接字符串 /// </summary> /// <param name="connectionString">数据库连接串</param> /// <returns></returns> private void CreateDatabaseByConnectionStrings(string ConnectionStrings) { MySqlConnection link = new MySqlConnection(); link.ConnectionString = ConnectionStrings; try { link.Open(); this.Link = link; } catch (Exception e) { throw new Exception(e.Message); } } #endregion /// <summary> /// 关闭数据库 /// </summary> public void CloseDataBase(DbConnection Link) { MySqlConnection Link_ = (MySqlConnection)Link; if (Link_ != null && Link_.State == ConnectionState.Open) { Link_.Close(); } } #region 获取cmd /// <summary> /// 获取数据库语句执行命令 /// </summary> /// <param name="query">查询语句</param> /// <returns></returns> public DbCommand GetSqlStringCommand(string query) { MySqlCommand cmd = null; if (Link != null && query != "") cmd = new MySqlCommand(query, Link); return cmd; } /// <summary> /// 获取数据库存储过程执行命令 /// </summary> /// <param name="ProcName">查询语句</param> /// <returns></returns> public DbCommand GetStoredProcCommand(string ProcName) { MySqlCommand cmd = null; if (Link != null && ProcName != "") { cmd = new MySqlCommand(ProcName, Link); cmd.CommandType = CommandType.StoredProcedure; } return cmd; } #endregion #region 输入输出参数 /// <summary> /// 增加输入参数 /// </summary> /// <param name="cmd">数据库执行命令</param> /// <param name="ParmsName">参数名</param> /// <param name="ParmsType">参数类型</param> /// <param name="value">参数值</param> public void AddInParameter(DbCommand cmd, string ParmsName, DbType ParmsType_, object value) { try { MySqlDbType ParmsType = BuildDbType(ParmsType_.ToString()); MySqlParameter parameter = new MySqlParameter(ParmsName, ParmsType); parameter.Direction = ParameterDirection.Input; if (value == null) parameter.Value = DBNull.Value; else parameter.Value = value; cmd.Parameters.Add(parameter); } catch (Exception e) { throw new Exception(e.Message); } } /// <summary> /// 增加输出参数 /// </summary> /// <param name="cmd">数据库执行命令</param> /// <param name="ParmsName">参数名</param> /// <param name="ParmsType">参数类型</param> /// <param name="value">参数值</param> public void AddOutParameter(DbCommand cmd, string ParmsName, DbType ParmsType, object value) { try { MySqlParameter parameter = new MySqlParameter(ParmsName, ParmsType); parameter.Direction = ParameterDirection.Output; if (value == null) parameter.Value = DBNull.Value; else parameter.Value = value; cmd.Parameters.Add(parameter); } catch (Exception e) { throw new Exception(e.Message); } } /// <summary> /// 获取输出参数值或者输出参数的字典 /// </summary> /// <param name="cmd">数据库执行命令</param> /// <param name="ParmsName">参数名</param> /// <returns></returns> public object GetParameterValueOrKeyValue(DbCommand cmd, string ParmsName = "") { Dictionary<string, object> dic = null; try { dic = new Dictionary<string, object>(); foreach (MySqlParameter parameter in cmd.Parameters) { if (parameter.Direction == ParameterDirection.Output) { dic.Add(parameter.ParameterName, parameter.Value); } } if (dic != null && dic.Count > 0) { if (ParmsName != "" && dic.ContainsKey(ParmsName)) return dic[ParmsName]; } else dic = null; } catch (Exception e) { throw new Exception(e.Message); } return dic; } /// <summary> /// 添加 Database 参数信息 /// </summary> /// <typeparam name="T">从何类型获取参数属性</typeparam> /// <param name="cmd">数据库执行命令</param> /// <param name="t">参数属性取值来源</param> public void AddDataBaseParas<T>(DbCommand cmd, T t) { Type type = typeof(T); PropertyInfo[] proinfos = type.GetProperties(); foreach (PropertyInfo p in proinfos) { AddInParameter(cmd, "@" + p.Name, BuildDbType(p.PropertyType), p.GetValue(t, null)); } } #endregion #region SQL语句执行 /// <summary> /// 执行获取DataSet /// </summary> /// <param name="cmd">数据库执行命令</param> /// <returns></returns> public DataSet ExecuteDataSet(DbCommand cmd_) { DataSet ds = null; MySqlCommand cmd = cmd_ as MySqlCommand; try { MySqlDataAdapter adapter = new MySqlDataAdapter(cmd); ds = new DataSet(); adapter.Fill(ds); cmd.Parameters.Clear(); } catch (Exception e) { throw new Exception(e.Message); } return ds; } /// <summary> /// 执行sql语句返回受影响的行数 /// </summary> /// <param name="cmd"></param> /// <returns></returns> public int ExecuteNonQuery(DbCommand cmd) { int returnCount = 0; try { returnCount = cmd.ExecuteNonQuery(); } catch (Exception e) { throw new Exception(e.Message); } return returnCount; } /// <summary> /// 返回结果集中的第一行第一列 /// </summary> /// <param name="cmd"></param> /// <returns></returns> public object ExecuteScalar(DbCommand cmd) { object obj = null; try { obj = cmd.ExecuteScalar(); cmd.Parameters.Clear(); } catch (Exception e) { throw new Exception(e.Message); } return obj; } /// <summary> /// 返回可读数据集 /// </summary> /// <param name="cmd"></param> /// <returns></returns> public IDataReader ExecuteReader(DbCommand cmd) { IDataReader idr = null; try { idr = cmd.ExecuteReader(); cmd.Parameters.Clear(); } catch (Exception e) { throw new Exception(e.Message); } return idr; } #endregion #region 反转实体 /// <summary> /// 反转数据行到实体(IDataReader) /// </summary> /// <typeparam name="T">实体类型</typeparam> /// <param name="idr">IDataReader</param> /// <param name="t">结果</param> /// <param name="IsOther">是否有Other字段,即(并将实体中不存在的列储存在名为Other的Dictionary(string, object)中)</param> public T ConvertToEntity<T>(IDataReader idr, bool IsOther = false) { T t = default(T);//default 关键字对于引用类型会返回 null,对于数值类型会返回零 try { Type type = typeof(T); ConstructorInfo SearchType = type.GetConstructor(new Type[0]); if (SearchType != null && idr != null) { t = (T)SearchType.Invoke(new Object[0]); ConvertToEntity<T>(idr, t, IsOther); } } catch (Exception e) { throw new Exception(e.Message); } return t; } /// <summary> /// 反转数据行到实体(DataRow) /// </summary> /// <typeparam name="T">实体类型</typeparam> /// <param name="dataRow">数据行</param> /// <param name="t">结果</param> /// <param name="IsOther">是否有Other字段,即(并将实体中不存在的列储存在名为Other的Dictionary(string, object)中)</param> public T ConvertToEntity<T>(DataRow dataRow, bool IsOther = false) { T t = default(T);//default 关键字对于引用类型会返回 null,对于数值类型会返回零 try { Type type = typeof(T); ConstructorInfo SearchType = type.GetConstructor(new Type[0]); if (SearchType != null && dataRow != null) { t = (T)SearchType.Invoke(new Object[0]); ConvertToEntity<T>(dataRow, t, IsOther); } } catch (Exception e) { throw new Exception(e.Message); } return t; } #endregion #region 私有函数 /// <summary> /// 创建 DbType 类型 /// </summary> /// <param name="t">System数据类型</param> /// <returns></returns> private MySqlDbType BuildDbType(string t) { switch (t) { case "Byte": return MySqlDbType.Byte; case "Byte[]": return MySqlDbType.Binary; case "Int32": return MySqlDbType.Int32; case "Int64": return MySqlDbType.Int64; case "UInt16": return MySqlDbType.UInt16; case "UInt32": return MySqlDbType.UInt32; case "UInt64": return MySqlDbType.UInt64; case "Decimal": return MySqlDbType.Decimal; case "Double": return MySqlDbType.Double; //case "Guid": // return MySqlDbType.Guid; //case "Xml": // return MySqlDbType.Xml; case "Object": return MySqlDbType.Binary; case "Boolean": return MySqlDbType.Bit; case "String": return MySqlDbType.String; case "DateTime": return MySqlDbType.DateTime; default: return MySqlDbType.String; } } private static DbType BuildDbType(Type t) { switch (t.Name) { case "Byte": return DbType.Byte; case "Byte[]": return DbType.Binary; case "Int32": return DbType.Int32; case "Int64": return DbType.Int64; case "UInt16": return DbType.UInt16; case "UInt32": return DbType.UInt32; case "UInt64": return DbType.UInt64; case "Decimal": return DbType.Decimal; case "Double": return DbType.Double; case "Guid": return DbType.Guid; case "Xml": return DbType.Xml; case "Object": return DbType.Binary; case "Boolean": return DbType.Boolean; case "String": return DbType.String; case "DateTime": return DbType.DateTime; default: return DbType.String; } } /// <summary> /// 反转数据行到现有实体 /// </summary> /// <typeparam name="T">实体类型</typeparam> /// <param name="idr">IDataReader</param> /// <param name="t">实体对象</param> /// <param name="IsOther">是否有Other字段,即(并将实体中不存在的列储存在名为Other的Dictionary(string, object)中)</param> private void ConvertToEntity<T>(IDataReader idr, T t, bool IsOther) { Type type = typeof(T); Dictionary<string, object> dic = new Dictionary<string, object>(); for (int i = 0, fieldCount = idr.FieldCount; i < fieldCount; i++) { string fieldName = idr.GetName(i); PropertyInfo pInfo = type.GetProperty(fieldName); if (pInfo != null && pInfo.PropertyType != dic.GetType() && pInfo.CanWrite) { object obj = idr.GetValue(i); if (obj != null && obj != DBNull.Value) { if (pInfo.PropertyType == typeof(String)) pInfo.SetValue(t, obj.ToString(), null); else pInfo.SetValue(t, obj, null); } } else dic.Add(fieldName, idr.GetValue(i)); } PropertyInfo dictInfo = type.GetProperty("Other", dic.GetType()); if (dictInfo != null && dictInfo.CanWrite && IsOther == true) dictInfo.SetValue(t, dic, null); } /// <summary> /// 反转数据行到实体 /// </summary> /// <typeparam name="T">实体类型</typeparam> /// <param name="dataRow">数据行</param> /// <param name="t">结果</param> /// <param name="IsOther">是否有Other字段,即(并将实体中不存在的列储存在名为Other的Dictionary(string, object)中)</param> private void ConvertToEntity<T>(DataRow dataRow, T t, bool IsOther) { Type type = typeof(T); Dictionary<string, object> dic = new Dictionary<string, object>(); for (int i = 0, colCount = dataRow.Table.Columns.Count; i < colCount; i++) { string colName = dataRow.Table.Columns[i].ColumnName; PropertyInfo pInfo = type.GetProperty(colName); if (pInfo != null && pInfo.PropertyType != dic.GetType() && pInfo.CanWrite) { object obj = dataRow[i]; if (obj != null && obj != DBNull.Value) { if (pInfo.PropertyType == typeof(String)) pInfo.SetValue(t, obj.ToString(), null); else pInfo.SetValue(t, obj, null); } } else dic.Add(colName, dataRow[i]); } PropertyInfo dictInfo = type.GetProperty("Other", dic.GetType()); if (dictInfo != null && dictInfo.CanWrite && IsOther == true) dictInfo.SetValue(t, dic, null); } #endregion }//end
mysql 需要导入 MySql.Data.dll
实现方式:
public class DataBase { public static DBTools.DBFactory fac = new DBTools.DBFactory(); public static IDataBase Sql_idb = null;static DataBase() { Sql_idb = fac.Creatobject("DBTools", "SQLHelper", new object[] { "PNOContext", true }) as IDataBase; } public static Dictionary<string, string> getState(string MachineCode) { Dictionary<string, string> dic = new Dictionary<string, string>(); string sqlStr = @""; DbCommand cmd = Sql_idb.GetSqlStringCommand(sqlStr); Sql_idb.AddInParameter(cmd, "@MachineCode", DbType.String, MachineCode); DataTable dt = Sql_idb.ExecuteDataSet(cmd).Tables[0]; foreach (DataRow dr in dt.Rows) { } return dic; } }//end
标签:
原文地址:http://www.cnblogs.com/hcfan/p/5089552.html