MySQL通过实例化对象参数如何查询数据
发表于:2026-11-02 作者:千家信息网编辑
千家信息网最后更新 2026年11月02日,下文主要给大家带来MySQL通过实例化对象参数如何查询数据 ,希望这些内容能够带给大家实际用处,这也是我编辑MySQL通过实例化对象参数如何查询数据 这篇文章的主要目的。好了,废话不多说,大家直接看下
千家信息网最后更新 2026年11月02日MySQL通过实例化对象参数如何查询数据
下文主要给大家带来MySQL通过实例化对象参数如何查询数据 ,希望这些内容能够带给大家实际用处,这也是我编辑MySQL通过实例化对象参数如何查询数据 这篇文章的主要目的。好了,废话不多说,大家直接看下文吧。
public static string QueryByEntity(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 (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 tList = new List (); 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 (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通过实例化对象参数如何查询数据 ,大家是不是觉得非常有帮助。如果需要了解更多内容,请继续关注我们的行业资讯,相信你会喜欢上这些内容的。
对象
参数
实例
数据
查询
内容
一致
下文
属性
名称
实际
废话
数据库
更多
用处
目的
篇文章
行业
资讯
反射
数据库的安全要保护哪些东西
数据库安全各自的含义是什么
生产安全数据库录入
数据库的安全性及管理
数据库安全策略包含哪些
海淀数据库安全审计系统
建立农村房屋安全信息数据库
易用的数据库客户端支持安全管理
连接数据库失败ssl安全错误
数据库的锁怎样保障安全
互联网加科技
互联网佳科技有限公司
互联网科技公司的优势
北京互联网科技有限公司
腾讯科技 互联网女皇
深圳天众互联网科技公司
赫美联合互联网科技
润盈互联网科技
广州善班互联网科技
互联网和高科技公司
华服互联网科技官网
栖莱科技互联网股份有限公司
互联网科技与管理
浙南科技城互联网大厦建设工程
黑云互联网科技有限公司
江苏互联网智能科技产品出厂价
中国互联网公司的科技投入
恒生科技指数 中概互联网
重庆科技产业互联网有哪些
恒生互联网科技趋势
瑰夏咖啡
互联网最新的科技有限公司
合肥互联网科技公司
互联网科技logo
哈尔滨秒房互联网科技有限公司
博兴的互联网电子科技公司
西藏七维空间互联网科技有限公司
互联网大会黑科技排名
青岛科技大学互联网大赛研究生
银川新华互联网科技学校原画