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


本文摘自PHP中文网,作者云罗郡主,侵删。

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

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

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视频教程,请关注PHP中文网。

以上就是MySQL如何通过实例化对象参数查询数据 ?(源代码)的详细内容,更多文章请关注木庄网络博客!!

相关阅读 >>

mysql数据库增量备份的实现思路方法介绍

如何更改mysql数据库的编码字符集

mysql事务处理详解

mysql怎么添加复合主键?

mysql存储引擎myisam和innodb之间的比较

mysql explain使用详解

mysql存储过程是什么意思?

sql好学吗?

数据库删除语句delete有什么用?

mysql如何设置数据表的默认编码格式

更多相关阅读请进入《MySQL查询数据》频道 >>


数据库系统概念 第6版
书籍

数据库系统概念 第6版

机械工业出版社

本书主要讲述了数据模型、基于对象的数据库和XML、数据存储和查询、事务管理、体系结构等方面的内容。



打赏

取消

感谢您的支持,我会继续努力的!

扫码支持
扫码打赏,您说多少就多少

打开支付宝扫一扫,即可进行扫码打赏哦

分享从这里开始,精彩与您同在

评论

管理员已关闭评论功能...