MySQL通过实例化对象参数查询实例讲解
本篇文章给大家带来的内容是关于MySQL如何通过实例化对象参数查询数据 ?(源代码),有一定的参考价值,有需要的朋友可以参考一下,希望对你有所帮助。 public static string QueryByE
本篇文章给大家带来的内容是关于MySQL如何通过实例化对象参数查询数据 ?(源代码),有一定的参考价值,有需要的朋友可以参考一下,希望对你有所帮助。
public static string QueryByEntity
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
通过实例化参数,对属性赋值,将对象作为参数传入,反射获取对象名称,列名,列值。要求对象名与表名一致,属性与列名一致,感谢大家对每日运维的支持。