濮阳杆衣贸易有限公司

主頁 > 知識庫 > MySQL通過實例化對象參數(shù)查詢實例講解

MySQL通過實例化對象參數(shù)查詢實例講解

熱門標簽:電銷機器人針對的 外呼系統(tǒng)防封號違法嗎 如何在高德地圖標注新地址 高德地圖標注中心個人注冊 400電話辦理都選易號網(wǎng) 高德地圖標注模式 寶應(yīng)電信400電話辦理費用 湘潭電銷機器人咨詢電話 外呼系統(tǒng)服務(wù)

本篇文章給大家?guī)淼膬?nèi)容是關(guān)于MySQL如何通過實例化對象參數(shù)查詢數(shù)據(jù) ?(源代碼),有一定的參考價值,有需要的朋友可以參考一下,希望對你有所幫助。

public static string QueryByEntityT>(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.GetPropertyValueT>(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();
    ListT> tList = new ListT>();    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 GetPropertyValueT>(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ù)傳入,反射獲取對象名稱,列名,列值。要求對象名與表名一致,屬性與列名一致,感謝大家對腳本之家的支持。

您可能感興趣的文章:
  • 淺談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)概念及查詢更改方法

標簽:黃山 馬鞍山 佛山 賀州 宿遷 黔南 南充 蘭州

巨人網(wǎng)絡(luò)通訊聲明:本文標題《MySQL通過實例化對象參數(shù)查詢實例講解》,本文關(guān)鍵詞  MySQL,通過,實例,化,對象,;如發(fā)現(xiàn)本文內(nèi)容存在版權(quán)問題,煩請?zhí)峁┫嚓P(guān)信息告之我們,我們將及時溝通與處理。本站內(nèi)容系統(tǒng)采集于網(wǎng)絡(luò),涉及言論、版權(quán)與本站無關(guān)。
  • 相關(guān)文章
  • 下面列出與本文章《MySQL通過實例化對象參數(shù)查詢實例講解》相關(guān)的同類信息!
  • 本頁收集關(guān)于MySQL通過實例化對象參數(shù)查詢實例講解的相關(guān)信息資訊供網(wǎng)民參考!
  • 推薦文章
    高青县| 民县| 沅江市| 秦安县| 景宁| 长子县| 郧西县| 汝南县| 乌拉特中旗| 启东市| 延津县| 漾濞| 饶平县| 五华县| 廉江市| 平武县| 峨边| 紫云| 保山市| 高清| 赫章县| 泊头市| 金塔县| 漳平市| 岑溪市| 新密市| 鄂温| 岚皋县| 石台县| 万州区| 昌平区| 东阿县| 晋江市| 寻乌县| 沙河市| 丹棱县| 无为县| 乌鲁木齐县| 邯郸县| 疏勒县| 化隆|