Add methods to retrieve companies from database according to their start letter and country.

This commit is contained in:
2018-04-20 19:19:51 +01:00
parent f8c5484974
commit ecb8941872

View File

@@ -110,6 +110,75 @@ namespace Cicm.Database
}
}
/// <summary>
/// Gets all companies that start with the specified letter
/// </summary>
/// <param name="entries">All companies</param>
/// <returns><c>true</c> if <see cref="entries" /> is correct, <c>false</c> otherwise</returns>
public bool GetCompanies(out List<Company> 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;
}
}
/// <summary>
/// Gets all companies from the specified country
/// </summary>
/// <param name="entries">All companies</param>
/// <returns><c>true</c> if <see cref="entries" /> is correct, <c>false</c> otherwise</returns>
public bool GetCompanies(out List<Company> 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;
}
}
/// <summary>
/// Gets company by specified id
/// </summary>