private void SetConditionIndexes(ref SearchEntity search) { int index = 0; foreach (Condition cond in search.Conditions) { cond.Index = index; } }
internal SqlParameter[] BuildSqlParametersForRead(SearchEntity search) { List <SqlParameter> result = new List <SqlParameter>(); foreach (Condition cond in search.Conditions) { result.Add(new SqlParameter(cond.GetParameterName(), cond.Value)); } return(result.ToArray()); }
public List <T> Read(SearchEntity search) { List <T> result = new List <T>(); List <Entity> readResult = new BaseRepository().Read(search); foreach (Entity entity in readResult) { result.Add(new ResponseMapper <T>().MapFromEntity(entity)); } return(result); }
internal string BuildCommandTextForRead(SearchEntity search) { StringBuilder command = new StringBuilder(); if (search.PageSizeLimit > 0) { command.AppendLine("SELECT TOP(" + search.PageSizeLimit + ")"); } else { command.AppendLine("SELECT"); } if (search.ColumnsToReturn == null || search.ColumnsToReturn.Count == 0) { command.AppendLine("*"); } else { command.AppendLine(search.ColumnsToReturn[0]); for (int i = 1; i < search.ColumnsToReturn.Count; i++) { command.AppendLine(", " + search.ColumnsToReturn[i]); } } command.AppendLine("FROM " + search.SchemaName + "." + search.TableName); if (search.Conditions.Count > 0) { command.AppendLine("WHERE"); command.AppendLine(search.Conditions[0].ToString()); for (int i = 1; i < search.Conditions.Count; i++) { command.AppendLine("AND " + search.Conditions[i].ToString()); } } if (search.OrderBy != null) { command.AppendLine("ORDER BY " + search.OrderBy.Column + " " + Enum.GetName(typeof(OrderByDirection), search.OrderBy.Direction)); } return(command.ToString()); }
public List <Entity> Read(SearchEntity search) { List <Entity> result = new List <Entity>(); try { SetConditionIndexes(ref search); SqlCommand command = new SqlCommand(); CommandBuilder builder = new CommandBuilder(); command.CommandText = builder.BuildCommandTextForRead(search); command.Connection = new SqlConnection(search.ConnectionString); command.Parameters.AddRange(builder.BuildSqlParametersForRead(search)); command.Connection.Open(); SqlDataReader reader = command.ExecuteReader(); while (reader.Read()) { Entity entity = new Entity(); for (int colIndex = 0; colIndex < reader.FieldCount; colIndex++) { entity.SetValue(reader.GetName(colIndex), reader[colIndex]); } result.Add(entity); } command.Connection.Close(); } catch { } return(result); }