In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-09-23 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Database >
Share
Shulou(Shulou.com)06/01 Report--
The content of this article is about how MySQL queries data by instantiating object parameters. (source code), there is a certain reference value, friends in need can refer to, hope to help you.
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 ("query database failed, {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;}}
By instantiating the parameter, assigning a value to the property, passing the object as a parameter, and reflecting to get the object name, column name, column value. The object name is required to be consistent with the table name, and the attribute is consistent with the column name. Thank you for your support.
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
The market share of Chrome browser on the desktop has exceeded 70%, and users are complaining about
The world's first 2nm mobile chip: Samsung Exynos 2600 is ready for mass production.According to a r
A US federal judge has ruled that Google can keep its Chrome browser, but it will be prohibited from
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
About us Contact us Product review car news thenatureplanet
More Form oMedia: AutoTimes. Bestcoffee. SL News. Jarebook. Coffee Hunters. Sundaily. Modezone. NNB. Coffee. Game News. FrontStreet. GGAMEN
© 2024 shulou.com SLNews company. All rights reserved.