public int Insert(ECTable _table) { Type t = _table.GetType(); PropertyInfo[] properties = t.GetProperties().Where(x => x.IsDefined(typeof(ECTableFieldAttribute), false)).ToArray(); string sql = "INSERT INTO `" + t.Name + "` ("; foreach (PropertyInfo p in properties.Where(x => !x.IsDefined(typeof(ECAutoIncrementAttribute), false))) { sql += "`" + p.Name + "`,"; } sql = sql.Substring(0, sql.Length - 1); //delete last comma sql += ") VALUES ("; foreach (PropertyInfo p in properties.Where(x => !x.IsDefined(typeof(ECAutoIncrementAttribute), false))) { sql += _table.GetValueInSqlFormat(p) + ","; } sql = sql.Substring(0, sql.Length - 1); //delete last comma sql += ");"; using (MySqlCommand cmd = new MySqlCommand(sql, connection)) cmd.ExecuteNonQuery(); sql = "SELECT MAX(RecId) AS RecId FROM " + _table.GetType().Name; using (MySqlCommand cmd = new MySqlCommand(sql, connection)) using (MySqlDataReader res = cmd.ExecuteReader()) { res.Read(); return(res.GetInt32(nameof(_table.RecId))); } }
virtual protected string MakeSelectFrom(ECTable _table, bool _isRootTable = false) { string ret; if (_isRootTable) { ret = "SELECT "; } else { ret = ""; } foreach (PropertyInfo p in _table.GetType().GetProperties().Where(x => x.IsDefined(typeof(ECTableFieldAttribute)))) { ret += GetSqlTableName(_table) + "." + p.Name + " AS '" + _table.TableName + "." + p.Name + "',"; } foreach (ECJoin j in _table.Joins) { ret += MakeSelectFrom((ECTable)j.Table) + ","; } ret = ret.Substring(0, ret.Length - 1); if (_isRootTable) { ret += $" FROM {GetSqlTableName(_table)}"; } return(ret); }