资讯

精准传达 • 有效沟通

从品牌网站建设到网络营销策划,从策略到执行的一站式服务

MySQL通过实例化对象参数如何查询数据

下文主要给大家带来MySQL通过实例化对象参数如何查询数据 ,希望这些内容能够带给大家实际用处,这也是我编辑MySQL通过实例化对象参数如何查询数据 这篇文章的主要目的。好了,废话不多说,大家直接看下文吧。                                                          

创新互联公司10年专注成都高端网站建设按需定制服务,为客户提供专业的成都网站制作,成都网页设计,成都网站设计服务;创新互联公司服务内容包含成都网站建设,微信小程序开发,软件开发,网络营销推广,网络运营服务及企业形象设计;创新互联公司拥有众多专业的高端网站制作开发团队,资深的高端网页设计团队及经验丰富的架构师高端网站策划团队;我们始终坚持从客户的角度出发,为客户量身订造网络营销方案,解决网络营销疑问。

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通过实例化对象参数如何查询数据 ,大家是不是觉得非常有帮助。如果需要了解更多内容,请继续关注我们的行业资讯,相信你会喜欢上这些内容的。


当前名称:MySQL通过实例化对象参数如何查询数据
当前地址:http://cdkjz.cn/article/phodgg.html
多年建站经验

多一份参考,总有益处

联系快上网,免费获得专属《策划方案》及报价

咨询相关问题或预约面谈,可以通过以下方式与我们联系

业务热线:400-028-6601 / 大客户专线   成都:13518219792   座机:028-86922220