最新文章专题视频专题问答1问答10问答100问答1000问答2000关键字专题1关键字专题50关键字专题500关键字专题1500TAG最新视频文章推荐1 推荐3 推荐5 推荐7 推荐9 推荐11 推荐13 推荐15 推荐17 推荐19 推荐21 推荐23 推荐25 推荐27 推荐29 推荐31 推荐33 推荐35 推荐37视频文章20视频文章30视频文章40视频文章50视频文章60 视频文章70视频文章80视频文章90视频文章100视频文章120视频文章140 视频2关键字专题关键字专题tag2tag3文章专题文章专题2文章索引1文章索引2文章索引3文章索引4文章索引5123456789101112131415文章专题3
当前位置: 首页 - 科技 - 知识百科 - 正文

MySQL通过实例化对象参数查询实例讲解

来源:动视网 责编:小采 时间:2020-11-09 20:23:35
文档

MySQL通过实例化对象参数查询实例讲解

MySQL通过实例化对象参数查询实例讲解:本篇文章给大家带来的内容是关于MySQL如何通过实例化对象参数查询数据 ?(源代码),有一定的参考价值,有需要的朋友可以参考一下,希望对你有所帮助。 public static string QueryByEntity<T>(T t) where T : new() { st
推荐度:
导读MySQL通过实例化对象参数查询实例讲解:本篇文章给大家带来的内容是关于MySQL如何通过实例化对象参数查询数据 ?(源代码),有一定的参考价值,有需要的朋友可以参考一下,希望对你有所帮助。 public static string QueryByEntity<T>(T t) where T : new() { st


本篇文章给大家带来的内容是关于MySQL如何通过实例化对象参数查询数据 ?(源代码),有一定的参考价值,有需要的朋友可以参考一下,希望对你有所帮助。

public static string QueryByEntity<T>(T t) where T : new()
{ string resultstr = string.Empty;
 MySqlDataReader reader = null; try
 {
 Type type = typeof(T);
 PropertyInfo[] properties = type.GetProperties(); string select = string.Format("Select * from {0} {1}", type.Name, "{0}"); string where = string.Empty; foreach (PropertyInfo property in properties)
 { var value = t.GetPropertyValue<T>(property); if (value != null && !value.Equals(property.GetDefaultValue()))
 { if (string.IsNullOrEmpty(where))
 { where = string.Format(" where {0}='{1}' ", property.Name, value);
 } else
 { where = string.Format(" {0} and {1} = '{2}' ", where, property.Name, value);
 }
 }
 } select = string.Format(select, where);
 
 MySqlConnection connection = OpenConnection(); if (connection == null) return resultstr;
 MySqlCommand _sqlCom = new MySqlCommand(select, connection);
 reader = _sqlCom.ExecuteReader();
 List<T> tList = new List<T>(); while (reader.Read())
 {
 T t1 = new T(); foreach (PropertyInfo property in properties)
 { if (!string.IsNullOrEmpty(reader[property.Name].ToString()))
 {
 property.SetMethod.Invoke(t1, new object[] { reader[property.Name] });
 }
 }
 tList.Add(t1);
 }
 resultstr = JsonConvert.SerializeObject(tList);
 } catch (Exception ex)
 {
 Logging.Error(string.Format("查询数据库失败,{0}", ex.Message));
 } finally
 { if (reader != null)
 {
 reader.Close();
 reader.Dispose();
 }
 } return resultstr;
}internal static class ObjectExtend
{ public static object GetPropertyValue<T>(this object obj, PropertyInfo property)
 {
 Type type = typeof(T);
 PropertyInfo propertyInfo = type.GetProperty(property.Name); if (propertyInfo != null)
 { return propertyInfo.GetMethod.Invoke(obj, null);
 } return null;
 } public static object GetDefaultValue(this PropertyInfo property)
 { return property.PropertyType.IsValueType ? Activator.CreateInstance(property.PropertyType) : null;
 }
}

通过实例化参数,对属性赋值,将对象作为参数传入,反射获取对象名称,列名,列值。要求对象名与表名一致,属性与列名一致,感谢大家对脚本之家的支持。

您可能感兴趣的文章:

  • MYSQL配置参数优化详解
  • MySQL性能全面优化方法参考,从CPU,文件系统选择到mysql.cnf参数优化
  • MySQL 5.6下table_open_cache参数优化合理配置详解
  • MySQL的常见存储引擎介绍与参数设置调优
  • MySQL中slave_exec_mode参数详解
  • MySQL中参数sql_safe_updates在生产环境的使用详解
  • mysql数据存储过程参数实例详解
  • 文档

    MySQL通过实例化对象参数查询实例讲解

    MySQL通过实例化对象参数查询实例讲解:本篇文章给大家带来的内容是关于MySQL如何通过实例化对象参数查询数据 ?(源代码),有一定的参考价值,有需要的朋友可以参考一下,希望对你有所帮助。 public static string QueryByEntity<T>(T t) where T : new() { st
    推荐度:
    标签: 查询 对象 参数
    • 热门焦点

    最新推荐

    猜你喜欢

    热门推荐

    专题
    Top