public DataTable GetListByCriteria(TableDrafteeCriteria criteria) { try { StringBuilder sqlBuilder = new StringBuilder(); sqlBuilder .Append("SELECT dr.id id, ") .Append("CONCAT(dr.last_name, ' ', dr.first_name, ' ', dr.patronymic) full_name, ") .Append("YEAR(dr.birth_date) birth_year ") .Append("FROM draftees dr "); bool havingExists = false; bool needAnd = false; if (!string.IsNullOrEmpty(criteria.FullName)) { sqlBuilder.Append("HAVING full_name LIKE '%") .Append(criteria.FullName) .Append("%' "); havingExists = true; needAnd = true; } if (criteria.BirthYear != null) { if (!havingExists) { sqlBuilder.Append("HAVING "); } if (needAnd) { sqlBuilder.Append("AND "); } sqlBuilder.Append("birth_year=") .Append(criteria.BirthYear.Value) .Append(" "); } sqlBuilder.Append(";"); var dataAdapter = new MySqlDataAdapter(sqlBuilder.ToString(), ConnectionUtils.GetConnection()); DataTable dataTable = new DataTable(); dataAdapter.Fill(dataTable); return(dataTable); } catch { return(null); } }
public TableDrafteeCriteria Build(string fullName, string year) { TableDrafteeCriteria criteria = new TableDrafteeCriteria(); if (!string.IsNullOrEmpty(fullName.Trim())) { criteria.FullName = fullName.Trim(); } int birthYear; Int32.TryParse(year, out birthYear); if (birthYear > 0) { criteria.BirthYear = birthYear; } return(criteria); }