public object GetEnityByPrimaryKey(object objEntity) { object objResult = null; try { GetConnection(); string strPrimaryKey = null; if (sqlCMD.Connection.State == ConnectionState.Open) { if (sqlCMD.Connection.State == ConnectionState.Closed) { MySqlConnection.GetConnection.OpenConnection(sqlCon); } CommonMSSQL.ClearParameter(sqlCMD); #region Get Primarykey Information from object PropertyInfo[] properties = objEntity.GetType().GetProperties(); foreach (PropertyInfo property in properties) { var attribute = Attribute.GetCustomAttribute(property, typeof(KeyAttribute)) as KeyAttribute; if (attribute != null) // This property has a KeyAttribute { // Do something, to read from the property: //object val = property.GetValue(objPrimaryKey); strPrimaryKey = ((TTAttributs)objEntity.GetType().GetProperty(property.Name).GetCustomAttribute(typeof(TTAttributs), false)).FieldName; break; } } #endregion CommonMSSQL.AddParameter(sqlCMD, objEntity, strPrimaryKey); sqlCMD.CommandType = CommandType.Text; sqlCMD.CommandText = CommonMSSQL.PrepairSelectPrimaryKey(HelperMethod.GetTableName(objEntity).ToString(), objEntity); using (SqlDataReader dr = sqlCMD.ExecuteReader()) { objResult = FillEntityFromReader(dr, objEntity); dr.Close(); } } } catch (Exception) { objResult = null; throw; } finally { MySqlConnection.GetConnection.CloseConnection(sqlCon); } return(objResult); }
public object GetEnityByPerameters(object objEntity, Dictionary <string, Enumration.Operators> parFields) { object objResult = null; try { GetConnection(); if (sqlCMD.Connection.State == ConnectionState.Open) { if (sqlCMD.Connection.State == ConnectionState.Closed) { MySqlConnection.GetConnection.OpenConnection(sqlCon); } CommonMSSQL.ClearParameter(sqlCMD); for (int i = 0; i < parFields.Count; i++) { var el = parFields.ElementAt(i); CommonMSSQL.AddParameter(sqlCMD, objEntity, el.Key); } sqlCMD.CommandType = CommandType.Text; sqlCMD.CommandText = CommonMSSQL.PrepairSelectPerameters(parFields, HelperMethod.GetTableName(objEntity), objEntity); using (SqlDataReader dr = sqlCMD.ExecuteReader()) { objResult = FillEntityFromReader(dr, objEntity); dr.Close(); } } } catch (Exception) { objResult = null; throw; } finally { MySqlConnection.GetConnection.CloseConnection(sqlCon); } return(objResult); }