private Contact SQLGetContact(string query)
{
using (SqlConnection connection = new SqlConnection(config.connectionString))
{
Contact contact = new Contact();
SqlCommand command = new SqlCommand(query, connection);
connection.Open();
SqlDataReader reader = command.ExecuteReader();
try
{
if (reader.Read())
{
// Loop through a list of contact fields that are configured and add them to the return object
foreach (string field in config.contactConfig.fields)
{
try
{
contact.GetType().GetProperty(field).SetValue(contact, reader[field].ToString(), null);
}
catch (Exception ex)
{
throw new WebFaultException<string>(String.Format("Column <{0}> not found in query results: {1}", field, ex.Message), HttpStatusCode.InternalServerError);
}
}
// Get all the phone numbers
contact.PhoneNumbers = getPhoneNumbers(reader, config.contactConfig.phoneCount, config.contactConfig.phoneMappings);
// Get all the email addresses
contact.EmailAddresses = getEmails(reader, config.contactConfig.emailCount, config.contactConfig.emailMappings);
if (config.contactConfig.getAddress)
{
// Get the address
contact.Address = getAddress(reader, config.contactConfig.addressFields);
}
}
else
{
throw new WebFaultException<string>("No results found", HttpStatusCode.NoContent);
}
}
catch (Exception ex)
{
throw ex;
}
finally
{
reader.Close();
}
return contact;
}
}