• MysqlHelper使用反射机制智能推算数据类型以及属性名称


        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 <= 0)
                {
                    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<>));
            }
    
        }
  • 相关阅读:
    ORM 实现数据库表的增删改查
    数据库表设计(一对多,多对多)
    Razor模板引擎
    文件的上传(表单上传和ajax文件异步上传)
    生成验证码封装(新版)
    MD5加密
    反射的一些基本用法
    数据的增删改查(三层)<!--待补充-->
    linux文件的硬连接和软连接
    linux磁盘用满的两种情况
  • 原文地址:https://www.cnblogs.com/xuhongfei/p/4528695.html
Copyright © 2020-2023  润新知