public class MySqlHelper
{
private string ConnString; public MySqlHelper(string connectionString)
{
ConnString = connectionString;
}
public IList<T> RunMySqlSelect4ReturnList<T>(string strCommand) where T : new()
{ MySqlCommand mySqlCommand = new MySqlCommand();
mySqlCommand.CommandText = strCommand.ToString();
IList<PropertyInfo> ilPropertyInfo = typeof(T).GetProperties().ToList(); //p[0].Name =travel_id;
IList<T> ilResult = new List<T>(); DataTable dt = new DataTable();
using (MySqlConnection conn = new MySqlConnection(this.ConnString))
{
conn.Open();
using (MySqlCommand cmd = mySqlCommand)
{
cmd.Connection = conn;
using (MySqlDataReader rdr = cmd.ExecuteReader(CommandBehavior.SequentialAccess))
{
dt.Load(rdr);
}
}
} foreach (DataRow dr in dt.Rows)
{
T tItem = new T();
foreach (var v in ilPropertyInfo)
{
if (dt.Columns[v.Name] == null) continue;
ConvertionExtensions.SetValue(tItem, v.Name, dr[v.Name]);
}
ilResult.Add(tItem);
}
return ilResult;
}
public T getSinggleObj<T>(string strCommand) where T : new()
{ MySqlCommand mySqlCommand = new MySqlCommand();
mySqlCommand.CommandText = strCommand.ToString();
IList<PropertyInfo> ilPropertyInfo = typeof(T).GetProperties().ToList(); //p[0].Name =travel_id;
T ilResult = new T(); DataTable dt = new DataTable();
using (MySqlConnection conn = new MySqlConnection(this.ConnString))
{
conn.Open();
using (MySqlCommand cmd = mySqlCommand)
{
cmd.Connection = conn;
using (MySqlDataReader rdr = cmd.ExecuteReader(CommandBehavior.SequentialAccess))
{
dt.Load(rdr);
}
}
}
if (dt.Rows.Count <= )
{
return default(T);
}
else
{
foreach (DataRow dr in dt.Rows)
{
T tItem = new T();
foreach (var v in ilPropertyInfo)
{
if (dt.Columns[v.Name] == null) continue;
ConvertionExtensions.SetValue(tItem, v.Name, dr[v.Name]);
}
ilResult = tItem;
}
}
return ilResult;
}
public IList<T> RunMySqlSelect4ReturnList<T>(DataTable dts) where T : new()
{ IList<PropertyInfo> ilPropertyInfo = typeof(T).GetProperties().ToList(); //p[0].Name =travel_id;
IList<T> ilResult = new List<T>(); DataTable dt = dts; foreach (DataRow dr in dt.Rows)
{
T tItem = new T();
foreach (var v in ilPropertyInfo)
{
if (dt.Columns[v.Name] == null) continue;
ConvertionExtensions.SetValue(tItem, v.Name, dr[v.Name]);
}
ilResult.Add(tItem);
}
return ilResult;
}
}
public static class ConvertionExtensions
{
public static T ConvertTo<T>(this IConvertible convertibleValue)
{
var t = typeof(T);
if (null == convertibleValue)
{
return default(T);
}
if (!typeof(T).IsGenericType)
{
return (T)Convert.ChangeType(convertibleValue, typeof(T));
}
else
{
Type genericTypeDefinition = typeof(T).GetGenericTypeDefinition();
if (genericTypeDefinition == typeof(Nullable<>))
{
return (T)Convert.ChangeType(convertibleValue, Nullable.GetUnderlyingType(typeof(T)));
}
}
throw new InvalidCastException(string.Format("Invalid cast from type \"{0}\" to type \"{1}\".", convertibleValue.GetType().FullName, typeof(T).FullName));
} public static void SetValue(object inputObject, string propertyName, object propertyVal)
{
//find out the type
Type type = inputObject.GetType(); //get the property information based on the type
System.Reflection.PropertyInfo propertyInfo = type.GetProperty(propertyName); //find the property type
Type propertyType = propertyInfo.PropertyType; //Convert.ChangeType does not handle conversion to nullable types
//if the property type is nullable, we need to get the underlying type of the property
var targetType = IsNullableType(propertyInfo.PropertyType) ? Nullable.GetUnderlyingType(propertyInfo.PropertyType) : propertyInfo.PropertyType; //Returns an System.Object with the specified System.Type and whose value is
//equivalent to the specified object.
propertyVal = Convert.ChangeType(propertyVal, targetType); //Set the value of the property
propertyInfo.SetValue(inputObject, propertyVal, null); } private static bool IsNullableType(Type type)
{
return type.IsGenericType && type.GetGenericTypeDefinition().Equals(typeof(Nullable<>));
} }

