From ecb894187270bdaf3cbadb896780e6ffcbae866c Mon Sep 17 00:00:00 2001 From: Natalia Portillo Date: Fri, 20 Apr 2018 19:19:51 +0100 Subject: [PATCH] Add methods to retrieve companies from database according to their start letter and country. --- Cicm.Database/Operations/Company.cs | 69 +++++++++++++++++++++++++++++ 1 file changed, 69 insertions(+) diff --git a/Cicm.Database/Operations/Company.cs b/Cicm.Database/Operations/Company.cs index fa127438..6413faf8 100644 --- a/Cicm.Database/Operations/Company.cs +++ b/Cicm.Database/Operations/Company.cs @@ -110,6 +110,75 @@ namespace Cicm.Database } } + /// + /// Gets all companies that start with the specified letter + /// + /// All companies + /// true if is correct, false otherwise + public bool GetCompanies(out List entries, char startingLetter) + { + if((startingLetter < 'a' || startingLetter > 'z') && (startingLetter < 'A' || startingLetter > 'Z')) + return GetCompanies(out entries); + + #if DEBUG + Console.WriteLine("Getting all companies that start with {0}..."); + #endif + + try + { + IDbCommand dbCmd = dbCon.CreateCommand(); + IDbDataAdapter dataAdapter = dbCore.GetNewDataAdapter(); + dbCmd.CommandText = $"SELECT * from companies WHERE name LIKE {startingLetter}%"; + DataSet dataSet = new DataSet(); + dataAdapter.SelectCommand = dbCmd; + dataAdapter.Fill(dataSet); + + entries = CompaniesFromDataTable(dataSet.Tables[0]); + + return true; + } + catch(Exception ex) + { + Console.WriteLine("Error getting companies."); + Console.WriteLine(ex); + entries = null; + return false; + } + } + + /// + /// Gets all companies from the specified country + /// + /// All companies + /// true if is correct, false otherwise + public bool GetCompanies(out List entries, int countryCode) + { + #if DEBUG + Console.WriteLine("Getting all companies that start with {0}..."); + #endif + + try + { + IDbCommand dbCmd = dbCon.CreateCommand(); + IDbDataAdapter dataAdapter = dbCore.GetNewDataAdapter(); + dbCmd.CommandText = $"SELECT * from companies WHERE country = '{countryCode}'"; + DataSet dataSet = new DataSet(); + dataAdapter.SelectCommand = dbCmd; + dataAdapter.Fill(dataSet); + + entries = CompaniesFromDataTable(dataSet.Tables[0]); + + return true; + } + catch(Exception ex) + { + Console.WriteLine("Error getting companies."); + Console.WriteLine(ex); + entries = null; + return false; + } + } + /// /// Gets company by specified id ///