MySQL通過實(shí)例化對象參數(shù)查詢實(shí)例講解
本篇文章給大家?guī)淼膬?nèi)容是關(guān)于MySQL如何通過實(shí)例化對象參數(shù)查詢數(shù)據(jù) ?(源代碼),有一定的參考價值,有需要的朋友可以參考一下,希望對你有所幫助。
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("查詢數(shù)據(jù)庫失敗,{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;
}
}
通過實(shí)例化參數(shù),對屬性賦值,將對象作為參數(shù)傳入,反射獲取對象名稱,列名,列值。要求對象名與表名一致,屬性與列名一致,感謝大家對腳本之家的支持。
- 淺談pymysql查詢語句中帶有in時傳遞參數(shù)的問題
- 使用Limit參數(shù)優(yōu)化MySQL查詢的方法
- MySQL8.0內(nèi)存相關(guān)參數(shù)總結(jié)
- python mysql中in參數(shù)化說明
- Python MySQLdb 執(zhí)行sql語句時的參數(shù)傳遞方式
- Python MySQL 日期時間格式化作為參數(shù)的操作
- MYSQL配置參數(shù)優(yōu)化詳解
- MySQL性能全面優(yōu)化方法參考,從CPU,文件系統(tǒng)選擇到mysql.cnf參數(shù)優(yōu)化
- MySQL 5.6下table_open_cache參數(shù)優(yōu)化合理配置詳解
- MySQL 參數(shù)相關(guān)概念及查詢更改方法
相關(guān)文章
mysql-8.0.15-winx64 解壓版安裝教程及退出的三種方式
本文通過圖文并茂的形式給大家介紹了mysql-8.0.15-winx64 解壓版安裝,非常不錯,具有一定的參考借鑒價值,需要的朋友可以參考下2019-04-04
通過mysqladmin遠(yuǎn)程管理mysql的方法
在一些特殊場景下,想要遠(yuǎn)程重啟mysql,以便讓某些修改能及時的生效,但是mysql并沒有提供遠(yuǎn)程重啟的功能,唯一能做的就是遠(yuǎn)程關(guān)閉mysql服務(wù)2013-03-03
mysql動態(tài)游標(biāo)學(xué)習(xí)(mysql存儲過程游標(biāo))
mysql動態(tài)游標(biāo)示例,通過準(zhǔn)備語句、視圖和靜態(tài)游標(biāo)實(shí)現(xiàn),大家參考使用吧2013-12-12
詳解DBeaver連接MySQL8以上版本以及解決可能遇到的問題
這篇文章主要介紹了DBeaver連接MySQL8以上版本以及解決可能遇到的問題,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2020-11-11
MySQL拋出Incorrect string value異常分析
從上至下統(tǒng)一用上UTF-8就高枕無憂,今天還是遇到字符的異常,本文將介紹解決方法2012-11-11
Windows10下mysql 8.0.16 安裝配置方法圖文教程
這篇文章主要為大家詳細(xì)介紹了Windows10下mysql 8.0.16 安裝配置方法圖文教程,具有一定的參考價值,感興趣的小伙伴們可以參考一下2019-05-05
Mysql數(shù)據(jù)庫從5.6.28版本升到8.0.11版本部署項(xiàng)目時遇到的問題及解決方法
這篇文章主要介紹了Mysql數(shù)據(jù)庫從5.6.28版本升到8.0.11版本過程中遇到的問題及解決方法,解決辦法有三種,每種方法給大家介紹的都很詳細(xì),感興趣的朋友跟隨腳本之家小編一起學(xué)習(xí)吧2018-05-05

