Esempio n. 1
0
 private List <Contact> GetList(SqlCommand command)
 {
     using (SqlConnection connection = new SqlConnection(_ConnectionString))
     {
         command.Connection = connection;
         connection.Open();
         try
         {
             using (SqlDataReader reader = command.ExecuteReader())
             {
                 if (reader.HasRows)
                 {
                     ContactsDataConverter converter = new ContactsDataConverter();
                     List <Contact>        result    = new List <Contact>();
                     while (reader.Read())
                     {
                         Contact contact = (converter.ContactFromDataReader(reader));
                         result.Add(contact);
                         Logger.Log.Info($" New result row added, {contact.ToString()}");
                     }
                     connection.Close();
                     return(result);
                 }
                 else
                 {
                     Logger.Log.Info($" No results");
                     connection.Close();
                     return(null);
                 }
             };
         }
         catch (SqlException ex)
         {
             Logger.Log.Info($"SQLCommand failed, {ex.ToString()}");
             throw new SQLCommandException(ex);
         }
     }
 }
Esempio n. 2
0
        internal Contact GetById(string id)
        {
            string searchExpression = "SELECT * from dbo.Contact WHERE ID = @id";

            using (SqlConnection connection = new SqlConnection(_ConnectionString))
            {
                SqlCommand searchCommand = new SqlCommand(searchExpression, connection);
                searchCommand.Parameters.Add(new SqlParameter("@id", id));
                connection.Open();
                try
                {
                    using (SqlDataReader reader = searchCommand.ExecuteReader())
                    {
                        if (reader.HasRows)
                        {
                            ContactsDataConverter converter = new ContactsDataConverter();
                            Contact contact = new Contact();
                            while (reader.Read())
                            {
                                contact = (converter.ContactFromDataReader(reader));
                            }
                            connection.Close();
                            return(contact);
                        }
                        else
                        {
                            connection.Close();
                            return(null);
                        }
                    };
                }
                catch (SqlException ex)
                {
                    Logger.Log.Info($"SQLCommand failed, {ex.ToString()}");
                    throw new SQLCommandException(ex);
                }
            }
        }