MysqlHelper使用反射机制智能推算数据类型以及属性名称的更多相关文章

  1. 通过java反射机制,获取对象的属性和值(包括所有继承的父类)

    java的反射机制提供了两种方法: getDeclaredFields() :该方法能获取到本类的所有属性,包括private,protected和public,但不能获取到继承的父类的属性. get ...

  2. 浅谈Java反射机制 之 使用类的 属性、方法和构造函数

    前面两篇我们总结了Java反射机制如何获取类的字节码,如何获取构造函数,属性和方法, 这篇我们将进一步验证如何使用我们获取到的属性.方法以及构造函数 1.使用 反射 获取到的 属性 import ja ...

  3. Android : 反射机制获取或设置系统属性(SystemProperties)【转】

    本文转载自:https://blog.csdn.net/wei_lei/article/details/70312512 Android.os.SystemProperties 提供了获取和设置系统属 ...

  4. C#利用反射机制,获取实例的属性和属性值

    C#利用反射,遍历获得一个类的所有属性名,以及该类的实例的所有属性的值 对应某个类的实例化的对象tc, 遍历获取所有属性(子成员)的方法(采用反射): Type t = tc.GetType();// ...

  5. 【java】解析java类加载与反射机制

    目录结构: contents structure [+] 类的加载.连接和初始化 类的加载 类的连接 类的初始化 类加载器 类加载器机制 自定义类加载器 URLClassLoader类 反射的常规操作 ...

  6. Android反射机制:手把手教你实现反射

    什么是反射机制? JAVA反射机制是在运行状态中,对于任意一个类,都能够知道这个类的所有属性和方法:对于任意一个对象,都能够调用它的任意一个方法和属性:这种动态获取的信息以及动态调用对象的方法的功能称 ...

  7. 面试题思考:什么是 Java 的反射机制

    一.反射机制概述 Java 反射机制是在运行状态中,对于任意一个类,都能够获得这个类的所有属性和方法,对于任意一个对象都能够调用它的任意一个属性和方法.这种在运行时动态的获取信息以及动态调用对象的方法 ...

  8. 面试题之------Java 反射机制

    一.反射机制概述 Java 反射机制是在运行状态中,对于任意一个类,都能够获得这个类的所有属性和方法,对于任意一个对象都能够调用它的任意一个属性和方法.这种在运行时动态的获取信息以及动态调用对象的方法 ...

  9. 【Java基础】java中的反射机制与动态代理

    一.java中的反射机制 java反射的官方定义:在运行状态下,可以获取任意一个类的所有属性和方法,并且可通过某类任意一对象实例调用该类的所有方法.这种动态获取类的信息及动态调用类中方法的功能称为ja ...

随机推荐

  1. setOnPageChangeListener 过时了怎么办?

    今天使用ViewPager发现setOnPageChangeListener的方法竟然过期了.并且AS编译不通过了,最后查了一下原来把set换成add了,代码例如以下: setOnPageChange ...

  2. sphinx增量索引和主索引来实现索引的实时更新

    项目中文章的信息内容因为持续有新增,而文章总量的基数又比较大,所以做搜索的时候,用了主索引+增量索引这种方式来实现索引的实时更新. 实现原理: 1. 新建一张表,记录一下上一次已经创建好索引的最后一条 ...

  3. PHP判断ajax请求:HTTP_X_REQUESTED_WITH

    PHP判断ajax请求的原理: 在发送ajax请求的时候,我们可以通过XMLHttpRequest这个对象,创建自定义的 header头信息, 在jquery框架中,对于通过它的$.ajax, $.g ...

  4. Java输出字符串格式问题 .UnknownFormatConversionException

    今天遇到一个问题,使用JSoup挖掘出的数据一直出错 Exception in thread "main" java.util.UnknownFormatConversionExc ...

  5. Go Revel - server.go 源码分析

    之前介绍了 Go Revel - main函数分析 http://www.cnblogs.com/hangxin1940/p/3263775.html 最后会调用 `revel.Run(*port)` ...

  6. Qt入门——使用QT+VS2008开发windows应用程序

    1.文件->新建->项目 Qt4 Projects 右边已安装模板当中选择At Application. 确定 2.选择需要使用的QT库 下一步 3. “class name”:指定类的名 ...

  7. 1 php protocolbuffers安装

    安装工具 yum install autoconf yum install libtool 安装protoc编译器 # cd /root/soft/protobuf- autogen.sh : if ...

  8. js学习(一)-动态添加、修改、删除对象的属性和方法

    //-----------------------js代码--------------------------- function class1(){ } //-------------------- ...

  9. SVN和IntelliJ IDEA忽略node_module设置

    SVN提交忽略node_modules 1.空白处右键>选中TortoiseSVN>设置(settings) 2.常规设置(General)>Subversion>编辑(edi ...

  10. 实验一 ASP.NET应用环境配置 总结

    通过本次实验我学会了在Windows XP系统中安装IIS服务器,虽然我的电脑是Windows7系统,但是通过虚拟机软件,配置了一台Windows XP系统的虚拟机,在虚拟机内进行各项配置操作.这次实 ...