• linkedu视频
  • 平面设计
  • 电脑入门
  • 操作系统
  • 办公应用
  • 电脑硬件
  • 动画设计
  • 3D设计
  • 网页设计
  • CAD设计
  • 影音处理
  • 数据库
  • 程序设计
  • 认证考试
  • 信息管理
  • 信息安全
菜单
linkedu.com
  • 网页制作
  • 数据库
  • 程序设计
  • 操作系统
  • CMS教程
  • 游戏攻略
  • 脚本语言
  • 平面设计
  • 软件教程
  • 网络安全
  • 电脑知识
  • 服务器
  • 视频教程
  • MsSql
  • Mysql
  • oracle
  • MariaDB
  • DB2
  • SQLite
  • PostgreSQL
  • MongoDB
  • Redis
  • Access
  • 数据库其它
  • sybase
  • HBase
您的位置:首页 > 数据库 >Mysql > MySQL如何通过实例化对象参数查询数据 ?(源代码)

MySQL如何通过实例化对象参数查询数据 ?(源代码)

作者:匿名 字体:[增加 减小] 来源:互联网 时间:2018-12-05

匿名通过本文主要向大家介绍了MySQL查询数据,MySQL等相关知识,希望本文的分享对您有所帮助
本篇文章给大家带来的内容是关于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如何通过实例化对象参数查询数据 ?(源代码)的详细内容,更多请关注微课江湖其它相关文章!

分享到:QQ空间新浪微博腾讯微博微信百度贴吧QQ好友复制网址打印

您可能想查找下面的文章:

  • MySQL如何通过实例化对象参数查询数据 ?(源代码)
  • MySQL 修改数据库名称的一个新奇方法

相关文章

  • 2018-12-05mysql的加密方法
  • 2018-12-05mysql alter table命令修改表结构实例_MySQL
  • 2017-05-11php基础之连接mysql数据库和查询数据
  • 2018-12-05access数据库用sql语句添加字段,修改字段,删除字段
  • 2018-12-05MySQL选择合适的引擎及引擎转换的详解
  • 2018-12-05 写给毕业生
  • 2018-12-05SQLSERVER 创建索引实现代码
  • 2018-12-05详解mysql函数拼接查询concat函数的使用方法
  • 2018-12-05SqlServer中的日期与时间函数
  • 2017-05-11mySQL占用虚拟内存达8百多兆问题解决思路

文章分类

  • MsSql
  • Mysql
  • oracle
  • MariaDB
  • DB2
  • SQLite
  • PostgreSQL
  • MongoDB
  • Redis
  • Access
  • 数据库其它
  • sybase
  • HBase

最近更新的内容

    • MySQL异常处理浅析
    • MySQL 统计最小数据 Select Min
    • Ubuntu Server下MySql数据库备份脚本代码
    • 【MySQL数据库】第二章解读:MySQL基准测试
    • MySQL锁的用法之表级锁
    • MySQL中文乱码问题的解决第1/2页
    • MySQL中的max()函数使用教程
    • SQL2005 服务器因重装改名后出错的说明
    • PHP连接MySql闪断自动重连的方法_MySQL
    • 解析mysql中如何获得数据库的大小

关于我们 - 联系我们 - 免责声明 - 网站地图

©2020-2025 All Rights Reserved. linkedu.com 版权所有