MySQL通过实例化对象参数如何查询数据
发表于:2025-11-10 作者:千家信息网编辑
千家信息网最后更新 2025年11月10日,下文主要给大家带来MySQL通过实例化对象参数如何查询数据 ,希望这些内容能够带给大家实际用处,这也是我编辑MySQL通过实例化对象参数如何查询数据 这篇文章的主要目的。好了,废话不多说,大家直接看下
千家信息网最后更新 2025年11月10日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安全错误
数据库的锁怎样保障安全
数据库字段显示怎么改
文件服务器权限管理方法
常州idc服务器哪个厂家质量好
公共场所wifi网络安全
前端定义一个按钮删除数据库
关系数据库的索引有那两种
加强网络管理 实施网络安全
电脑和服务器同步
网络安全形势教育提纲
易语言内存数据库sqlite
软件开发信息理解误差
平谷区品牌软件开发质量服务
提高网络安全知识主题班会内容
虚拟机服务器开机
上海系统软件开发价钱
unix服务器日志怎么看
戴尔服务器加装sata硬盘
维护国家网络安全的技术群
有机物三元相图数据库
攻坚克难能力 软件开发
ps4服务器存档
软件开发走哪个方向
在软件开发过程中抽取和整理
企业软件开发有哪些项目类型
ppt 模板 网络安全
网络技术部招新
北京嬴信通网络技术
软件开发17%
深圳市服务器
郑州嵌入式软件开发