利用反射插入資料庫與更新資料庫

來源:互聯網
上載者:User

標籤:


public int Insert<T>(T m) where T : class
{
  PropertyInfo[] pInfos = m.GetType().GetProperties();
  FieldInfo[] fInfos = m.GetType().GetFields();

  List<string> lstCol = new List<string>();
  List<OleDbParameter> lstParam = new List<OleDbParameter>();

  for (int i = 0; i < pInfos.GetLength(0); i++)
  {
    PropertyInfo pInfo = pInfos[i];
    object value = pInfo.GetValue(m, null);
    string name = pInfo.Name;
    string DeclarType = pInfo.PropertyType.Name;
    if (DeclarType.ToUpper().Contains("DateTime".ToUpper()))
    {
      value = ((DateTime)value).ToString("yyyy-MM-dd HH:mm:ss.fff");
    }

    lstCol.Add(name);
    lstParam.Add(new OleDbParameter("@" + name, value));
  }

  string Keys = " " + string.Join(",", lstCol.ToArray());
  string Values = " " + "@" + string.Join(",@", lstCol.ToArray());


  string sql;
  sql = @"Insert into " + GetTableName() + "("
  + Keys
  + ")values("
  + Values
  + ")";

  string ErrorMsg;
  return DBAHelp.DBA_ExecuteNonQuery(sql, (OleDbParameter[])(lstParam.ToArray()), out ErrorMsg);

}

public int Update<T>(T m, string UpdateKey) where T : class
{
  string FullName = m.GetType().FullName;
  T mNew = Activator.CreateInstance(m.GetType()) as T;

  PropertyInfo[] pInfos = m.GetType().GetProperties();
  PropertyInfo[] pInfosNew = mNew.GetType().GetProperties();


  List<string> lstCol = new List<string>();
  List<OleDbParameter> lstParam = new List<OleDbParameter>();

  OleDbParameter KeyParam = null;
  string sKey = "";

  for (int i = 0; i < pInfos.GetLength(0); i++)
  {
    PropertyInfo pInfo = pInfos[i];
    object value = pInfo.GetValue(m, null);
    string name = pInfo.Name;

    PropertyInfo pInfoNew = pInfosNew[i];
    object valueNew = pInfoNew.GetValue(mNew, null);

    if (UpdateKey.ToUpper() == name.ToUpper())
    {
      KeyParam = new OleDbParameter("@" + name, value);
      sKey = name + "[email protected]" + name;
    }
    else if (!value.Equals(valueNew))
    {
      string Line = name + "[email protected]" + name;
      lstCol.Add(Line);
      lstParam.Add(new OleDbParameter("@" + name, value));
    }
  }

  string Lines = string.Join(",", lstCol.ToArray());
  if (KeyParam != null)
  {
    lstParam.Add(KeyParam);
  }

  string sql;
  sql = @"Update " + GetTableName() + " Set "
  + Lines
  + " Where "
  + sKey
  + "";

  string ErrorMsg;
  return DBAHelp.DBA_ExecuteNonQuery(sql, (OleDbParameter[])(lstParam.ToArray()), out ErrorMsg);
}

利用反射插入資料庫與更新資料庫

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.